Excel VBA - I'm looking for code that will loop through a column and return a value to another column if a condition is true.

I have data in Column H of my excel file.  I want to hit a button on the sheet that will loop through the data in column H and if the data is not blank or a zero i want the value that is in that cell to be placed in cell J1.  Then if it finds another value that is not blank or a zero i want that value place in cell J2 and so on.

If my H column looked like this:
H1 = Green
H2 =
H3 = 0
H4 = 0
H5 = Blue

I would want my J column to look like this:
J1= Green
J2= Blue
jnikodymAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Professor JMicrosoft Excel ExpertCommented:
Sub test

With activesheet
LR= .cells(.cells.rows.count, "H").end(xlup)
End with
For each c in activesheet.range("h1:h"&LR)
If c.value<>"" and c.value<>0 then
C.offset(0,-2).value=c.value
End if
Next c
End sub
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Try this....
Sub CopyNonBlankValues()
Dim lr As Long
Dim rng As Range, cell As Range
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "H").End(xlUp).Row
Set rng = Range("H1:H" & lr)
For Each cell In rng
   If cell <> 0 Then
      cell.Copy
      If Range("J1").Value = "" Then
         Range("J1").PasteSpecial xlPasteValues
      Else
         Range("J" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
      End If
   End If
Next cell
Application.CutCopyMode = 0
Application.ScreenUpdating = True
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.