简体   繁体   English

VBA:如何将公式拖到最后使用的行?

[英]VBA: How to drag formulaS to the last used row?

Good day, please help me. 美好的一天,请帮助我。 The scenario is like this, I have 7 different formulas assigned to 7 different top rows after the header. 场景是这样的,我将7个不同的公式分配给标题后的7个不同的顶部行。 What I want to achieved is to drag this formulas down to the last used rows simultaneously. 我要实现的是同时将此公式向下拖动到最后使用的行。 I successfully wrote a code on doing this but it is a static code, I want to do it in a dynamical way because every month the amount of data is different so my static code is not reliable. 我成功地编写了一个执行此操作的代码,但这是一个静态代码,我想以一种动态的方式进行操作,因为每个月的数据量都不同,所以我的静态代码不可靠。

Here is the code that I have wrote: 这是我编写的代码:

'format border '格式边框

 ActiveSheet.Range("BK1", "BQ22").Select

borderMeFn BorderMeFn

                Dim strFormulas_OR1_ASR_DATA_DETAILS(1 To 7) As Variant
                    strFormulas_OR1_ASR_DATA_DETAILS(1) = "=COUNTIF(LSR_WISOR_USERS_" & month & ".xlsx!$C:$C,J2)" 'WISOR_COUNT - counts the PON_VER in LSR_WISOR_USER file.
                    strFormulas_OR1_ASR_DATA_DETAILS(2) = "=IFERROR(VLOOKUP(J2,LSR_WISOR_USERS_" & month & ".xlsx!$C:$E,3,FALSE),"""")"
                    strFormulas_OR1_ASR_DATA_DETAILS(3) = "=IFERROR(VLOOKUP(H2,CPXLIST_" & month & ".xlsx!$A:$B,2,FALSE),"""")"
                    strFormulas_OR1_ASR_DATA_DETAILS(4) = "=IFERROR(VLOOKUP(H2, DDVRFY_" & month & ".xlsx!$A:$B,2,FALSE),"""")"
                    strFormulas_OR1_ASR_DATA_DETAILS(5) = "=IFERROR(VLOOKUP(H2,HTG_" & month & ".xlsx!$A:$D,4,FALSE),"""")"
                    strFormulas_OR1_ASR_DATA_DETAILS(6) = "=IFERROR(VLOOKUP(H2,RPON_" & month & ".xlsx!$A:$B,2,FALSE),"""")"
                    strFormulas_OR1_ASR_DATA_DETAILS(7) = "=IFERROR(VLOOKUP(H2,PROV_PLAN_" & month & ".xlsx!$A:$F,6,FALSE),"""")"
                'apply formulas to designated cells
                With ActiveWorkbook.Sheets("Sheet1")
                    .Range("BK2:BQ2").formula = strFormulas_OR1_ASR_DATA_DETAILS
                End With
                Worksheets("Sheet1").Range("BK2:BQ22").FillDown
                    'Range("BK2:BQ" & LastRow).FillDown

Thank you in advance. 先感谢您。

Try the code below, explanations inside the code's comments: 尝试以下代码,并在代码注释中进行解释:

Dim LastRow As Long, LastCell As Range

'apply formulas to designated cells
With ThisWorkbook.Worksheets("Sheet1")
    .Range("BK2:BQ2").Formula = strFormulas_OR1_ASR_DATA_DETAILS

    ' use Find function to get last row 
    Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
    If Not LastCell Is Nothing Then
        LastRow = LastCell.Row
    Else
        MsgBox "Error!", vbCritical
    End If

    .Range("BK2:BQ" & LastRow).FillDown
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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