VBA / Macros to copy & paste and create a .CSV file, but to ignore keep empty values and not paste zero

maleodillet

New Member
Joined
Feb 15, 2018
Messages
12
Hi Gurus,

I have this VBA Code that I using to copy an Excel range and then use it to create a CSV file. It is using "PasteValuesOnly" to get the Job done but when a cell from the range is empty it pastes a Zero when I actually want it to keep the empty cell on the newly created CSV file. Any suggestions? please see my code below:


Sub CreateDatafile()


Dim content As String
Dim rng As Range
Set rng = Range("Q20:Y40")
Dim Path As String
Dim FileName1 As String


Dim sWB As Workbook, _
sWS As Worksheet

Dim dWB As Workbook, _
dWS As Worksheet

Path = "C:\Users\maleo\Desktop\MASTERDATA_LOADING"
'Path = "\ROOT\WEBFOLDERS\PLNSBX\SUMMARY\DATAMANAGER\DATAFILES\ or C:\Users\maleo\Desktop\MASTERDATA_LOADING"
FileName1 = Range("R16")
Set sWB = ActiveWorkbook
Set sWS = sWB.ActiveSheet


Set dWB = Workbooks.Add
Set dWS = dWB.Sheets(1)


sWS.Range("Q20:Y40").Copy
dWS.Range("A1").PasteSpecial xlPasteValues
dWB.SaveAs Filename:=Path & FileName1 & ".csv", FileFormat:=xlCSV
dWB.Close False

MsgBox "The Updated Master Data file has been created"

End Sub


 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the Board!

but when a cell from the range is empty it pastes a Zero when I actually want it to keep the empty cell on the newly created CSV file.
I cannot reproduce this behavior.
What exactly is in these blanks cells? Are they really blank, or is there a formula that is returning blanks?
Are you sure that there really aren't any zeroes in the original data that may just be hidden from your view with a Custom Format or Conditional Formatting?
How exactly are you viewing the CSV file after it is created to confirm that there are zeroes in it?
 
Upvote 0
Hi & welcome to the board
Try
Code:
Set dWB = Workbooks.Add
Set dWs = dWB.Sheets(1)

dWs.Range("A1:I21").Value = sWs.Range("Q20:Y40").Value
dWB.SaveAs Filename:=Path & Filename1 & ".csv", FileFormat:=xlCSV
dWB.Close False
 
Upvote 0
Welcome to the Board!


I cannot reproduce this behavior.
What exactly is in these blanks cells? Are they really blank, or is there a formula that is returning blanks?
Are you sure that there really aren't any zeroes in the original data that may just be hidden from your view with a Custom Format or Conditional Formatting?
How exactly are you viewing the CSV file after it is created to confirm that there are zeroes in it?

Joe, Actually you're right. the Zero Value it's hidden by formatting so i guess I have to figure out a way make that cell "empty if equal to zero" prior to creating the copying the values and creating the CSV right?
 
Upvote 0
You can do that with a simple Find/Replace after you paste it in your new sheet:
Code:
    [COLOR=#333333]dWs.Range("A1:I21")[/COLOR].Replace What:="0", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Upvote 0
thanks for the response, but wouldn't this replace any other zeros for other cells such as "2018" being replaced with "2 18" and so on?
 
Upvote 0
thanks for the response, but wouldn't this replace any other zeros for other cells such as "2018" being replaced with "2 18" and so on?
Nope. Test it out and see!

The key is this part:
Code:
LookAt:=xlWhole
If you do Find/Replace manually, this is the "Match entire cell contents" check box.
This tells it to only replace if the entire entry matches exactly. So it will only replace cells that are EXACTLY 0 (and will not touch the others than might have a zero in them, among other things).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,811
Members
449,127
Latest member
Cyko

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