excelvba

VBA Filldown to Blank Cell, Then skip 4 rows, and Filldown again


I am running a report but it breaks up the information poorly when export to excel. Column B cell 1 has the part number I need to autofill down. Which is fine no biggie. But then it goes to NEXT part number after subtotal row, and then a blank row. I need to autofill down, skip subtotal row, skip blank row, skip next row cuz thats the part number and if i filldown i need to be one under it. and the number of lines always changes so i cant just set a nth row skip. Any suggestions??

Heres the code i have that i have to run over an dover and over for 40 pages on my report every time i run it....

Sub filldown()
For Each cell In Selection
    If Not IsEmpty(ActiveCell.Value) Then
        cell.filldown
    End If
Next
End Sub

enter image description here


Solution

  • The script has been tested using the sample data from the original post. If it doesn't work with your actual data, please share more details.

    Sub Demo()
        Dim c As Range, cellStart As Range, cellEnd As Range
        ' Starting cell
        Set c = Range("A1")
        ' Loop through rows until the end of the worksheet
        Do While c.Row < c.Worksheet.Rows.Count
            ' Mark the start of the range
            Set cellStart = c
            ' Move to the last non-empty cell
            Set c = c.End(xlDown)
            If Len(c) > 0 Then
                ' Check if the cell contains "Sub Total:"
                If c = "Sub Total:" Then
                    ' Set the end of the range to the cell above "Sub Total:"
                    Set cellEnd = c.Offset(-1)
                Else
                    ' Otherwise, set the end of the range to the current cell
                    Set cellEnd = c
                End If
                ' Fill down the adjacent column for the range
                Range(cellStart, cellEnd).Offset(, 1).FillDown
            End If
            ' Move to the next non-empty cell
            Set c = c.End(xlDown)
        Loop
    End Sub
    

    enter image description here