Use Msgbox for data validation

TPortsmouth

New Member
Joined
Apr 6, 2017
Messages
41
Hello Excel VBA expert,

By the help of MrExcel expert's help, I've developed below VBA to copy a specific record to the last row within the same worksheet.

Code:
Sub CopyRecord()
'

lr = Range("A" & Rows.Count).End(xlUp).Row + 1
'lr is to determine the Last Row

Last = Range("A" & Rows.Count).End(xlUp).Row
'This is to to determine the last Row address


Record_Number = InputBox("Which record you want to copy?")

If Record_Number < Last Then MsgBox "Record do not exist!": Exit Sub
' This is to display error message if user entered a number larger than the existing record number.


Range("A" & Record_Number & ":E" & Record_Number + 1).Copy
    Range("A" & lr).Select
    ActiveSheet.Paste
End Sub

The general idea is when use click the Macro button, a message box appear and copy the 2 row data and paste and the bottom of the same data set.

This is working fine. However, I want to add a validation to Stop the Macro if user enter a record number which does not exist.

Code:
If Record_Number < Last Then MsgBox "Record do not exist!": Exit Sub

However, there is some problem of this statement, and it didn't work. Can you help me on pointing out which part caused the failure?

Thanks.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Is there some Help file which can show me the remaining of those "Type"?

Hi, see here:
Application.InputBox Method (Excel)

By the way, I've used below code as validation, seems like it's working fine, do you think if it's ok?

Code:
If Record_Number = "" Then MsgBox ("Cancelled by user"): Exit Sub
' this checks for Cancel

If Record_Number <= 0 Then MsgBox ("Invalid record number!"): Exit Sub
' this checks for 0 and negative number

For the cancel shouldn't you be testing that the Record_Number variable = False?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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