Solved

Autofill in Excel with VBA repeatedly in same column

Posted on 2011-03-14
4
629 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:dzielinski
  • 3
4 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 35133803
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
0
 
LVL 39

Accepted Solution

by:
nutsch earned 250 total points
ID: 35133812
Little cleaner update

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

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

Application.ScreenUpdating = True

End Sub

Open in new window

0
 

Author Closing Comment

by:dzielinski
ID: 35134088
Clean and quick. You are awesome, nutsch. Thanks!
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35134115
Glad to help. Thanks for your appreciation.

Thomas
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel - Row Height +1 VBA 2 24
VBA in SharePoint 3 18
onOpen 14 42
Need custom function to combine cells in Excel 2010 3 16
A short article about a problem I had getting the GPS LocationListener working.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now