简体   繁体   中英

Fill Down Formula based on data in another sheet

I want to build a macro to fill down formulas.Data is being copied from a website and pasted on Sheet1 separately. Sheet2 contains formulas to clean up and arrange the data including eliminating rows based on a criteria. The problem is that if I fill down the formulas prior to eliminating the rows, I get a #REF error. I want to include the fill down in the macro because I already have one to delete the rows. I do not want to substitute character 160 for an actual space. My macro is placed below.

Code:

Sub chng()

    Dim x As Long, lastrow As Long
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    For x = lastrow To 1 Step -1
        If Cells(x, 2).Value = "SA" Or Cells(x, 2).Value = "EVC" Then
            Rows(x).Delete
        End If
    Next x


    Application.ScreenUpdating = False


    Columns("A:A").Select


    Selection.Replace What:=" ", Replacement:=" ", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    Application.ScreenUpdating = True


    Range("A1").Select

End Sub

In other words what I want to accomplish is once I have finished pasting the info, run the macro, delete the rows, change character 160 for an actual space then count the rows (not including headers) on Sheet1 and fill down on sheet2 based on that eg if I have 140 rows of data I want to fill down from row 2 until 141.

You could capture the number of rows and decrement it for each row you delete, then use that range:

Dim x As Long, lastrow As Long, totalRows As Long
totalRows = lastrow

    Rows(x).Delete
    totalRows = totalRows - 1

Then you can copy down your formulas from row 2 to totalRows, in whatever column it is.

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