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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Code:
If Record_Number < Last Then MsgBox [B][COLOR=#ff0000]([/COLOR][/B]"Record do not exist!"[COLOR=#ff0000][B])[/B][/COLOR]: Exit Sub
 
Upvote 0
How doesn't it work? Are you getting an error?

Like below situation using smaller then,

Code:
If Record_Number < Last Then MsgBox "Too Big!": Exit Sub
if the last cell in column A which contains value is A10, then "Last" = 10, however, no matter I enter 1 to 100, no message box appear.

However, if I change to lager then,

Code:
If Record_Number > Last Then MsgBox "Too Big!": Exit Sub

It always show the alert.

You can have a try to copy and paste the same code and have a try, and you will have the same result.

Thanks.
 
Upvote 0
I don't have sample data. However this is confusing:

Code:
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

Last will be the row number of the last used row in Column A

lr will be the first blank row after Last

What is your thinking here?

I've commented your code how i see it:

Code:
Sub CopyRecord()'


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


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


Record_Number = InputBox("Which record you want to copy?") 'User input to copy a certain row


If Record_Number > Last Then MsgBox ("Record do not exist!"): Exit Sub 'Changed the sign to GREATER THAN from LESS THAN
' 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 'Copy Columns A:E of the row the user selected plus the row below it
    Range("A" & lr).Select 'Select next empty cell
    ActiveSheet.Paste 'paste to that cell
End Sub

Also note that the last 3 lines can be written on one line:

Code:
Range("A" & Record_Number & ":E" & Record_Number + 1).Copy Range("A" & lr)
 
Upvote 0
no matter I enter 1 to 100, no message box appear.

Hi, the way you are using InputBox() means that it is returning text and text is always greater than a number.

One way around this would be to force it return a number by using:
Code:
Record_Number = Application.InputBox("Which record you want to copy?", Type:=1)

You'll probably also want to add some code to check if the user pressed the cancel button.
 
Upvote 0
Hello gallen,

Thanks for your feedback, I am using Last to check the maximum number of rows.
While using lr as the placeholder as the "Copied" row.

And it's working fine. Thanks for your inspiration, I've modified the code a little bid.


I don't have sample data. However this is confusing:

Code:
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

Last will be the row number of the last used row in Column A

lr will be the first blank row after Last

What is your thinking here?

I've commented your code how i see it:

Code:
Sub CopyRecord()'


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


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


Record_Number = InputBox("Which record you want to copy?") 'User input to copy a certain row


If Record_Number > Last Then MsgBox ("Record do not exist!"): Exit Sub 'Changed the sign to GREATER THAN from LESS THAN
' 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 'Copy Columns A:E of the row the user selected plus the row below it
    Range("A" & lr).Select 'Select next empty cell
    ActiveSheet.Paste 'paste to that cell
End Sub

Also note that the last 3 lines can be written on one line:

Code:
Range("A" & Record_Number & ":E" & Record_Number + 1).Copy Range("A" & lr)
 
Upvote 0
Hi FormR,

Wow!, I didn't realized input box will always return as text until you told me so, big thank you.

Is there some Help file which can show me the remaining of those "Type"?

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

Hi, the way you are using InputBox() means that it is returning text and text is always greater than a number.

One way around this would be to force it return a number by using:
Code:
Record_Number = Application.InputBox("Which record you want to copy?", Type:=1)

You'll probably also want to add some code to check if the user pressed the cancel button.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,045
Members
449,206
Latest member
Healthydogs

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