简体   繁体   中英

vba iterate over columns with formulas and filldown

I have a datasheet that is imported regularly and the length changes.

So, I want to write some VBA code that deletes any extra rows and autofills any formulas to the last row.

I have the first part done. This code finds the last row, and deletes anything below it.

Sub CleanData()
    Dim lastrow As Long
    Sheets("Open Operations").Select
    Range(Cells(Rows.Count, 1).End(xlUp).Offset(1), _
      Cells(Rows.Count, 1)).EntireRow.Delete
    lastrow = ActiveSheet.UsedRange.Rows.Count

End Sub

The part I'm stuck on is that I'm not sure how to autofill any columns that need it. These columns could be changing, so I want my code to be able to handle this. So, I want to iterate over all of the cells in the first row, from the very first column to the last column in use. Then, if that cell is a formula, I want to fill the formula down to the lastrow, as defined in the first code block.

Here's what I have so far:

Dim lastcolumn As Long

lastcolumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column

For Each c In ActiveSheet.Range("A1:A" & lastcolumn).Cells

    If c.HasFormula = True Then

But I'm pretty new to VBA, and I'm not sure how to make the column fill down to the previously defined Last Row.

EDIT: To clarify -- I want to iterate over every first cell in each column until the last column. Then, if that cell contains a formula, I want to autofill/filldown that whole column to the lastrow defined in the first code block.

Thanks.

I've managed to create code that accomplishes the task mentioned above:

Here is the code for anyone interested:

Sub CleanData()

' Clean the data
    Application.Calculation = xlManual

    Call CleanSheet("Order Headers")
    Call CleanSheet("Open Operations")
    Call CleanSheet("Confirmations (SAP)")
    Call CleanSheet("VA05")
    Call CleanSheet("ZOOP")
    Call CleanSheet("PremExped")

    Application.Calculation = xlCalculationAutomatic


End Sub

Sub CleanSheet(SheetName As String)

' Cleans the Sheet specified by SheetName

    ' Variable Declaration
    Dim NumColumns As Long
    Dim NumRows As Long
    Dim ColumnCounter As Long

    ' Find the number of rows and columns in SheetName
    NumRows = Sheets(SheetName).Cells(Rows.Count, 1).End(xlUp).Row
    NumColumns = Sheets(SheetName.Cells(1, Columns.Count).End(xlToLeft).Column

    ' Define the ranges for pulling down the formulas
    Set rng1 = Worksheets(SheetName).Range("A2:A" & NumRows)
    Set rng2 = Worksheets(SheetName).Range("A2")

    ' Delete extraneous rows of data
    Sheets(SheetName).Select
    Range(Cells(Rows.Count, 1).End(xlUp).Offset(1), _
      Cells(Rows.Count, 1)).EntireRow.Delete

    ' Ensure all formulas are dragged down appropriately
    For ColumnCounter = 0 To NumColumns
        If rng2.Offset(0, ColumnCounter).HasFormula = True Then
        rng1.Offset(0, ColumnCounter).FillDown
        End If
        Next ColumnCounter

End Sub

("A1:A" & lColumn) seems to confuse rows and columns. I think you want:

LastRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
or
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

EDIT: Maybe just the following:

For Each c In ActiveSheet.UsedRange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM