Filename = Dir(Path) is not giving me the full name

ds2020

New Member
Joined
Mar 15, 2020
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
this is part of my code to open multiple workbooks and put them in sheets in one workbook. However, the name (path) is not the full path (it's missing the important part of the names). What can I do to get the full name of every sheet from this code?



Path = "D:\uni\Test Data\197\New folder\"
Filename = Dir(Path)

Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy after:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop

Dim xsheet As Worksheet
For Each xsheet In ThisWorkbook.Worksheets
xsheet.Select
 
Thanks I didn't know it's limited to 31 characters. the part after unknown road is actually what I need if i can't do anything with the code I may need to change the file names but there are more than.
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Do you just want the last 15 characters of the workbook & are they all csv files?
 
Upvote 0
Ok how about
VBA Code:
Do While Filename <> ""
   Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
   Sheets(1).Copy after:=ThisWorkbook.Sheets(1)
   ThisWorkbook.Sheets(2).Name = Left(Right(Filename, 19), 15)
   Workbooks(Filename).Close
   Filename = Dir()
Loop
 
Upvote 0
Ok how about
VBA Code:
Do While Filename <> ""
   Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
   Sheets(1).Copy after:=ThisWorkbook.Sheets(1)
   ThisWorkbook.Sheets(2).Name = Left(Right(Filename, 19), 15)
   Workbooks(Filename).Close
   Filename = Dir()
Loop
Yes, it works perfectly. Thank you so much for your help. I've been playing with it for two days to figure something out.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Can I ask another question? I'm trying to refer a cell from a sheet (sheet1) to another cell in the first sheet but without mentioning the first sheets name. whatever I can't get the cell value from first sheet to the new one.


VBA Code:
    Sheets("Sheet1").Select
    Range("H2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=sheets(1)!RC"
    Range("H3").Select


I tried this, sheet1, and some other values for ActiveCell.FormulaR1C1 = but it doesn't give me the value
 
Upvote 0
How about
VBA Code:
Range("H2").FormulaR1C1 = "='" & Sheets(1).Name & "'!rc"
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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