Consulting

Results 1 to 3 of 3

Thread: Macro works via step through but does not on button click

  1. #1
    VBAX Newbie
    Joined
    May 2020
    Posts
    1
    Location

    Macro works via step through but does not on button click

    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"


  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    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
    Semper in excretia sumus; solum profundum variat.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    None of the "If IsEmpty" lines refer to any sheet.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •