*urgent* - Cell mandatory before click on button (macro)

Rahulwork

Active Member
Joined
Jun 9, 2013
Messages
284
Hello Everyone,

Hope you all are safe.

I need your help to create a code,

I have one macro on button which collect and generate something

Sub Button20_Click()

Range("C12:T16").Select
ActiveCell.FormulaR1C1 = "=R[8]C[25]"
Range("C10").Select
Range("C12").Copy

End Sub


if A2 and D2 are blank and user click on button, there would be msgbox stating that pls provide value in these cells. please help me.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Message if both inputs are empty:

VBA Code:
if sheet("sheetname").range("a2").value = "" and sheet("sheetname").range("d2").value = "" then
msgbox("Both input boxes are empty")
Exit sub
End If

Or

VBA Code:
if sheet("sheetname").range("a2").value = "" and sheet("sheetname").range("d2").value = "" then
msgbox("Both input boxes are empty")
else 
'do something
End If



Message if either are empty:

VBA Code:
if sheet("sheetname").range("a2").value = "" or sheet("sheetname").range("d2").value = "" then
msgbox("One or more input boxes are empty")
Exit sub
End If

or

VBA Code:
if sheet("sheetname").range("a2").value = "" or sheet("sheetname").range("d2").value = "" then
msgbox("Both input boxes are empty")
else 
'do something
End If
 
Upvote 0
Thanks for your response

in place of do something can i put this statement

Sub Button20_Click()

Range("C12:T16").Select
ActiveCell.FormulaR1C1 = "=R[8]C[25]"
Range("C10").Select
Range("C12").Copy


Please help how can i merge these two things in one code
 
Upvote 0
VBA Code:
Sub Button20_Click()

if sheet("sheetname").range("a2").value = "" and sheet("sheetname").range("d2").value = "" then
msgbox("Both input boxes are empty")
else 
Range("C12:T16").Select
ActiveCell.FormulaR1C1 = "=R[8]C[25]"
Range("C10").Select
Range("C12").Copy 
End If
End sub
 
Upvote 0
Thank you so much your response, it working but i have one more case like

if A2 is blank msg box should say input value in A2 and if D2 is blank then vise versa..is it possible to make this?

bottom line is .. if any cell is blank there respective msg box should appear
 
Upvote 0
Yes,
I put an example of 'or' instead of 'and' code

You can also put it as separate if statements that run in turn.

VBA Code:
If sheet("sheetname").range("A1").value = "" then
Msgbox("A1 is blank, please address")
Exit sub
End if

If sheet("sheetname").range("B1").value = "" then
Msgbox("B1 is blank, please address")
Exit sub
End if

If sheet("sheetname").range("C1").value = "" then
Msgbox("C1 is blank, please address")
Exit sub
End if

' code
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,064
Members
448,941
Latest member
AlphaRino

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