将单元格中的值与命名范围匹配 - vba

matching values in cells to Named ranges - vba

我正在使用 VBA 来尝试查看一个工作簿中的单元格中的值是否与另一个工作簿中的命名范围匹配,如果它们匹配,则从这些命名范围中的另一列复制粘贴值。我知道他们会匹配。目的只是将值复制到指定的命名范围内。

问题出在这一行:

If rng = ws2.Range("NamedRange") Then

下面是我的代码:

Sub Button4_Click()

Dim strFileName As String
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Dim cell As Range
Dim rng As Range
Dim RangeName As String
Dim CellName As String


''Set wb2 = ActiveWorkbook
''Set ws2 = wb2.Sheet("Output")
''ws2.Range("D1:D12").Copy

''Set wb1 = ActiveWorkbook

strFileName = CreateObject("WScript.Shell").specialfolders("Desktop") & "\BAC GVP - Template_Update_121917.xlsm"

If Dir(strFileName) <> vbNullString Then
    Set wb1 = Workbooks.Open(strFileName)
Else
MsgBox "Sorry, the file does not exist on your Desktop at this time, please drop a copy to your Desktop from server!"
End If

''Set wb2 = ThisWorkbook
''Set ws2 = wb2.Sheets("Output")
''Set ws1 = wb1.Sheets("RVP Local GAAP")

''ws2.Range("D4:D12").Copy
''ws1.Range("G13:G21").PasteSpecial xlPasteValues

  ''RangeName = "myData"
  ''CellName = "G11:G83"

  ''Set cell = Worksheets("RVP Local GAAP").Range(CellName)
  ''ThisWorkbook.Names.Add Name:=RangeName, RefersTo:=cell

  ''RangeName = "NamedRange"
  ''CellName = "C4:C12"


Set wb2 = ThisWorkbook
Set ws2 = wb2.Sheets("Output")
Set ws1 = wb1.Sheets("RVP Local GAAP")

For Each rng In ws1.Range("CurrentTaxPerLocalGAAPProvision")
    If rng = ws2.Range("NamedRange") Then
    ws2.Range("ReportBalance").Copy
    ws1.Range("CurrentTaxPerLocalGAAPProvision").PasteSpecial xlPasteValues
    MsgBox "Values Copied Successfully"
End If
Next rng
MsgBox "Both Ranges do not have the same data"
End Sub

见下图 - 单元格 G29 称为 "GVP_Donations_CurrentTaxPerLocalGAAPProvision"... 因此对于此示例,我希望 $4,313 出现在单元格 G29

CurrentTaxPerLocalGAAPProvision:

范围("NameRange"):

你的台词

If rng = ws2.Range("NamedRange") Then

由于试图将 rng 的值(例如 ""rng 指的是单元格 G29)与值数组(值在 "NamedRange")。 VBA 无法处理标量与向量的比较。但这不是你想要做的。


我相信,要完成您正在尝试做的事情,您可以将循环替换为:

'Loop through all the values in NamedRange
For Each rng In ws2.Range("NamedRange")
    'Transfer the value from the next column to the appropriate range in the
    'destination sheet
    ws1.Range(rng.Value).Value = rng.Offset(0, 1).Value
Next
MsgBox "Values Copied Successfully"

如果只复制 "NamedRange" 中的 一些 值,那么您可能需要进行一些测试以查看范围是否正确目标区域:

Dim dstRng As Range
'Loop through all the values in NamedRange
For Each rng In ws2.Range("NamedRange")
    Set dstRng = Nothing
    On Error Resume Next
    Set dstRng = ws1.Range(rng.Value)
    On Error GoTo 0
    'Check that the range exists in destination sheet
    If Not dstRng Is Nothing Then
        'Check that the range exists in the appropriate area
        If Not Intersect(dstRng, ws1.Range("CurrentTaxPerLocalGAAPProvision")) Is Nothing Then
           'Transfer the value from the next column to the appropriate range in the
           'destination sheet
           dstRng.Value = rng.Offset(0, 1).Value
        End If
    End If
Next
MsgBox "Values Copied Successfully"