-2

As mentioned above, the code works just fine both in the developer and with the hot keys but when I use the button, it seems like the "go to last row + 1" doesn't work. When I select the button, the data keeps being copied and recopied into row 2.

Sub Entry_Schedule()
' Entry_Schedule Macro
    Dim FrstEmptCll As Range
    FrstEmtCll = Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Sheet2").Range("D4:I4").Copy
    Sheets("Data").Range("A" & FrstEmtCll + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   End Sub
10
  • Can you explain exactly what you mean by button? Is it a button on a userform, an ActiveX command button, a form control button? Then also, what exactly have to tried with the button itself? We probably need to see the code for the button rather than this. Nov 23, 2018 at 6:57
  • And "...it seems like the 'go to last row + 1' doesn't work" isn't listed anywhere in your provided code. Nov 23, 2018 at 6:59
  • Form Control. I don't have a code for the button, I just linked it to the code mentioned above. Sheets("Data").Range("A" & FrstEmtCll + 1).... is what I was referencing with the "go to last row + 1"
    – B-Rye
    Nov 23, 2018 at 6:59
  • 1
    So, I am assuming that this "button" is on another sheet, right? Because Range("A" & Rows.Count).End(xlUp).Row isn't qualified with a worksheet. You did good and qualified the next two range objects with sheets Sheet2 and Data, but didn't qualify the range you are having issues with. Nov 23, 2018 at 7:05
  • 1
    @Pᴇʜ I have retracted my flag, but can't do anything about the one who flagged before I did (No MCVE I believe it was). Nov 23, 2018 at 7:38

1 Answer 1

2

You need to qualify every Range, Rows, Columns, etc with a worksheet:

FrstEmtCll = Worksheets("Data").Range("A" & Worksheets("Data").Rows.Count).End(xlUp).Row

The second issue is that you declare Dim FrstEmptCll As Range but you try to put the row number into a range object .End(xlUp).Row.

So either Set the range to the cell and offset:

Option Explicit

Sub Entry_Schedule()
    ' Entry_Schedule Macro
    Dim FrstEmtCll As Range
    Set FrstEmtCll = Worksheets("Data").Range("A" & Worksheets("Data").Rows.Count).End(xlUp).Offset(RowOffset:=1)
    Worksheets("Sheet2").Range("D4:I4").Copy
    FrstEmtCll.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

Or use the row number (as Long):

Option Explicit

Sub Entry_Schedule()
    ' Entry_Schedule Macro
    Dim FrstEmptRow As Long
    FrstEmptRow = Worksheets("Data").Range("A" & Worksheets("Data").Rows.Count).End(xlUp).Row + 1
    Worksheets("Sheet2").Range("D4:I4").Copy
    Worksheets("Data").Range("A" & FrstEmptRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

Additionally you mistyped your variable name FrstEmptCll vs FrstEmtCll (missing p). This means you actually have 2 different variables now (which easily blows up your code). To avoid such errors I highly recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.

2
  • 1
    This is very good advice. Thank you. I'm new to VBA so I've got a lot of catching up to do.
    – B-Rye
    Nov 23, 2018 at 7:30
  • 1
    @B-Rye Especially if you are new to VBA Option Explicit forces you to write good code and prevents you from running into errors.
    – Pᴇʜ
    Nov 23, 2018 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.