How to shorten file name which is used to name the sheet name in a master file using VBA?

Sheila003

New Member
Joined
Nov 8, 2024
Messages
3
Office Version
  1. 365
Hi,

I am not used to Macro, however I would like to automate a process which I found a formula but need it be tweaked so I need some help please.

I am basically trying to copy a specific excel sheet in files that are kept in a folder and name the sheet in the master workbook the name of the file. Below is the the VBA code

It is unable to copy due to the length of the file name being more than 31. How do I shortened the file name on ActiveSheet name please?

Sub MergeWorkbooks()

Dim FolderPath As String
Dim File As String


FolderPath = "C:\Users\x\yyyy\Documents\Testing\Test\"

File = Dir(FolderPath)

Do While File <> ""

Workbooks.Open FolderPath & File
ActiveWorkbook.Worksheets(2).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = Replace(File, ".xlsx", "")
Workbooks(File).Close

File = Dir()

Loop


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try:
VBA Code:
ActiveSheet.Name = Left(Replace(File, ".xlsx", ""), 31)
 
Upvote 0

Forum statistics

Threads
1,223,670
Messages
6,173,724
Members
452,529
Latest member
jpaxonreyes

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