Solved

Excel 2003 copy only positive numbers of a list and paste to new column

Posted on 2014-10-13
3
275 Views
Last Modified: 2014-10-27
I have a list of positive and negative numbers in column C. I want to extract only the positive numbers to a new column E. A formula or VBA  for excel 2003 would be nice
0
Comment
Question by:llawrenceg
  • 2
3 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40377565
Hi,

pls try

Sub Macro1()

    ActiveSheet.Range("A1:A100").AutoFilter Field:=1, Criteria1:=">0", _
        Operator:=xlAnd
    Range("A1:A100").Copy Range("E1")
    ActiveSheet.Range("A1:A100").AutoFilter
End Sub

Open in new window

Regards
0
 
LVL 2

Assisted Solution

by:Glen Richmond
Glen Richmond earned 100 total points
ID: 40379438
Hard coding the range ("A1:A100") will limit the scalability and dynamism of the code.. You need the code to dynamically adapt to more rows.
Mabye concatenate on the UsedRange.Rows.Count into Rgonzo's solution?

I just wrote this alternative that can be added to any module and tailored to use ..

Sub RemoveNegs()
    
    'set up obj var to sheet
    Dim wrkSht As Excel.Worksheet
    
    Dim intSource As Integer
    Dim intTarget As Integer
    
    'set up row e target row starting value
    intTarget = 1
    
    'set obj to sheet
    Set wrkSht = Application.Worksheets("Sheet1")
    
    'loop through all rows used
    For intSource = 1 To wrkSht.UsedRange.Rows.Count
        
        'is the number posertive?
        If wrkSht.Cells(intSource, "C").Value >= 0 Then
            
            'place value into target row E
            wrkSht.Cells(intTarget, "E").Value = wrkSht.Cells(intSource, "C").Value
            'add next row for target row (E)
            intTarget = intTarget + 1
        End If
        
    Next
    
End Sub

Open in new window


Hope this helps?
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 400 total points
ID: 40379466
then try

Sub Macro1()
    Set myRange = Range("C1:C" & Range("C" & Rows.Count).End(xlUp).Row)
    myRange.AutoFilter Field:=1, Criteria1:=">0", _
        Operator:=xlAnd
    myRange.Copy Range("E1")
    myRange.AutoFilter
End Sub

Regards
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 109
Selenium and Interactive Data Language 3 53
add projects t working set in maven 2 38
storing csv file in table variable in Python 2 59
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question