我有一个包含以下数据的工作表:
A B C D E
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"
然后
A B C D E
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':应用程序定义的错误或对象定义的错误
答案 0 :(得分:0)
您错过单元格功能
中的行和列索引wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC
答案 1 :(得分:0)
没有更新值,因为虽然声明了要比较的工作簿未被使用,因为此行中的Cells(I,1)
未被限定,因此该过程正在使用任何活动的工作表。
这是您的代码修改,请尝试让我知道结果...
我为RefEdit1
和RefEdit2
分配了一些值以进行测试
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
建议访问这些页面:
Excel Objects,If...Then...Else Statement,On Error Statement
Range Object (Excel),Variables & Constants,With Statement
请告诉我您可能遇到的有关所使用的代码和资源的任何问题。
答案 2 :(得分:0)
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