Renaming worksheet in VBA

Crocdundee

Board Regular
Joined
May 10, 2010
Messages
174
Office Version
  1. 2013
Platform
  1. Windows
Hi I import a csv file each day in excel,and the worksheet has a different name each day.
I am using sheet1 as the worksheet in a macro.
How can I change the unknown worksheet to Sheet1 in VBA
This is the name of the worksheet I import "worksheet_Thursday, 12 May"
However it changes each day

-----------------------------------------
The way I see it,is
(1) use vba to capture the worksheet in a variable....the use the variable ??
(2) Rename it in vba ???
------------------------------------------------------
Any Help would be appreciated
Thank You
Graham
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Activesheet.name = "Sheet1"

Or in your host macro reference the sheet index (probably 1)

Sheets(1). instead of Sheets("Sheet1").

Cheers

Dan
 
Upvote 0
Graham,

If you know the sheet's position post import then you can change it simply by running code, say for the leftmost sheet (ie position 1)
Code:
ThisWorkbook.Sheets(1).Name = "Sheet1"
How do you do your import? VBA or manually?

Dave
 
Upvote 0
Thanks Dave I will try that code and let u know.
I Import Manually
it is a different name each day as i use Python code and it generates a file each day with todate in the file name

Grah
 
Upvote 0
Dave The code dos'nt work for this worksheet
I also tried it on other excel files to no avail
Regards
Graham
 
Upvote 0
Hi I import a csv file each day in excel,and the worksheet has a different name each day.
I am using sheet1 as the worksheet in a macro.
How can I change the unknown worksheet to Sheet1 in VBA
This is the name of the worksheet I import "worksheet_Thursday, 12 May"
However it changes each day

-----------------------------------------
The way I see it,is
(1) use vba to capture the worksheet in a variable....the use the variable ??
(2) Rename it in vba ???
------------------------------------------------------
Any Help would be appreciated
Thank You
Graham


Maybe:

Sheets("worksheet_Thursday, 12 May").Name = "Sheet1"

Or

Activesheet.name = "Sheet1"
 
Upvote 0
Thanks very much, The Active code worked fine

Appreciated your effort

Graham
 
Upvote 0
Graham,

You may want to consider automating the input as well. The code below should be hosted in the workbook where the renaming takes place (set by ThisWorkbook - possibly why the earlier code failed if you places it elsewhere). The code checks for an existing Sheet1 before the renaming

Cheers

Dave

Code:
Sub ReName()
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets("Sheet1")
    On Error GoTo 0
    If ws Is Nothing Then
        ThisWorkbook.ActiveSheet.Name = "Sheet1"
    Else
        MsgBox "Sheet1 already exists", vbCritical
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

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