Using VBA to set Name in code

Joined
Mar 11, 2021
Messages
7
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hello everyone,

I want VBA to look up cell A46 and set the name. This is the code I currently have.

Sub Rectangle2_Click()

ActiveSheet.Copy after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = GetName
End Sub

Private Function GetName() As String
Dim x As Long, n As String
n = "JJJJ "
If SheetExists(n) Then
Do
x = x + 1
If Not SheetExists(n & x) Then Exit Do
Loop
n = n & x
End If
GetName = n
End Function

Private Function SheetExists(aName As String) As Boolean
On Error Resume Next
Dim sh As Worksheet: Set sh = Sheets(aName)
If Err = 0 Then SheetExists = True Else SheetExists = False
End Function
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi & welcome to MrExcel.
What exactly is your question?
 
Upvote 0
See a screenshot attached here.
 

Attachments

  • VBA Screenshot.PNG
    VBA Screenshot.PNG
    72.3 KB · Views: 10
Upvote 0
Yup you can use
VBA Code:
n = Range("A46").Value & " - "
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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