Trying to prevent VBA from changing date string into an actual date

2020914

New Member
Joined
Feb 27, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello -

I have a block of code below which "copies" formulas in row 1 of my worksheet and "pastes" them into the first row of data below the header row. In this case, my header row is row 4 on my worksheet and the data starts in row 5. (I put "copy" and "paste" in quotes because I'm using a range to avoid using actual .copy & .paste functions in my code.)

The code executes perfectly until I hit the final line below. That line works too from a VBA standpoint, but it's changing one of my columns from a string to a date because of .Value. I've tried alternatives such as .Text (this wipes out data from that column completely). I've also tried making sure my Excel formula that is being copied uses the TEXT function, hoping that would keep it as text. But it always changes it to a date when this .Value part of my code executes, and I'm not sure how to stop it.

VBA Code:
        'determine start/end columns
        Copy_From_Col = Header_Range.Find("Order_Date", LookIn:=xlValues, LookAt:=xlWhole).Column
        Copy_To_Col = Header_Range.Find("PO Sort Key", LookIn:=xlValues, LookAt:=xlWhole).Column

        'copy/paste formulas        
        Set Copy_Range = .Range(.Cells(1, Copy_From_Col), .Cells(1, Copy_To_Col))
        .Range(.Cells(First_Row, Copy_From_Col), .Cells(First_Row, Copy_To_Col)) = Copy_Range.FormulaR1C1
        
        'autofill & calculate
        .Range(.Cells(First_Row, Copy_From_Col), .Cells(First_Row, Copy_To_Col)).AutoFill _
            Destination:=.Range(.Cells(First_Row, Copy_From_Col), .Cells(Last_Row, Copy_To_Col))
        .Cells.Calculate
        
        'copy/paste values
        Set Copy_Range = .Range(.Cells(First_Row, Copy_From_Col), .Cells(Last_Row, Copy_To_Col))
        .Range(.Cells(First_Row, Copy_From_Col), .Cells(Last_Row, Copy_To_Col)) = Copy_Range.Value   'problem, .Value changes date string to actual date

The column in question is technically a date, but I have it formatted as follows: "2021-05-01". The reason I'm trying to avoid Excel changing this to an actual date (in thise case, 5/1/2021) is because I later use this column to create folders on our shared drive and that's how we want the folder names formatted.

Hope this makes sense and that someone can provide a possible solution.

Thank you!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I suggest using only real dates in your file. Using text to represent dates means they can't be used for anything else but display. Solve the problem downstream where you create the folder names:

VBA Code:
FolderName = "My New Folder for date " & Format(Range("A1"), "yyyy-mm-dd")
 
Upvote 0
Solution
I suggest using only real dates in your file. Using text to represent dates means they can't be used for anything else but display. Solve the problem downstream where you create the folder names:

VBA Code:
FolderName = "My New Folder for date " & Format(Range("A1"), "yyyy-mm-dd")

Didn't even consider that, and that's a much better solution. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

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