Solved

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

Posted on 2014-10-13
3
251 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 49

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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now