VBA to capture Sheetname and use.

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a project that is currently only 1 sheet. I'm using Sheets(1) to reference it so I don't have to worry about what it is named. However, in the future, this single sheet may be copied up to 30 times and the sheet names changed each time. Instead of manually changing all of them to Sheets(2), Sheets(3)... etc, how can I capture the Codename or Sheet name with VBA and then use as the active sheet for the rest of my formula?

Does something like this work? I've tried it but with no success so I wonder what I'm missing.

VBA Code:
shName = ActiveSheet.Name
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
VBA Code:
Option Explicit

Sub WatShtName()
Dim shName As String
    shName = ActiveSheet.Name
    MsgBox shName
End Sub
 
Upvote 0
VBA Code:
Option Explicit

Sub WatShtName()
Dim shName As String
    shName = ActiveSheet.Name
    MsgBox shName
End Sub

How would I add shName into another variable? I get a type mismatch error when trying the below.

I added your code above and wanted to do the following because I'm using sh in a with statement further down.

VBA Code:
Dim shName As String
Dim sh As Worksheet

Name = ActiveSheet.Name

sh = Name
 
Upvote 0
.
[ICODE] [CODE=vba]Option Explicit Sub shVar() Dim sh As Worksheet Set sh = ActiveSheet MsgBox sh.Name With sh Range("B2").Value = "Hello" End With End Sub[/CODE] [/ICODE]
 
Upvote 0
How would I add shName into another variable? I get a type mismatch error when trying the below.

I added your code above and wanted to do the following because I'm using sh in a with statement further down.

VBA Code:
Dim shName As String
Dim sh As Worksheet

Name = ActiveSheet.Name

sh = Name

so I did this:
VBA Code:
Set sh = ActiveSheet

This works great, however, I'm having issues with my clear content vba. This is what it was

Code:
  Sheet1.Range("D5:O5, A7:O109, A200").ClearContents

I don't know what to do with it now to make it work on the active sheet only. Any ideas?

I tried the following but neither option worked out. It does clear content, but it clears it on the sheets1instead of what I'm actively using.

Code:
sh.Range("D5:O5, A7:O109, A200").ClearContents
ActiveSheet.Range("D5:O5, A7:O109, A200").ClearContents

Update: I don't know what the heck is going on, I re-opened Excel and the sh.Range command works for clearing content now. Bizzare issue but thanks for the help @Logit
 
Last edited:
Upvote 0
.
See if this works :

VBA Code:
Option Explicit

Sub shVar()
Dim sh As Worksheet

Set sh = ActiveSheet

With sh

    .Range("D5:O5, A7:O109, A200").ClearContents

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
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