VBA - Copy and paste values before saving

leolbi

New Member
Joined
Nov 3, 2016
Messages
5
Hi all!

I'm using a macro to copy data from a sheet and save it as a new .csv file.
I'm currently using the below which I got from searching online, which does exactly what I was expecting:
Code:
Sub Export()
Dim MyPath As String
Dim MyFileName As String


MyFileName = "SW_" & Sheets("Creator").Range("C3").Value & "_CampaignBuild_" & Format(Date, "ddmmyyyy")


If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"


Sheets("Google Upload").Copy


With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = "" '<~~ The start folder path for the file picker.
    If .Show <> -1 Then GoTo NextCode
    MyPath = .SelectedItems(1) & "\"
End With


NextCode:


With ActiveWorkbook
    
    .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
End With
End Sub

However, for some reason, the CSV file loads with a few #REF errors from some named ranges (but not all named ranges!) even though the original spreadsheet presents these values fine.
So, I'd like to add a line to paste the cells as values in the code above to see if that helps.

Can anyone help me achieve this?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
For more information, the cells causing the errors use INDIRECT - '=INDIRECT("Phrase"&ROW($A1)))'
 
Upvote 0
You might try...

Code:
Sub Export()
Dim MyPath As String
Dim MyFileName As String

MyFileName = "SW_" & Sheets("Creator").Range("C3").Value & "_CampaignBuild_" & Format(Date, "ddmmyyyy")
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"

Sheets("Google Upload").Copy
[COLOR=#ff0000]With ActiveSheet.UsedRange
    .Value = .Value
End With[/COLOR]
With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = "" '<~~ The start folder path for the file picker.
    If .Show <> -1 Then GoTo NextCode
    MyPath = .SelectedItems(1) & "\"
End With
NextCode:
With ActiveWorkbook
    .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
End With
End Sub

Cheers,

tonyyy
 
Upvote 0
Thanks tonyyy.

Unfortunately, the exported CSVs still have #REF errors and don't work!

Any other ideas?
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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