Can't rename cell using VBA

Coda

New Member
Joined
Mar 30, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I'm trying to make a spreadsheet with a macro that asks the user for the Case ID (case_id), and creates a new sheet which is copied from a template (wsTemp). I then want to rename the new sheet with the case ID that the user provided, and also enter the case ID in the cell A1 of the new sheet.

That's where the problem is. For some reason, Excel creates a sheet and calls it "Template (2)" instead using the case ID. It also does not change the value of cell A1. Any ideas?

VBA Code:
Sub new_case_macro ()

Dim wsTemp As Worksheet

Dim case_id As String

case_id InputBox ("Enter Case ID: ")

With ThisWorkbook

Set wsTemp Sheets ("Template") wsTemp.Copy Before:-.Sheets (.Sheets.Count)

ActiveSheet.Range ("Al").Value -case_id

ActiveSheet.Name = case_id

End With

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I assume that is not the actual code since you are missing a lot of = signs in it.
 
Upvote 0
I assume that is not the actual code since you are missing a lot of = signs in it.
Oh, I'm sorry.. I had google lens copy the code from a picture and didn't notice how flawed it was. Here's the code again:

VBA Code:
Sub new_case_macro ()

Dim wsTemp As Worksheet
Dim case_id As String

case_id = InputBox ("Enter Case ID: ")

With ThisWorkbook

Set wsTemp = .Sheets ("Template")
wsTemp.Copy Before:=.Sheets(.Sheets.Count) 
ActiveSheet.Range("A1").Value = case_id ActiveSheet.Name = case id

End With
End Sub
 
Upvote 0
If you are not getting an error then there must be a sheet that is getting renamed and has A1 altered, even if it's not the one you want. I'd suggest changing the last lines to:

Code:
with .Sheets(.Sheets.Count - 1)
    .Range("A1").Value = case_id
   .Name = case_id
End With

rather than:

Code:
ActiveSheet.Range("A1").Value = case_id
ActiveSheet.Name = case_id
 
Upvote 0
Solution
Hello again!

I tried your proposed solution:

VBA Code:
Sub new_case_macro ()

Dim wsTemp As Worksheet
Dim case_id As String

case_id = InputBox ("Enter Case ID: ")

With ThisWorkbook

Set wsTemp = .Sheets ("Template")
wsTemp.Copy Before:=.Sheets(.Sheets.Count) 

with .Sheets(.Sheets.Count - 1)

   .Range("A1").Value = case_id
   .Name = case_id

End With

End With
End Sub

Unfortunately I am still getting the same result :(
 
Upvote 0
Do you have any other event code in the workbook?
 
Upvote 0
Ok, Update. I closed the file and opened it again, and it just works... Thank you Rory for the solution!
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

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