Input Box Results to Cell

smac001

New Member
Joined
Feb 18, 2013
Messages
12
I have the following code would like to have the results from the input box to copied to cell B7, however it is not copying the results Can someone help.

Private Sub CommandButton1_Click()
'Sub AddSheetTest()
'Replace "Sheet1" with the name of the sheet to be copied.
Dim i As Integer, x As Integer
Dim shtname As String
Dim shtTemp As Worksheet

ActiveSheet.Unprotect
ActiveWorkbook.Sheets("Template").Copy _
Before:=ActiveWorkbook.Sheets("Summary")
shtname = InputBox("Enter Project Number?", "Sheet name?")
ActiveSheet.Name = shtname
shtTemp.Range("b7").Valve = shtname
ActiveSheet.Shapes("CommandButton1").Delete
ActiveSheet.Protect
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I have the following code would like to have the results from the input box to copied to cell B7, however it is not copying the results Can someone help.

Private Sub CommandButton1_Click()
'Sub AddSheetTest()
'Replace "Sheet1" with the name of the sheet to be copied.
Dim i As Integer, x As Integer
Dim shtname As String
Dim shtTemp As Worksheet

ActiveSheet.Unprotect
ActiveWorkbook.Sheets("Template").Copy _
Before:=ActiveWorkbook.Sheets("Summary")
shtname = InputBox("Enter Project Number?", "Sheet name?")
ActiveSheet.Name = shtname
shtTemp.Range("b7").Valve = shtname
ActiveSheet.Shapes("CommandButton1").Delete
ActiveSheet.Protect
End Sub
You've dim'd shtTemp but youu haven't set it so when this line:
shtTemp.Range("b7").Valve = shtname
executes, shtTemp is Nothing.
You need a statement prior to that line like this:
Set shtTemp = Sheets("Your Sheet name here")
OR if shtTemp is the sheet you added then replace shtTemp with Activesheet. And whatever way you do it change .Valve to .Value
 
Last edited:
Upvote 0
Not sure how to code it would be the activesheet, but the info in the input is the name of the sheet, before it is actually named it Template (x).
This is the update and it now errors on the red...

Sub AddSheet()
'Replace "Sheet1" with the name of the sheet to be copied.
Dim i As Integer, x As Integer
Dim shtname As String
Dim ActiveSheet As Worksheet
' ActiveSheet.Unprotect
ActiveWorkbook.Sheets("Template").Copy _
Before:=ActiveWorkbook.Sheets("Summary")


shtname = InputBox("Enter Project Number?", "Sheet name?")


ActiveSheet.Name = shtname


ActiveSheet.Range("b7").Value = shtname


ActiveSheet.Shapes("Button 10").Delete


ActiveSheet.Protect
End Sub
 
Upvote 0
Not sure how to code it would be the activesheet, but the info in the input is the name of the sheet, before it is actually named it Template (x).
This is the update and it now errors on the red...

Sub AddSheet()
'Replace "Sheet1" with the name of the sheet to be copied.
Dim i As Integer, x As Integer
Dim shtname As String
Dim ActiveSheet As Worksheet
' ActiveSheet.Unprotect
ActiveWorkbook.Sheets("Template").Copy _
Before:=ActiveWorkbook.Sheets("Summary")


shtname = InputBox("Enter Project Number?", "Sheet name?")


ActiveSheet.Name = shtname


ActiveSheet.Range("b7").Value = shtname


ActiveSheet.Shapes("Button 10").Delete


ActiveSheet.Protect
End Sub
What has been entered for Project Number when the error occurs? What is the error number/message?

Also, you need an error trap with the input box like this:
shtname = InputBox("Enter Project Number?", "Sheet name?")
If shtname = "" Then Exit Sub
 
Upvote 0
Not sure what was going on, tried again this morning and it is now working will add the trap as you suggested. Thanks
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,685
Members
449,463
Latest member
Jojomen56

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