【问题标题】:Vba Formula R1C1 help needVba公式R1C1帮助需要
【发布时间】:2018-12-27 05:08:38
【问题描述】:

我想使用 r1c1 公式从当前工作簿 (cbook) 中查找 AF 列中的值,并将其与之前工作簿 (pbook) 中的值进行比较。我使用 r1c1 是因为它更快,但对其他方法持开放态度。

我想复制 AG、AH、AI 和 AJ 列中的值。我已经进行了研究以创建正确的代码,但是在范围和地址(Srng.Address)以及将工作表的名称添加到变量中时遇到了困难。

Srng 是前一个工作簿的路径和文件名,但 .Address 给出了变量所在的单元格。 (我有一个变量工作簿(InstVariable),单元格在 C28 中,但我需要字符串值)。我尝试了许多不成功的尝试来定义 Srang,它是具有先前工作簿的工作表名称的路径。工作表应该是当前工作簿的名称。

下面是我的部分代码。任何帮助表示赞赏!

 'Current List Template
    Dim cbook As Workbook
    Set cbook = ActiveWorkbook


    'Prior List Template
    Dim pbook As Workbook
    'Workbook
   ' Set pbook = Workbooks.Open(JRDCPriorNoBrk)

    cbook.Activate


    '********************
    'Turns off screen updates (no flashes)
    '********************
    With Application
        '.Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With




    'Make sure user is on JobReq & DataChg INST tab to run macro
    Sheets("JobReq & DataChg INST").Activate


    'unshare the workbook to run macro
    'ActiveWorkbook.SaveAs ActiveWorkbook.FullName, accessmode:=xlExclusive
    Application.DisplayAlerts = False
   ' ActiveWorkbook.ExclusiveAccess


For Each xworksheet In ActiveWorkbook.Worksheets

    xworksheet.Activate

If ActiveSheet.Name = "Original" Or ActiveSheet.Name = "JobReq & DataChg INST" Then GoTo NotThisSheet

    'unprotects sheets so user can run macro
    ActiveSheet.Unprotect


    'ActiveSheet.Range("AG2").Select

    Dim Srng As Range
    Dim LastRow As Long

    'Set Srng = Worksheets("Coniguration").Range("_Configuration")
    'Set Srng = ActiveSheet.Range("AF2:AJ18")

    'pbook.Worksheet (cbook.ActiveSheet.Name)
     'LastRowp = .Cells(.Rows.Count, "AF").End(xlUp).Row


     'Set Srng = pbook.Worksheet(ActiveSheet.Name)
     'Set Srng = PathJRDCPrior.ActiveSheet.Range("AF2:AJ" & LastRowp)





    With ActiveSheet


     'With pbook.Worksheets(cbook.ActiveSheet.Name)
    'With Worksheets(" & PathJRDCPrior & ")
        'current worksheet last row
        LastRow = .Cells(.Rows.Count, "AF").End(xlUp).Row

        'prior worksheet last row
        'MsgBox (cbook.ActiveSheet.Name)
        With pbook.Worksheets(cbook.ActiveSheet.Name)
        'pbook.Worksheets (cbook.ActiveSheet.Name)
        LastRowp = .Cells(.Rows.Count, "AF").End(xlUp).Row
        SSheet = cbook.ActiveSheet.Name
        Set SPath = PathJRDCPrior

        'Set Srng = PathJRDCPrior.ActiveSheet.Range("AF2:AJ" & LastRowp)
        'Srng = pbook.Worksheets(cbook.ActiveSheet.Name).Range("AF2:AJ" & LastRowp)
        Set Srng = Worksheets(SSheet).Range("AF2:AJ" & LastRowp)
        End With
       ' MsgBox (Srng)

        With cbook.ActiveSheet.Range("AG2:AG" & LastRow)
                   With cbook.ActiveSheet.Range("AG2:AG" & LastRow)
            **.FormulaR1C1 = "=VLOOKUP(RC32," & Srng.Address(, , xlR1C1, True) & ", 2, 0)"**


            .Value = .Value
        End With
        With cbook.ActiveSheet.Range("AH2:AH" & LastRow)
            .FormulaR1C1 = "=VLOOKUP(RC32," & Srng.Address(, , xlR1C1, True) & ", 3, 0)"
            .Value = .Value
        End With
        With cbook.ActiveSheet.Range("AI2:AI" & LastRow)
            .FormulaR1C1 = "=VLOOKUP(RC32," & Srng.Address(, , xlR1C1, True) & ", 4, 0)"
            .Value = .Value
        End With
        With cbook.ActiveSheet.Range("AJ2:AJ" & LastRow)
            .FormulaR1C1 = "=VLOOKUP(RC32," & Srng.Address(, , xlR1C1, True) & ", 5, 0)"
            .Value = .Value
        End With
           End With

