VBA Not Working Correctly

luke1438

Board Regular
Joined
Nov 1, 2004
Messages
156
I am trying to copy paste into a new workbook, then save that workbook as a CSV file with a number in cell J1 followed by today's date. I have the following code. It is working with but saving it as "& ThisFile & DateStr" instead of the number in Cell J1 and today's date.

VBA Code:
Sub NewBook()

    Range("A2:F200").Select
    Selection.Copy
    Workbooks.Add
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Dim ThisFile
    Dim DateStr
    ThisFile = Range("J1").Value
    DateStr = Format(Date, "dd-mm-yyyy")
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\TOM\Documents\Company\Temp\& ThisFile & DateStr.csv", FileFormat:= _
        xlCSV, CreateBackup:=False
    Range("J10").Select
End Sub

Any help or advice would be greatly appreciated.

Thanks
Luke
 

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.
you need to change this:
VBA Code:
"C:\Users\TOM\Documents\Company\Temp\& ThisFile & DateStr.csv", FileFormat:= _
to
VBA Code:
"C:\Users\TOM\Documents\Company\Temp\" & ThisFile & DateStr & ".csv", FileFormat:= _
 
Upvote 0
Hello offthelip,

I had already tried that but tried it again with your suggestions. I am getting a compiling error.

Regards
Luke
 
Upvote 0
try it like this:
VBA Code:
    ActiveWorkbook.SaveAs Filename:= _
  "C:\Users\TOM\Documents\Company\Temp\" & ThisFile & DateStr & ".csv", FileFormat:=xlCSV, CreateBackup:=False
 
Upvote 0
That works saving it as a date but the formatting is wrong and it is not pulling in the value from "J7". Why is it not pulling in ThisFile and DateStr? This is weird, it should work. It should be pulling in those defined values into the path.
 
Upvote 0
You need to be very specific about which object you're working with. For example, following the line:
VBA Code:
Workbooks.Add
the new workbook becomes the active workbook, so the line:
VBA Code:
ThisFile = Range("J1").Value
gets the value of J1 in the newly created workbook - which is blank. (I note you say not pulling in the value from "J7" in post #5 but you use J1 in your code.
 
Upvote 0
Solution
Ah, completely missed that - thanks. I put the code before the:

VBA Code:
Workbooks.Add

And it works perfectly - thanks so much for your help.

Regards
Luke
 
Upvote 0
Ah, completely missed that - thanks. I put the code before the:

VBA Code:
Workbooks.Add

And it works perfectly - thanks so much for your help.

Regards
Luke
Happy to help, and thanks for the feedback 👍
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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