0

I have two workbooks. I need to apply an if else condition on the 2nd workbook and the output of if else should be saved in the cell of 1st workbook using VBA. my code looks like :

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
Community
  • 1
  • 1
fguy
  • 1

1 Answers1

1

Open both Workbooks. Edit sub code:

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

If you receive this error: Subscript out of range (Error 9), it means that either one of the names you provided is not right or that one of the workbooks isn't opened. So check for spelling, spaces etc. and make sure both workbooks are opened.

Moritz Schmitz v. Hülst
  • 3,229
  • 4
  • 36
  • 63