VBA macro not splitting the path name properly

dpteo

New Member
Joined
Dec 8, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a code to split the path name into separate columns. However it is not splitting it correctly.
1638944523105.png

The S/N column is from the path after Gantry1\
The year column should be 2021
The month column should be 01
the date column should be 08

However after running the macro, the year became 1 and the month became 8 and the date became 16
I would greatly appreciate your help to find out what is the problem with my code.
1638944707493.png

VBA Code:
        If InStr(DestinationRange, "Gantry1") > 0 Then
        
            DestinationRange.Offset(0, 3) = Split(Split(DestinationRange.Value, "Gantry1\")(1), "\")(0)  'S/N
            DestinationRange.Offset(0, 4) = Split(Split(DestinationRange.Value, "Gantry1\")(1), "_")(1)  'Year
            DestinationRange.Offset(0, 5) = Split(Split(DestinationRange.Value, "Gantry1\")(1), "_")(2)  'Month
            DestinationRange.Offset(0, 6) = Split(Split(DestinationRange.Value, "Gantry1\")(1), "_")(3)  'Day
            
            yearonly = Split(Split(DestinationRange.Value, "Gantry1\")(1), "_")(1)
            monthonly = Split(Split(DestinationRange.Value, "Gantry1\")(1), "_")(2)
            dayonly = Split(Split(DestinationRange.Value, "Gantry1\")(1), "_")(3)
            
            Dateonly = dayonly & "/" & monthonly & "/" & yearonly
            
            DestinationRange.Offset(0, 7) = Dateonly
            
            DestinationRange.Offset(0, 8) = WeekNum(Dateonly)
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
If I correctly understood since I don't want to manually create a sample of your path:
VBA Code:
'with this:
destinationrange.Offset(0, 3) = Split(destinationrange.Value, "Gantry1\")(1)
'you fetch:
ATM-MD-00002\2021_08_01_16_14_12\Report.pdf
'so to fetch the date from this these need to be:
destinationrange.Offset(0, 3) = Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(0) 'S/N
destinationrange.Offset(0, 4) = Split(Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(1), "_")(0) 'Year
destinationrange.Offset(0, 5) = Split(Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(1), "_")(1) 'Month
destinationrange.Offset(0, 6) = Split(Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(1), "_")(2) 'Day
then you can update your macro to rebuild your "Dateonly".
 
Last edited:
Upvote 0
Hi & welcome to MrExcel.

You're letting us know that the leading zeros of day and month should be preserved.
In that regard, in addition to @rollis13's contribution, I would like to note that it's necessary to format the respective columns as text.
 
Upvote 0
If I correctly understood since I don't want to manually create a sample of your path:
VBA Code:
'with this:
destinationrange.Offset(0, 3) = Split(destinationrange.Value, "Gantry1\")(1)
'you fetch:
ATM-MD-00002\2021_08_01_16_14_12\Report.pdf
'so to fetch the date from this these need to be:
destinationrange.Offset(0, 3) = Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(0) 'S/N
destinationrange.Offset(0, 4) = Split(Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(1), "_")(0) 'Year
destinationrange.Offset(0, 5) = Split(Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(1), "_")(1) 'Month
destinationrange.Offset(0, 6) = Split(Split(Split(destinationrange.Value, "Gantry1\")(1), "\")(1), "_")(2) 'Day
then you can update your macro to rebuild your "Dateonly".
Hi @rollis13, thanks for the help. It solved my problem!
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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