Search code examples
vbabasic

Copy value into rows between two blank rows


Please see image below. What I would like to do is copy the value in cell C3 into column B into rows 4-13. Then copy vehicle number in cell C16 into B17 and so on. Basically this displays all the trips a vehicle has made and the data for different vehicles is separated by blank rows.

Please help.

Check image below:

enter image description here


Solution

  • given your data structure you could try this:

    Option Explicit
    
    Sub main()
        Dim vehicleRng As Range, cell As Range
    
        With Range("A2", Cells(Rows.count, 1).End(xlUp))
            .AutoFilter field:=1, Criteria1:="VEHICLE"
            Set vehicleRng = .Resize(.Rows.count - 1).Offset(1).SpecialCells(xlCellTypeVisible)
        End With
        ActiveSheet.AutoFilterMode = False
    
        For Each cell In vehicleRng
            With cell
                Range(cell.Offset(1), cell.End(xlDown).Offset(-1)).Offset(, 1).Value = cell.Offset(, 2)
            End With
        Next
    End Sub