Link to home
Start Free TrialLog in
Avatar of dzielinski
dzielinski

asked on

Autofill in Excel with VBA repeatedly in same column

I have some Excel files with a few million records between them, and they are grouped by source filename. I would like to write a VBA macro that places the source filename of each record in the first column so they will be easy to trace when I combine them into a database.

I am assuming the best way to do this is to use autofill coding on some sort of loop but I'm having trouble with the autofill part. The files are laid out exactly like in the exampleData tab in the attached file and I would like it to look like the solution tab. Thanks!
exampleData.xlsx
Avatar of nutsch
nutsch
Flag of United States of America image

Try this code:

Sub quickcleanup()
Dim rg As Range
Set rg = Range(Cells(3, 2), Cells(ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1, 2))

Application.ScreenUpdating = False

With rg

    .AutoFilter field:=1, Criteria1:="=C:\*"
    .Offset(, -1).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=RC[1]"
    .AutoFilter

    .Offset(, -1).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    .Offset(, -1).Value = .Offset(, -1).Value


    .AutoFilter field:=1, Criteria1:="=C:\*"
    .SpecialCells(xlCellTypeVisible).Offset(, -1).ClearContents

End With

Set rg = Range(Cells(3, 2), Cells(ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1, 2))

With rg
    .AutoFilter field:=1, Criteria1:="=~***"
    .SpecialCells(xlCellTypeVisible).Offset(, -1).ClearContents
    
    .AutoFilter
End With

Application.ScreenUpdating = True

End Sub

Open in new window


Thomas
ASKER CERTIFIED SOLUTION
Avatar of nutsch
nutsch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dzielinski
dzielinski

ASKER

Clean and quick. You are awesome, nutsch. Thanks!
Glad to help. Thanks for your appreciation.

Thomas