1

I tried adapting this post: Error in finding last used cell in VBA to my needs but couldn't quite get it to work.

I'm pasting data into a new worksheet and then want to select the first empty row after data. Currently what's happening is the data is pasted and then the very first row in the sheet is selected. See code below. Any thoughts?

'runs when user enters data
If Target.Cells.Count = 1 And _
   Not Application.Intersect(Target, [I3:I10000]) Is Nothing Then

    Application.EnableEvents = False
    'User inputs type of event
    Archive = InputBox("Was this event a Win, Loss, or Close? (Please input Win/Loss/Close)")
With Target

    If Archive = "Win" Then
    'all data to transfer is selected and cut
            .EntireRow.Select
            Selection.Cut
    'the receiving sheet is selected and data is pasted to the selected cell
        Sheets("Win").Select
            ActiveSheet.Paste
    'the selection on the sheet the data was cut from is deleted
        Sheets("Begin").Select
            Selection.Delete
    'this is the issue I'm having - I want to select the row below the row I just copied into.
        Sheets("Win").Select
           lastRow = Range("C" & .Rows.Count).End(xlUp).Row
           ActiveSheet.Range("C" & lastRow & ":C" & lastRow).EntireRow.Select
       Sheets("Begin").Select
Community
  • 1
  • 1
Erin
  • 147
  • 1
  • 3
  • 9

2 Answers2

2

Try replacing this:

'this is the issue I'm having - I want to select the row below the row I just copied into.
Sheets("Win").Select
    lastRow = Range("C" & .Rows.Count).End(xlUp).Row
    ActiveSheet.Range("C" & lastRow & ":C" & lastRow).EntireRow.Select

with this:

With Sheets("Win")
    lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
    .Cells(lastRow + 1, 1).EntireRow.Select
End With
paul bica
  • 10,417
  • 3
  • 22
  • 40
  • Hi, this isn't seeming to work with the program I have. It keeps the data i just pasted selected, where I want it to be the row right below that. I tried changing the offset values, but no luck. Any tips? – Erin Jun 29 '15 at 23:29
  • Solved it - I wasn't calling it correctly. Thank you for your help! – Erin Jun 29 '15 at 23:34
  • I'm glad it helped (sorry for the edit - I thought it might be better to fix the code you provided) – paul bica Jun 29 '15 at 23:41
  • 2
    to select the row below should it not read `.Cells(lastRow + 1, 1).EntireRow.Select` ? – whytheq Jun 29 '15 at 23:55
1

Just to add to the existing answer. You can avoid doing so much selection by using a construction more like this:

On Error GoTo problem

Dim Archive As String

If (Target.Cells.Count = 1) And _
   Not (Excel.Application.Intersect(Target, [I3:I10000]) Is Nothing) Then

    Excel.Application.EnableEvents = False
    'User inputs type of event
    Archive = InputBox("Was this event a Win, Loss, or Close? (Please input Win/Loss/Close)")

    With Target

        '>>>> good idea to defend against users entering "win" instead of "Win"
        If (LCase(Archive) = "win") Then

            '>>>> find the last row in Win sheet at the beginning
            With Sheets("Win")
               lr = .Range("C" & .Rows.Count).End(Excel.xlUp).Row
            End With

            '>>>> as you are cutting there should be no need to do any subsequent deletion or clearcontents 
            .EntireRow.Cut Sheets("Win").Rows(lr + 1)

        End If

    End With
End If

problem:
Excel.Application.EnableEvents = True
whytheq
  • 32,991
  • 61
  • 166
  • 261