Loop through Rows and Return Non-Blank Cells Based on Column Headers [VBA]

eda

New Member
Joined
Jul 23, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

I've seen some similar questions but not quite what I need asked before, any help or suggestions would be greatly appreciated.

I have multiple rows that contain column headers with the same name (but for a number added on to the end), what I’d like to do is to loop through each row and column and output the row data so only non-blank cells relevant to the header name are returned. I've attached an image of the original table and also the desired output as hopefully this will provide a clearer idea as to what I'd like to achieve.

Many thanks in advance.
 

Attachments

  • Rows&Columns1.jpg
    Rows&Columns1.jpg
    100.2 KB · Views: 15
  • Output1.jpg
    Output1.jpg
    61.4 KB · Views: 15

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,603
Office Version
  1. 2007
Platform
  1. Windows
I did try to run the code on another computer and received a Run-time error ‘13’: Type Mismatch
I believe the error arose as I had text longer than a single line in the Formula Bar
Try the following code.

VBA Code:
Sub Loop_Rows()
  Dim sh1 As Worksheet
  Dim i As Long, j As Long, lr As Long
  
  Set sh1 = Sheets("Sheet1")    'Data
  For i = 2 To sh1.Range("A" & Rows.Count).End(3).Row
    Sheets.Add(, Sheets(Sheets.Count)).Name = sh1.Range("A" & i).Value
    Range("A1").Value = sh1.Range("A" & i).Value
    For j = 2 To sh1.Cells(i, Columns.Count).End(1).Column Step 3
      lr = Range("A" & Rows.Count).End(3).Row + IIf(j = 2, 1, 2)
      Range("A" & lr).Resize(3, 1).Value = Application.Transpose(sh1.Cells(1, j).Resize(1, 3).Value)
      Range("B" & lr).Resize(3, 1).Value = Application.Transpose(sh1.Cells(i, j).Resize(1, 3))
    Next
  Next
End Sub
 
  • Like
Reactions: eda
Solution

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

eda

New Member
Joined
Jul 23, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi DanteAmor, that works perfect! Thank you so much for all your solutions and help.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,603
Office Version
  1. 2007
Platform
  1. Windows
Im glad to help you, thanks for the feedback.
 

Forum statistics

Threads
1,141,480
Messages
5,706,633
Members
421,460
Latest member
Taamrak

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
Top