Loop through all cells in worksheet a, copy and consolidate to b minus blanks

RobGEOD

New Member
Joined
Oct 25, 2016
Messages
19
I have data in rows 1-50 and columns A:AN. For each cell in columns 2,4,6,etc. has data I need to copy that cell and the one next to it to a consolidated worksheet in groups of 50. Below is a small example of what I'm trying to do:

Data
[TABLE="width: 500"]
<tbody>[TR]
[TD]1
[/TD]
[TD]aaaaa
[/TD]
[TD]6
[/TD]
[TD]bbbb
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD][/TD]
[TD]7
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]aaaaa
[/TD]
[TD]8
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]aaaaa
[/TD]
[TD]9
[/TD]
[TD]bbbb
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD][/TD]
[TD]10
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Desired output
[TABLE="width: 500"]
<tbody>[TR]
[TD]1
[/TD]
[TD]aaaaa
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]aaaaa
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]aaaaa
[/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]bbbb
[/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD]bbbb
[/TD]
[/TR]
</tbody>[/TABLE]
 
Give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub AllToActive()
  Dim R As Long, LastCol As Long, Col As Long
  Application.ScreenUpdating = False
  With Sheets("All")
    LastCol = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
    For Col = 1 To LastCol Step 2
      Intersect(.Columns(Col).Resize(, 2), .Rows("1:50")).Copy Sheets("NewActive").Cells(Rows.Count, "A").End(xlUp).Offset(1)
    Next
  End With
  With Sheets("NewActive")
    On Error GoTo NoBlanks
    .Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
    For R = 51 To .Cells(Rows.Count, "A").End(xlUp).Row Step 50
      .Cells(R, "A").Resize(50, 2).Cut .Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
    Next
  End With
NoBlanks:
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Rick Rothstein, you are the MAN! Works perfectly. Thank you, thank you, thank you!

Give this macro a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub AllToActive()
  Dim R As Long, LastCol As Long, Col As Long
  Application.ScreenUpdating = False
  With Sheets("All")
    LastCol = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
    For Col = 1 To LastCol Step 2
      Intersect(.Columns(Col).Resize(, 2), .Rows("1:50")).Copy Sheets("NewActive").Cells(Rows.Count, "A").End(xlUp).Offset(1)
    Next
  End With
  With Sheets("NewActive")
    On Error GoTo NoBlanks
    .Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
    For R = 51 To .Cells(Rows.Count, "A").End(xlUp).Row Step 50
      .Cells(R, "A").Resize(50, 2).Cut .Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
    Next
  End With
NoBlanks:
  Application.ScreenUpdating = True
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,224,833
Messages
6,181,240
Members
453,026
Latest member
cknader

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