Code Correction Required

ShoaibAli

Banned - Rules violations
Joined
Jan 15, 2020
Messages
121
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Platform
  1. Windows
Dear Team,

Using below but i dont want to add sheet name with xlxs. please correct the code so just it only paste the WB name as just 201710 201711 201712

Please update the code.


</>
Sub mergeWorkbooks()
Dim files, fn, wb As Workbook
files = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", , "Select Files", "Merge", True)
If TypeName(files) = "Boolean" Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Workbooks.Add
While wb.Sheets.Count > 1
wb.Sheets(wb.Sheets.Count).Delete
Wend
For Each fn In files
If fn <> ThisWorkbook.FullName Then
With Workbooks.Open(fn)
.Sheets(1).Copy After:=wb.Sheets(wb.Sheets.Count)
If you want to rename the new sheet to be the filename of the sheet it came from uncomment the line below
wb.Sheets(wb.Sheets.Count).Name = .Name
.Close False
End With
End If
Next fn
wb.Sheets(1).Delete
wb.Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Done!", vbInformation
End Sub
</>
 

Attachments

  • Capture.PNG
    Capture.PNG
    2.2 KB · Views: 6

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Change this line:
VBA Code:
wb.Sheets(wb.Sheets.Count).Name = .Name

For this:
VBA Code:
wb.Sheets(wb.Sheets.Count).Name = Left(.Name, Len(.Name) - 5)
 
Upvote 0
How about
VBA Code:
wb.Sheets(wb.Sheets.Count).Name = split(.Name,".")(0)
 
Upvote 0
If you have a name with intermediate dots and a 3 character extension example:
invoice.2020.xls
Then

VBA Code:
wb.Sheets(wb.Sheets.Count).Name = Left(.Name, InStrRev(.Name, ".") - 1)
 
Upvote 0
If you have a name with intermediate dots and a 3 character extension example:
invoice.2020.xls
Then

VBA Code:
wb.Sheets(wb.Sheets.Count).Name = Left(.Name, InStrRev(.Name, ".") - 1)

Sir thank you so much.
 
Upvote 0

Forum statistics

Threads
1,214,838
Messages
6,121,885
Members
449,057
Latest member
Moo4247

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