[英]Error '1004' Method 'Range' of object '_Worksheet' failed
我正在嘗試為變量Arg1分配范圍。
dim wkb2 as workbook
set wkb2 = workbooks("File2.xlsx")
dim ws2 as worksheet
set ws2 = wkb2.worksheets(k)
Dim Arg1 As Range 'the range to sum
Dim Arg2 As Range 'criteria1 range`enter code here`
Dim Arg3 As Variant 'criteria1
Dim Arg4 As Range 'criteria2 range
Dim Arg5 As Variant 'criteria2
Set Arg1 = ws2.Range(Cells(i + 2, 6), Cells(i + 2, 6).End(xlDown).Offset(-1,
0))
Set Arg2 = ws2.Range(Cells(i + 2, 4), Cells(i + 2, 4).End(xlDown))
Set Arg4 = ws2.Range(Cells(i + 2, 5), Cells(i + 2, 5).End(xlDown))
Arg3 = ws1.Cells(j, 20)
Arg5 = ws1.Cells(j, 21)
ws1.Cells(j, 23).Value = Application.WorksheetFunction.SumIfs(Arg1, Arg2,
Arg3, Arg4, Arg5)
當我設置Arg1 = ...時出現錯誤(標題中)
我想做的是為Arg1分配一個Cells(i + 2,6):Cells(i + 2,“使用Ctrl Down后的單元格高1”)的范圍。 因此,例如,如果i + 2 = 5,則我想使用Range。(“ F5:F9”),假設最后一行是Row10。
任何幫助是極大的贊賞! (:
如果將對ws1的引用調整為所使用的工作表,則應編譯以下內容
另外,將變量放在一起。
Sub test()
Dim wkb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim Arg1 As Range 'the range to sum
Dim Arg2 As Range 'criteria1 range`enter code here`
Dim Arg3 As Variant 'criteria1
Dim Arg4 As Range 'criteria2 range
Dim Arg5 As Variant 'criteria2
Dim i As Long
Dim k As Long 'Or if it is a worksheet name then as string
Dim j As Long
Set wkb2 = Workbooks("File2.xlsx")
'k = ''Set K variable here
Set ws2 = wkb2.Worksheets(k)
Set ws1 = ThisWorkbook.Worksheets("Sheet1") ' Change as appropriate
Set Arg1 = ws2.Range(Cells(i + 2, 6), Cells(i + 2, 6).End(xlDown).Offset(-1, 0))
Set Arg2 = ws2.Range(Cells(i + 2, 4), Cells(i + 2, 4).End(xlDown))
Set Arg4 = ws2.Range(Cells(i + 2, 5), Cells(i + 2, 5).End(xlDown))
Arg3 = ws1.Cells(j, 20)
Arg5 = ws1.Cells(j, 21)
ws1.Cells(j, 23).Value = Application.WorksheetFunction.SumIfs(Arg1, Arg2, Arg3, Arg4, Arg5)
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.