NotThisSheet:
Next xworksheet

'share workbook
ActiveWorkbook.SaveAs ActiveWorkbook.FullName, accessmode:=xlShared
Application.DisplayAlerts = True

MsgBox ("Copying from the prior list is complete.")

End Sub

【问题讨论】:

  • 我使用 r1c1 是因为它更快更快?什么?
  • 真的需要使用Option Explicit 并正确声明所有变量 - 很难知道SSheet 是拼写错误还是变量。另外,尽量避免使用太多 cmets - cmets 应该提高可读性,而不是阻碍它。

标签: excel vba lookup


【解决方案1】:

您的代码很难遵循。忽略细节,似乎你想:

  • 遍历cbook 中的所有工作表(两个除外)。
  • 对于您循环访问的每个工作表,您希望在 cbookpbook 之间执行一些 VLOOKUP。
  • 并将 VLOOKUP 的结果硬编码为 cbook 中的静态值。

也许试试下面的,看看你能走多远。您需要更改代码中的文件路径以匹配您自己的路径。

很可能我的代码并没有完成你想要/你所做的一切。还建议您复制当前工作簿和以前的工作簿,并让代码在运行时引用这些副本。

Option Explicit

Sub LookUpRangeInPreviousWorkbook()

    Dim currentWorkbook As Workbook
    Set currentWorkbook = Workbooks.Open("C:\Users\user\misc.xlsb") ' If you're putting your VBA code into currentWorkbook and running it from there, then you may as well just use 'Thisworkbook'

    Dim previousWorkbook As Workbook
    Set previousWorkbook = Workbooks.Open("C:\Users\user\lol.xlsb") ' JRDCPriorNoBrk is not assigned anywhere in your code, hence it's not assigned anywhere in mine.

    Dim sheetsToSkip As Variant
    sheetsToSkip = Array("Original", "JobReq & DataChg INST")

    Dim currentSheet As Worksheet ' The worksheet from the current workbook that is being looped over. Make the variable name better if possible.

    For Each currentSheet In currentWorkbook.Worksheets
        If IsError(Application.Match(currentSheet.Name, sheetsToSkip, 0)) Then

            'unprotects sheets so user can run macro
            currentSheet.Unprotect

            With previousWorkbook.Worksheets(currentSheet.Name) ' This will throw an error if the previous workbook does not contain the current sheet in the current workbook
                Dim lastRowInPreviousWorkbook As Long
                lastRowInPreviousWorkbook = .Cells(.Rows.Count, "AF").End(xlUp).Row

                Dim rangeAddressFromPreviousWorkbook As String ' This doesn't change below, so just assign it once here.
                rangeAddressFromPreviousWorkbook = .Range("AF2:AJ" & lastRowInPreviousWorkbook).Address(RowAbsolute:=True, ColumnAbsolute:=True, ReferenceStyle:=xlA1, External:=True)
            End With

            With currentSheet
                Dim lastRowInCurrentWorkbook As Long
                lastRowInCurrentWorkbook = .Cells(.Rows.Count, "AF").End(xlUp).Row

                With .Range("AG2:AG" & lastRowInCurrentWorkbook)
                    .Formula = "=VLOOKUP(AF2," & rangeAddressFromPreviousWorkbook & ", 2, 0)"
                    .Value = .Value
                End With
                With .Range("AH2:AH" & lastRowInCurrentWorkbook)
                    .Formula = "=VLOOKUP(AF2," & rangeAddressFromPreviousWorkbook & ", 3, 0)"
                    .Value = .Value
                End With
                With .Range("AI2:AI" & lastRowInCurrentWorkbook)
                    .Formula = "=VLOOKUP(AF2," & rangeAddressFromPreviousWorkbook & ", 4, 0)"
                    .Value = .Value
                End With
                With .Range("AJ2:AJ" & lastRowInCurrentWorkbook)
                    .Formula = "=VLOOKUP(AF2," & rangeAddressFromPreviousWorkbook & ", 5, 0)"
                    .Value = .Value
                End With
            End With

            ' Do you need re-protect the sheet now? Loop proceeds to next sheet in a few lines.
        End If
    Next currentSheet

    ' You might need to save either/both files -- or do something of that nature here.

    MsgBox ("Copying from the prior list is complete.")

End Sub

【讨论】:

    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多