我正在尝试将一些代码粘贴到一个匹配函数中的列“B”中,该列引用相邻列“A”中的相同行,使用另一个工作表中的“A”列中的列表作为查找阵列。这一切都发生的工作表是ws0,查找数组所在的工作表是ws2。
代码在“With”括号中的最后一行(跳到我的代码的底部)跳了起来:
Cells(i, 2).Value = "=MATCH(A" & i & "," & ws2.Name & "!$A$9:$A$" & lRow1 & ", 0)""",
给我一个
运行时错误1004.应用程序定义或对象定义 错误。
我无法弄清楚造成这种情况的原因。我有一种感觉,我的引号可能不正确,或者我的一个变量lRow1,fRow,ws2可能是一个问题,但我尽可能多地修改它们,但没有用。任何想法将不胜感激!注意,在抛出错误的那一行上方注释掉的那一行证明了公式的结果应该是什么样的。
Dim wb0 As Workbook 'this workbook (the log) '''''''''''''''''''''''
Dim wb1 As Workbook 'the opened T12 sheet
Dim rng0 As Range 'the range of cells in this workbook's 2nd sheet to be copied to
Dim rng1 As Range 'the range of cells from the opened workbook to be copied from
Dim rng2 As Range 'the range of the cells copied into ws0 from ws1
Dim fRow As Integer 'the first row of the range used in wb0, ws0
Dim lRow0 As Integer 'the last row of the range used in wb0, ws0
Dim lRow1 As Integer 'the last row of the range of accounts in ws2
Dim usedRng As Range 'the full range of cells used in wb0, ws0
Set ws0 = ActiveSheet
Set wb0 = ActiveWorkbook '''''''''''''''
Set rng0 = ws0.Range("A9:A159")
'Find the desired T12 workbook filepath
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
fileChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If fileChoice <> 0 Then
'get the file path selected by the user
filePath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
End If
'Set variables using the newly-opened workbook
Set wb1 = Workbooks.Open(filePath)
Set ws1 = ActiveSheet
Set rng1 = ws1.Range("B9:B159")
'copy wb1, ws1's into wb0, ws0
rng0.Value = rng1.Value
wb1.Close
'create variable to refer to the first tab of wb0
wb0.Worksheets("Sheet1").Activate
Set ws2 = ActiveSheet
With ws2
lRow1 = Cells(.Rows.Count, "A").End(xlUp).Row 'Last row
End With
'switch back to ws0
ws0.Activate
'identify the range used in ws0 of wb0
Set rng2 = ActiveSheet.UsedRange
fRow = rng2(1).Row 'First row
With ws0
lRow0 = Cells(.Rows.Count, "A").End(xlUp).Row 'Last row
End With
Dim i As Integer
For i = fRow To lRow0
With ws0
'Cells(i, 2).Value = "=MATCH(A9,Sheet2!A9:A100,0)"
Cells(i, 2).Value = "=MATCH(A" & i & "," & ws2.Name & "!$A$9:$A$" & lRow1 & ", 0)"""
End With
Next i
End Sub
我在Cells.Formula中得到了同样的错误......并且使用Range.Formula ...我得到了一个不同的运行时错误1004:“对象的方法'范围'_全局失败。”任何建议,将不胜感激!我把宏的大部分包括在内作为参考......尽管相关部分位于底部。提前谢谢!
答案 0 :(得分:0)
我认为这是你的)"""
,你只需要关闭一次