PDA

View Full Version : Macro works via step through but does not on button click



Arsen258
05-13-2020, 05:41 AM
Hello,

i have created the macro below, what it does, it copies data from 2 Sheets into one to compile it and then copies it into the 3rd one called database. The problem is, if I manually click through every step it does everything correctly but if i use the button, only the first "IF" works, the rest skips to "Else" for some reason even though it should not. Do you see any reason for that?


Sub Save_To_DB()

Sheets("Import").Range("A10:E13").Copy
With Sheets("Temp").Range("G" & Rows.Count).End(xlUp).Offset(1)
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With

If IsEmpty(Range("G2")) = False Then
Sheets("Import").Range("A5:E5").Copy
Sheets("Temp").Range("B2").PasteSpecial xlPasteValuesAndNumberFormats
Sheets("Import").Range("C2").Copy
Sheets("Temp").Range("A2").PasteSpecial xlPasteValuesAndNumberFormats
Else
End If
If IsEmpty(Range("G3")) = False Then
Sheets("Import").Range("A5:E5").Copy
Sheets("Temp").Range("B3").PasteSpecial xlPasteValuesAndNumberFormats
Sheets("Import").Range("C2").Copy
Sheets("Temp").Range("A3").PasteSpecial xlPasteValuesAndNumberFormats
Else
End If
If IsEmpty(Range("G4")) = False Then
Sheets("Import").Range("A5:E5").Copy
Sheets("Temp").Range("B4").PasteSpecial xlPasteValuesAndNumberFormats
Sheets("Import").Range("C2").Copy
Sheets("Temp").Range("A4").PasteSpecial xlPasteValuesAndNumberFormats
Else
End If
If IsEmpty(Range("G5")) = False Then
Sheets("Import").Range("A5:E5").Copy
Sheets("Temp").Range("B5").PasteSpecial xlPasteValuesAndNumberFormats
Sheets("Import").Range("C2").Copy
Sheets("Temp").Range("A5").PasteSpecial xlPasteValuesAndNumberFormats
Else
End If

Sheets("Temp").Range("A2:K5").Copy
With Sheets("Order_DB").Range("A" & Rows.Count).End(xlUp).Offset(1)
.PasteSpecial Paste:=xlPasteColumnWidths
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
End With

MsgBox "Data saved to the database"

paulked
05-13-2020, 05:57 AM
Hi and welcome to the forum.

Have you tried putting a sheet reference before the range? eg



If IsEmpty(Sheets("WHATEVER").Range("G2")) = False Then

SamT
05-13-2020, 05:59 AM
None of the "If IsEmpty" lines refer to any sheet.