Rename query after generating with vba

peterhess2002

New Member
Joined
Mar 29, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have a file called fileX.dat, which I conneted with a query in a sheet.

The file look like this:

data1.dat
xyz.dat
abc.dat

And I have a sheet called sheet_X, which has a query predefined and called: sample_query

Now I want to use the items in fileX.dat (data1.dat, xyz.dat & abc.dat) to create three sheets as a copy from sheet_X with its query

I was able to do that using the vba_code:

---------------------
Sub Test_1()

Dim i As Long
Dim LastFileNum As Long
Dim Name As String

Sheets("FileX").Activate

New_Position = 0

For i = 1 To 3

Actual_Name = Sheets("FileX").Cells(i, 1)
Length_Name = Len(Actual_Name)

Length_Name_New = Length_Name - 4
Actual_Name_New = Left(Actual_Name, Length_Name_New)

New_Position = New_Position + 1
Sheets("sheet_X").Copy After:=Sheets(New_Position)
ActiveSheet.Name = Actual_Name_New
Sheets(Actual_Name_New).Activate
Next i
End Sub
--------------------------------

The code works fine and generates for me the three sheets with the queries.

The problem that I am facing is that the name of the new queries generated are copies of the original name of the query + (2) and + (3)

I want to rename the new generated queries to be as Actual_Name_New.

Any Idea?

Regards

Peter
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,658
Latest member
GStorm

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