Loop through rows and columns

Eurekaonide

Active Member
Joined
Feb 1, 2010
Messages
422
Hi
Please can you help my code is not working.

I want it to work through the rows and columns and if the cell is blank move on to the next cell, if that cell is not blank copy the data and paste it into cell K1, K2 etc.
Dropping down a row each time it finishes.

My range of data is ever changing in length but always the same number of columns (5) so A - E
The second set of data will start in K1 onwards and downwards.

Thanks in advance

VBA Code:
Sub Test2()
Dim x As Integer
Application.ScreenUpdating = False

Sheets("Sheet2").Select

NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count

Range("A2").Select

For x = 1 To NumRows
    If Cells(i, 2).Value = "" Then
  Next
    ElseIf Cells(i, 2).Value = "<>""" Then
    Cells(i, 2).Copy

    Range("K1").PasteSpecial
    End If
Next


Application.ScreenUpdating = True


End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about
VBA Code:
Sub Eurekaonide()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   With Sheets("Sheet2")
      Ary = .Range("A1", .Range("A" & Rows.count).End(xlUp)).Resize(, 5).Value2
   End With
   ReDim Nary(1 To UBound(Ary) * 5, 1 To 1)
   
   For r = 1 To UBound(Ary)
      For c = 1 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            Nary(nr, 1) = Ary(r, c)
         End If
      Next c
   Next r
   Sheets("Sheet2").Range("K1").Resize(nr).Value = Nary
End Sub
 
Upvote 0
How about
VBA Code:
Sub Eurekaonide()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
  
   With Sheets("Sheet2")
      Ary = .Range("A1", .Range("A" & Rows.count).End(xlUp)).Resize(, 5).Value2
   End With
   ReDim Nary(1 To UBound(Ary) * 5, 1 To 1)
  
   For r = 1 To UBound(Ary)
      For c = 1 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            Nary(nr, 1) = Ary(r, c)
         End If
      Next c
   Next r
   Sheets("Sheet2").Range("K1").Resize(nr).Value = Nary
End Sub
Thats great thank you so much - how would I get it to paste into K, L,M etc rather than all in column K - Basically it needs to replicate the format skipping past any blanks and leaving the subsequent cell/column blank where its pasting. (its because its forming a part of a larger piece of work and I cant just copy and paste).
Thanks in advance
 
Upvote 0
I'm afraid I have no idea what you mean, you cannot replicate 5 columns of data into 3
 
Upvote 0
VBA Code:
Sub Check()
        Dim lr, k, i As Long
        Dim store As Integer
        lr = Range("A" & Rows.Count).End(xlUp).Row
       
       
        For k = 1 To lr
                For i = 1 To 5
                      If Cells(k, i) <> "" Then
                            store = store + 1
                      End If
                Next i
                    If store = 5 Then
                        Range("A" & k).Resize(1, 5).Copy Range("k" & Rows.Count).End(xlUp).Offset(1, 0)
                    End If
                store = 0
        Next k
       Range("k1:o1").Cells.Delete shift:=xlUp
       
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top