Nguyễn Hữu Quốc
Nguyễn Hữu Quốc

Reputation: 21

Print an excel file using Macro. But if the value in one row is 0 then you skip it

Sub printF()
    Dim i As Long, printFrom As Long, printTo As Long

    printFrom = Sheet3.Range("AF6").Value
    printTo = Sheet3.Range("AF7").Value

    For i = printFrom To printTo
        If (Sheet3.Range("AE20").Value > 0) 'This check
        Sheet3.Range("AF4").Value = i
        Sheet3.PrintOut Preview:=False
    Next i

End Sub

It works fine but if I try to input the checking value if crashes.

Upvotes: 2

Views: 78

Answers (2)

braX
braX

Reputation: 11755

If you are just asking how to use an If block....

For i = printFrom To printTo
    If Sheet3.Range("AE20").Value > 0 Then
      Sheet3.Range("AF4").Value = i
      Sheet3.PrintOut Preview:=False
    End If
Next i

Upvotes: 3

Storax
Storax

Reputation: 12167

You possibly have to add an error check with IsError before you do a comparision.

If Not IsError(Sheet3.Range("AE20")) Then
    If (Sheet3.Range("AE20").Value > 0) Then
        Sheet3.Range("AF4").Value = i
        Sheet3.PrintOut Preview:=False
    End If
End If

If you have an error value in the cell the comparision Sheet3.Range("AE20").Value > 0 will run into a run time error 13

Upvotes: 1

Related Questions