Copy values from right until non blank cell without impacting headers

YasSheikh

New Member
Joined
Dec 19, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hello Wizards!

Really hoping someone can help. Apologies if I break any forum rules. I am fairly new to VBA and have searched many threads but couldn't find anything for my problem.

Unable to post an example here but hopefully the below illustration helps. I am trying to copy data from the right until I hit the next header without impacting position of data based on headers. The problem is that the headers appear multiple times in the worksheet.

First header appears in Row3

Current state:

Header1Header2Header3Header4Header5
datadatadata
datadatadata
datadatadata
Header1Header2Header3Header4Header5
datadatadatadata
datadata
datadatadatadata
Header1Header2Header3Header4Header5
datadata
datadatadata
datadatadata

Desired state:
Header1Header2Header3Header4Header5
datadatadata
datadatadata
datadatadata
Header1Header2Header3Header4Header5
datadatadatadata
datadata
datadatadatadata
Header1Header2Header3Header4Header5
datadata
datadatadata
datadatadata

I would be grateful if anyone is able to help!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
With:

Book1
ABCDEFGH
1Header1Header2Header3Header4Header5
2datadatadata
3datadatadata
4datadatadata
5Header1Header2Header3Header4Header5
6datadatadatadata
7datadata
8datadatadatadata
9Header1Header2Header3Header4Header5
10datadata
11datadatadata
12datadatadata
13
Sheet5


How about:

VBA Code:
Sub AlignHeaders()
'
    Dim LastColumnInRow As Long, LastRowInSheet As Long
    Dim ColumnCounter   As Long, RowCounter     As Long
'
    LastRowInSheet = Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row                                          ' Get last Row Number in sheet
    LastColumnInRow = Cells(1, Columns.Count).End(xlToLeft).Column                                                      ' Get last column nummber in row
'
    For RowCounter = 1 To LastRowInSheet Step 4                                                                         ' Loop through every 4th row
        For ColumnCounter = LastColumnInRow To 1 Step -1                                                                '   Loop backwards through column numbers
            If Cells(RowCounter, ColumnCounter) = vbNullString Then                                                     '       If cell is blank then ...
                Range(Cells(RowCounter, ColumnCounter), Cells(RowCounter + 3, ColumnCounter)).Delete Shift:=xlToLeft    '           Delete 4 cells in column
            End If
        Next                                                                                                            '   Loop back
    Next                                                                                                                ' Loop back
End Sub
 
Upvote 0
Hi JohnnyL

Thank you so much for your reply. Apologies, I should have been clearer. The length below each header isn't always 4 rows. These vary however there is always a blank line and then the next row of headers. Hopefully the below explains better:

Current state:

Header1Header2Header3Header4Header5
datadatadata
datadatadata
datadata
datadatadata
datadata
Header1Header2Header3Header4Header5
datadatadatadata
datadata
datadatadatadata
data
data
datadata
Header1Header2Header3Header4Header5
data
datadatadata
datadatadata

Desired state:
Header1Header2Header3Header4Header5
datadatadata
datadatadata
datadata
datadatadata
data
Header1Header2Header3Header4Header5
datadatadatadata
datadata
datadatadatadata
data
data
datadata
Header1Header2Header3Header4Header5
datadata
datadatadata
datadatadata
Apologies again and thanks in advance!
 
Upvote 0
Hi JohnnyL

Thank you so much for your reply. Apologies, I should have been clearer. The length below each header isn't always 4 rows. These vary however there is always a blank line and then the next row of headers.

That is going to be more difficult. Not impossible, just more difficult.
 
Upvote 0
With:

ManyTests.xlsm
ABCDEFG
1Header1Header2Header3Header4Header5
2datadatadata
3datadatadata
4datadata
5datadatadata
6datadata
7
8Header1Header2Header3Header4Header5
9datadatadatadata
10datadata
11datadatadatadata
12data
13datadata
14
15Header1Header2Header3Header4Header5
16data
17datadatadata
18datadatadata
Sheet5


How about:

VBA Code:
Sub FindRowNumbersOfFirstBlankRowsAfterEachRangeOfDataInSheet()
'
    Dim BlankRowNumber                  As Long
    Dim ColumnCounter                   As Long, RowCounter     As Long
    Dim ColumnsWithValuesInRow          As Long
    Dim LastColumnNumberUsedInSheet     As Long, LastRowNumberUsedInSheet   As Long
    Dim StartRow                        As Long
    Dim FirstBlankRowInEachRangeList    As Object
    Dim FoundFirstRowInRange            As String
'
'--------------------------------------------------------------------------------------------------
'   Find Blank Rows in data range
'--------------------------------------------------------------------------------------------------
'
    Set FirstBlankRowInEachRangeList = CreateObject("System.Collections.ArrayList")         ' Establish 1D zero based arraylist called FirstBlankRowInEachRangeList
'
    LastRowNumberUsedInSheet = Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row            ' Get LastRowNumberUsedInSheet
    LastColumnNumberUsedInSheet = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column   ' Get LastColumnNumberUsedInSheet
'
    FoundFirstRowInRange = "No"                                                                     ' Initialize FoundFirstRowInRange flag
'
    For BlankRowNumber = 1 To LastRowNumberUsedInSheet + 1                                          ' Loop to loop through each row in sheet
        ColumnsWithValuesInRow = WorksheetFunction.CountA(Range(Cells(BlankRowNumber, 1), _
            Cells(BlankRowNumber, LastColumnNumberUsedInSheet)))                                    '   Check each row for any values in the row
'
        If ColumnsWithValuesInRow > 0 And FoundFirstRowInRange = "No" Then                          '   If a value is found in the row & Not FoundFirstRowInRange flag
            FoundFirstRowInRange = "Yes"                                                            '       Set FoundFirstRowInRange flag = "Yes"
'
        ElseIf ColumnsWithValuesInRow = 0 And FoundFirstRowInRange = "Yes" Then                     '   Else if no values found in row & FoundFirstRowInRange = "Yes"
            FoundFirstRowInRange = "No"                                                             '       Set FoundFirstRowInRange flag = "No"
'
            FirstBlankRowInEachRangeList.Add BlankRowNumber                                         '       Append BlankRowNumber to FirstBlankRowInEachRangeList
        End If
    Next                                                                                            ' Loop back
'
'--------------------------------------------------------------------------------------------------
'   Delete cells in columns
'--------------------------------------------------------------------------------------------------
'
    StartRow = 1                                                                                    ' Initialize StartRow
'
    For RowCounter = 0 To FirstBlankRowInEachRangeList.Count - 1                                    ' Loop through list of Blank Rows
        For ColumnCounter = LastColumnNumberUsedInSheet To 1 Step -1                                '   Loop backwards through column numbers
            If Cells(StartRow, ColumnCounter) = vbNullString Then                                   '       If cell is blank then ...
                Range(Cells(StartRow, ColumnCounter), _
                  Cells((FirstBlankRowInEachRangeList(RowCounter) - 1), ColumnCounter)).Delete Shift:=xlToLeft  '           Delete cells in column
            End If
        Next                                                                                        '   Loop back
'
        StartRow = StartRow + (FirstBlankRowInEachRangeList(RowCounter) - StartRow) + 1             '   Increase StartRow
    Next                                                                                            ' Loop back
End Sub

This assumes your Row 1 is the starting row. If it isn't, adjust the StartRow = 1 line of code.
 
Last edited:
Upvote 0
Solution
Hi JohnnyL - you sir are an absolute lifesaver. Works like a charm. Cannot thank you enough for all your help!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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