Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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

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
llawrenceg
Asked:
llawrenceg
  • 2
2 Solutions
 
Rgonzo1971Commented:
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
 
Glen RichmondCommented:
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
 
Rgonzo1971Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now