How to copy/paste only cells>0 from a range


Posted by Mac P. on May 15, 2000 10:46 AM

Let's say I have values in the range (A1:F1). I want to copy using (For...next) only cells that have value>0 and paste them verticaly in colum H starting at H3. Thanks a lot for your help.

Posted by Celia on May 15, 2000 3:37 PM

Mac P.
Here's one way :-

Dim I As Integer
Range("A1:F1").Copy
Range("H3").PasteSpecial Paste:=xlAll, Transpose:=True
For I = 8 To 3 Step -1
If Cells(I, 8).Value < 0 Then Cells(I, 8).Delete Shift:=xlUp
Next

Celia


Posted by Mac P. on May 25, 2000 6:49 AM

Posted by Mac P. on May 25, 2000 6:54 AM

Thank you Celia for your help. I'm wondering if there is another way without deleting the cells that would be zero. Thanks a lot.


Posted by Celia on May 25, 2000 1:49 PM

Mac P
I think you mean WITH deleting the zeros :-

Dim I As Integer
Range("A1:F1").Copy
Range("H3").PasteSpecial Paste:=xlAll, Transpose:=True
For I = 8 To 3 Step -1
If Cells(I, 8).Value < 0 Or Cells(I, 8).Value = 0 Then Cells(I, 8).Delete Shift:=xlUp
Next

Celia


Posted by Mac P. on May 25, 2000 6:59 PM

Hi Celia. Actualy this is what I meant. The code that you recommended works like this: copy cell by cell, paste one by one but delete the cell where was pasted zero and shifts cells up. My question was if you know how to paste one cell after another only cells with value>0, without deleting, shifting up. In other words: if A1>0 copy A1 and paste it in H3, if A1<=0 do nothing (no copy or paste), if A2>0 copy A2 and paste it in H3 if H3 is empty or in H4 if H3 is number, if A2<=0 do nothing and so on. I'm tring to avoid the paste and delete/shift cell when zero because it screws up the cells nearby. Thank alot for all your help.

Posted by Mac P. on May 25, 2000 7:08 PM

It's something wrong with my browser; the prev post was cut off. I'll try again.

I do not know what's going on, it seems that my browser misbehave or my post is too large and it shows incomplete on top of the page; in Comments box is OK.

Posted by Mac P. on May 25, 2000 7:15 PM

Celia please read my post in the Comments box cause the one on top of the page is cut off

Posted by Celia on May 25, 2000 7:35 PM


Mac P
Try this :-

Dim cell As Range, dest As Range
Set dest = Range("H3")
Range("H3:H8").ClearContents
For Each cell In Range("A1:F1")
If cell.Value > 0 Then
Do Until dest = ""
Set dest = dest.Offset(1, 0)
Loop
cell.Copy dest
End If
Next

Celia

Posted by Celia on May 26, 2000 3:27 AM

P.S.
I've just looked at my code again and noticed that I've just about looped it to death.
The following shorter code will achieve the same result :-

Sub Macro1()
Dim cell As Range, dest As Range
Set dest = Range("H3")
Range("H3:H8").ClearContents
For Each cell In Range("A1:F1")
If cell.Value > 0 Then
cell.Copy dest
Set dest = dest.Offset(1, 0)
End If
Next
End Sub

Celia


Posted by Mac P. on May 26, 2000 8:03 AM

Thank you so much Celia, this is exactly what I needed.

Thanks alot for your help, your last code works perfectly. By the way, if I can ask, can you recommend any manual because I would like to learn more about VBA for Excel? Thank you again zillion times.



Posted by Celia on May 26, 2000 12:41 PM

Re: Thank you so much Celia, this is exactly what I needed.


Mac P.
As a start, you might like to try Excel Visual Basic Step by Step written by Reed Jacobson.
Celia