在我的输出范围上得到错误1004。 需要正确指定它

我有一个工作表,其中包含以下数据:

ABCDE SF15-100 MFG1 JOB1 TOTALMFG TOTALWC SF15-101 MFG2 JOB1 SF15-102 MFG3 JOB1 

我试图编写一个循环来通过列A,并确定该值是否在一个特定的范围内的不同的工作簿相同。如果它相同,那么它需要粘贴到右边的值在列D和E.

即如果

 INWBK.Sheets("QTR").Range("H7").Value = "SF15-101" 

然后

  ABCDE SF15-100 MFG1 JOB1 TOTALMFG TOTALWC SF15-101 MFG2 JOB1 TOTALFOB TOTALWC SF15-102 MFG3 JOB1 

这是我迄今为止所尝试的:

 Private Sub OKBTN_Click() Dim TOTALFOB As String Dim TOTALWC As String Dim wbk As Workbook Dim INWBK As Excel.Workbook Dim TOTMFG As Variant Dim TOTWC As Variant Dim QTR_NUM As String Dim ILast As Long Dim i As Long TOTALFOB = RefEdit1 TOTALWC = RefEdit2 Set INWBK = ActiveWorkbook Set wbk = Workbooks.Open("C:\QUOTE REQUEST LOG 2015.xlsm") QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value ILast = wbk.Sheets("QTR_LOG").Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To ILast If Cells(i, 1).Value = QTR_NUM Then wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC Else End If Next i ThisWorkbook.Save: ThisWorkbook.Saved = True Unload Me ActiveWorkbook.Close End Sub 

我遇到以下错误:

  wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC 

运行时错误“1004”:应用程序定义或对象定义的错误

没有值被更新为要比较的工作簿,虽然声明没有被使用,但是这行中的Cells(I,1)没有被限定,所以过程使用任何工作表被激活。

这是你的代码修改,请尝试让我知道结果…我分配了一些值RefEdit1RefEdit2进行testing

 Private Sub OKBTN_Click() Const kPath As String = "C:\" Const kFile As String = "QUOTE REQUEST LOG 2015.xlsm" Dim TOTALFOB As double Dim TOTALWC As double Dim Wbk As Workbook Dim INWBK As Workbook 'Dim TOTMFG As Variant ' Not Used 'Dim TOTWC As Variant ' Not Used Dim QTR_NUM As String Dim ILast As Long Dim i As Long Dim RefEdit1, RefEdit2 'Not declared before 'Values Assigned for testing TOTALFOB = 450 TOTALWC = 500 ' TOTALFOB = RefEdit1 ' TOTALWC = RefEdit2 Set INWBK = ThisWorkbook Rem Set Wbk in case it's open On Error Resume Next Set Wbk = Workbooks(kFile) On Error GoTo 0 Rem Validate Wbk If Wbk Is Nothing Then Set Wbk = Workbooks.Open(kPath & kFile) QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value With Wbk.Sheets("QTR_LOG") ILast = .Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To ILast If .Cells(i, 1).Value = QTR_NUM Then .Cells(i, 4) = TOTALFOB .Cells(i, 5) = TOTALWC End If: Next: End With INWBK.Save: INWBK.Saved = True 'Unload Me Wbk.Close SaveChanges:=True End Sub 

build议访问这些页面:

Excel对象 , 如果…然后…其他语句 , 在错误声明

范围对象(Excel) , variables和常量 , 带语句

请让我知道您可能对使用的代码和资源有任何疑问。

您错过了Cells函数中的行和列索引

 wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC 
 Const kPath As String = "C:\" Const kFile As String = "QUOTE REQUEST LOG 2015.xlsm" Dim TOTALFOB As Variant Dim TOTALWC As String Dim Wbk As Workbook Dim INWBK As Workbook Dim QTR_NUM As String Dim ILast As Long Dim i As Long Dim TOTMFG As Variant Dim TOTWC As Variant Dim LR As Long Set INWBK = ThisWorkbook With Sheets("QTR") LR = .Range("I" & Rows.Count).End(xlUp).Row TOTALFOB = WorksheetFunction.Sum(.Range("I23:I" & LR)) End With 'Values Assigned for testing ' TOTALFOB = 450 ' TOTALWC = 500 TOTALWC = TOTALFOB + INWBK.Sheets("QTR").Range("D18").Value QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value Rem Set Wbk in case it's open On Error Resume Next Set Wbk = Workbooks(kFile) On Error GoTo 0 Rem Validate Wbk If Wbk Is Nothing Then Set Wbk = Workbooks.Open(kPath & kFile) With Wbk.Sheets("QTR_LOG") ILast = .Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To ILast If .Cells(i, 1).Value = QTR_NUM Then .Cells(i, 6) = TOTALFOB .Cells(i, 7) = TOTALWC End If: Next: End With 'INWBK.Save: INWBK.Saved = True 'Unload Me 'Wbk.Close SaveChanges:=True End If End Sub