我有两本工作簿。我需要在第二个工作簿上应用if else条件,if else的输出应该使用VBA保存在第一个工作簿的单元格中。我的代码看起来像:
Sub plan()
Dim i As Integer, wb As Workbook
For i = 4 To 100
ActivateWB ("Bhandup Plan 11.xls")
If Workbooks("Bhandup Plan 11.xls").Sheets("Sheet1").Cells(i, 11).Value > 0 _
Or Workbooks("Bhandup Plan 11.xls").Sheets("Sheet1").Cells(i, 12).Value > 0 Then
Workbooks("premium solver.xls").Sheets("AHMD").Cells(i, 1).Value = _
Workbooks("Bhandup Plan 11.xls").Sheets("Sheet1").Cells(i, 2).Value
End If
Next i
End Sub
答案 0 :(得分:1)
同时打开Workbooks
。编辑子代码:
Sub plan()
Dim i As Integer
For i = 4 To 100
If Workbooks("Bhandup Plan 11.xls").Sheets("Sheet1").Cells(i, 11).Value > 0 _
Or Workbooks("Bhandup Plan 11.xls").Sheets("Sheet1").Cells(i, 12).Value > 0 Then
Workbooks("premium solver.xls").Sheets("AHMD").Cells(i, 1).Value = _
Workbooks("Bhandup Plan 11.xls").Sheets("Sheet1").Cells(i, 2).Value
End If
Next i
End Sub
如果收到此错误:下标超出范围(错误9),则表示您提供的其中一个名称不对,或者未打开其中一个工作簿。因此,请检查拼写,空格等,并确保打开两个工作簿。