Delete Last Row

trsisko

Board Regular
Joined
May 20, 2008
Messages
176
I currently use the following code to move up one question in a spreadsheet that I'm doing; Problem is that it actually keeps the last answer. I actually want to delete the last Excel Row when the Prev Button is pressed. How Can I delete the last row on an excel worksheet?

Code:
Private Sub CmdPrevQuestion_Click()
If m_QID = 1 Then
m_QID = 1
CmdPrevQuestion.Enabled = False
Else
m_QID = QID_Prev(m_Question)
 
Call Question_Load
 
CmdPrevQuestion.Enabled = True
End If
 
End Sub
 
'This will return to Last question
Function QID_Prev(Question As String) As Integer
QID_Prev = 0
On Error Resume Next
QID_Prev = Application.WorksheetFunction.Match(Question, shOutput.Range("$B$1:$B$5000"), 0) - 1
If QID_Prev = 0 Then QID_Prev = Application.CountA(shOutput.Columns(1))
QID_Prev = CInt(shOutput.Range("A" & QID_Prev).Value)
End Function
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

To define lastrow use;

Code:
Dim LastRow as long

LastRow = Range("A" & Rows.Count).end(xlup).Row

Add that at the top and this will delete the row;

Code:
Rows(LastRow).delete
 
Upvote 0
This works great. Though I have two issues with it. I need it to always work on the "output" sheet and secondly I need to switch off Screen updating?
 
Upvote 0
Hi,

As a stand alone this;

Code:
Sub Delete_LastRow ()

Application.Screenupdating = False

Dim LastRow as long

LastRow = Sheets("output").Range("A" & Rows.Count).end(xlup).Row

Sheets("output").Rows(LastRow).delete

Application.Screenupdating = True

End Sub
 
Upvote 0
The suggested code could be shortened to:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Delete_LastRow()<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    Sheets("output").Range("A" & Rows.Count).End(xlUp).EntireRow.Delete<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top