From inputbox to cell works but how to add an cell overwrite warning MsgBox

borreltijd

New Member
Joined
Jul 2, 2008
Messages
25
Hi,

I am quite new with VBA and I am working on a excel sheet to gather data from an experiment. I want to have an absolute monkey proof program. Therefore I use an InputBox to gather data and to write it into a specif cell. I also check if the data range is valid.


For example:
Sub GetData()
' This macro is a first attempt to generate monkey proof macro for entering expimental data into a excel sheet.
' First some variables are declared.

Dim MP1_Suction_Temperature As Single
Dim MinValueTemperature As Single
Dim MaxValueTemperature As Single
MinValueTemperature = 0
MaxValueTemperature = 250

' Input of MP1_Suction_Temperature
Msg = " Enter the suction temperature, ranges from " & MinValueTemperature & " up to " & MaxValueTemperature & " [C]"
Do
UserEntry = InputBox(Msg)
If UserEntry = "" Then Exit Sub
If IsNumeric(UserEntry) Then
DblEntry = Val(UserEntry)
If DblEntry >= MinValueTemperature And DblEntry <= MaxValueTemperature Then Exit Do
End If
Msg = "Your previous entry was INVALID."
Msg = Msg & vbNewLine
Msg = Msg & "Enter a value between " & MinValueTemperature & " and " & MaxValueTemperature
Loop
Worksheets("DATASHEET").Range("MP1_Suction_Temperature").Value = UserEntry
' End of input MP1_Suction_Temperature
End Sub
What currently is missing in this procedure is a way to give a warning when the cell (MP1.Suction_Temperature) already contains data.( So when the macro is run for the second time)

I want a MsgBox to pop with the warning that the cell already contains data and the question overwrite yes or no = cancel.

Yes means continue and overwrite, cancel should should down the macro and leave the current data intact.

I am thinking about the IsEmpty tag but I am not sure,

thanks in advance.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I think you just need to add

Code:
IF Worksheets("DATASHEET").Range("MP1_Suction_Temperature").Value <> 0 Then
     X = MsgBox("Data in there already",vbYesNo)
     If X = 7 then Exit Sub     'Clicking No returns 7
End If

There are other options for the IF condition,
IF Worksheets("DATASHEET").Range("MP1_Suction_Temperature").Text <> ""
 
Last edited:
Upvote 0
yes something like the below at the commencement of your routine (you could think about storing your cell as a variable - range) but this would work I think

Code:
If IsEmpty(Worksheets("DATASHEET").Range("MP1_Suction_Temperature")) = False then
M1 = MsgBox("Cell Not Empty: Continue ?",vbyesno,"Overwrite?")
If M1 = vbno then Exit Sub
End If
 
Upvote 0
yes something like the below at the commencement of your routine (you could think about storing your cell as a variable - range) but this would work I think

Code:
If IsEmpty(Worksheets("DATASHEET").Range("MP1_Suction_Temperature")) = False then
M1 = MsgBox("Cell Not Empty: Continue ?",vbyesno,"Overwrite?")
If M1 = vbno then Exit Sub
End If

Thanks this works fine !
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,630
Members
449,041
Latest member
Postman24

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