Code check

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
hi what is wrong with this code? i have set ws as Sheet2

With ws
.Copy.SaveAs Filename:= _
"U:\TL\PV\CSV\test.csv", FileFormat:=xlCSV, _
CreateBackup:=False
.Close = False
End With
 
Ok, although i am on a network, the U: is a local as this is my personal drive on the network. I just did a test on my home computer which is local and i still got the same results, blank csv file when i run the code.

You need to use Share name for that drive that should begin with \\
Rich (BB code):
Sub test()
Dim temp As String, txt As String, i As Long, ii As Long
With Sheets("sheet2").UsedRange
    For i = 1 To .Rows.Count
        For ii = 1 To .Columns.Count
            temp = temp & "," & .Cells(i, ii).Text
        Next
        txt = txt & vbCrLf & Mid$(temp, 2) : temp = ""
    Next
End With
CreateObject("WScript.Shell").<WBR>CurrentDirectory = "\\server\TL\PV\CSV"
Open "test.csv" For Output As #1
    Print #1, Mid$(temp, 3)
Close #1
End Sub
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
OOOps,

Sorry
Rich (BB code):
   Print #1, Mid$(temp, 3)
shoule be
Rich (BB code):
   Print #1, Mid$(txt, 3)
 
Upvote 0
Seiya, one issue, i have comma in some of the data, and when i run the code some of data moves

OOOps,

Sorry
Rich (BB code):
   Print #1, Mid$(temp, 3)
shoule be
Rich (BB code):
   Print #1, Mid$(txt, 3)
 
Upvote 0
change
Rich (BB code):
        For ii = 1 To .Columns.Count
            temp = temp & "," & .Cells(i, ii).Text
        Next
to
Rich (BB code):
        For ii = 1 To .Columns.Count
            If Cells(i, ii).Text Like "*,*" Then flg = True
            temp = temp & "," & IIf(flg,Chr(34), "") & _
                .Cells(i, ii).Text & IIf(flg,Chr(34), "")
            flg = False
        Next
This will wrap those string includes comma(s) with double quotes, so it will fit to the right column when out put.
 
Upvote 0
i get an error "Next without For"

Code:
For ii = 1 To .Columns.Count
If Cells(i, ii).Text Like "*,*" Then flg = True
temp = temp & "," & IIf(flg, Chr(34), "") & _
.Cells(i, ii).Text & IIf(flg, Chr(34), "")
flg = False
Next
txt = txt & vbCrLf & Mid$(temp, 2): temp = ""
[B]Next[/B]

change
Rich (BB code):
        For ii = 1 To .Columns.Count
            temp = temp & "," & .Cells(i, ii).Text
        Next
to
Rich (BB code):
        For ii = 1 To .Columns.Count
            If Cells(i, ii).Text Like "*,*" Then flg = True
            temp = temp & "," & IIf(flg,Chr(34), "") & _
                .Cells(i, ii).Text & IIf(flg,Chr(34), "")
            flg = False
        Next
This will wrap those string includes comma(s) with double quotes, so it will fit to the right column when out put.
 
Upvote 0
Yeah, I think I made it unclear
Rich (BB code):
Sub test()
Dim temp As String, txt As String, i As Long, ii As Long
With Sheets("sheet2").UsedRange
    For i = 1 To .Rows.Count
        For ii = 1 To .Columns.Count
            If Cells(i, ii).Text Like "*,*" Then flg = True
            temp = temp & "," & IIf(flg,Chr(34), "") & _
                .Cells(i, ii).Text & IIf(flg,Chr(34), "")
            flg = False
        Next
        txt = txt & vbCrLf & Mid$(temp, 2) : temp = ""
    Next
End With
CreateObject("WScript.Shell").<WBR>CurrentDirectory = "\\server\TL\PV\CSV"
Open "test.csv" For Output As #1
    Print #1, Mid$(txt, 3)
Close #1
End Sub
 
Upvote 0
Excellent that works, thanks Seiya

Yeah, I think I made it unclear
Rich (BB code):
Sub test()
Dim temp As String, txt As String, i As Long, ii As Long
With Sheets("sheet2").UsedRange
    For i = 1 To .Rows.Count
        For ii = 1 To .Columns.Count
            If Cells(i, ii).Text Like "*,*" Then flg = True
            temp = temp & "," & IIf(flg,Chr(34), "") & _
                .Cells(i, ii).Text & IIf(flg,Chr(34), "")
            flg = False
        Next
        txt = txt & vbCrLf & Mid$(temp, 2) : temp = ""
    Next
End With
CreateObject("WScript.Shell").<WBR>CurrentDirectory = "\\server\TL\PV\CSV"
Open "test.csv" For Output As #1
    Print #1, Mid$(txt, 3)
Close #1
End Sub
 
Upvote 0
Seiya I tried your macro on another set of data which has commas and its moving data to next cell, so not sure why it worked before but not now

Yeah, I think I made it unclear
Rich (BB code):
Sub test()
Dim temp As String, txt As String, i As Long, ii As Long
With Sheets("sheet2").UsedRange
    For i = 1 To .Rows.Count
        For ii = 1 To .Columns.Count
            If Cells(i, ii).Text Like "*,*" Then flg = True
            temp = temp & "," & IIf(flg,Chr(34), "") & _
                .Cells(i, ii).Text & IIf(flg,Chr(34), "")
            flg = False
        Next
        txt = txt & vbCrLf & Mid$(temp, 2) : temp = ""
    Next
End With
CreateObject("WScript.Shell").<WBR>CurrentDirectory = "\\server\TL\PV\CSV"
Open "test.csv" For Output As #1
    Print #1, Mid$(txt, 3)
Close #1
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,786
Members
449,125
Latest member
shreyash11

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