Using " and , and ; in macro

volmer

New Member
Joined
Aug 20, 2008
Messages
3
Hi

I want to create a .csv file from a macro in excel, but when i use letters as , " ; it looks strange when i open the file in note path.

Eg. Feedback:=M10.1, when i save this line and open the file in Note path the line starts with " and ends with " and i don't know how i can make the file with out those letters.

Hope some can help me:ROFLMAO:
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi

Are you saving the file down as a csv using SaveAs? If so, you end up with those 'helpful' tags. You need to amend the way you create the file to eg:

Code:
Dim i as Integer, strToWrite As String
Dim strFileName As String

i = FreeFile
strFileName = "C:\YourFolder\YourFileToWriteTo.csv"

strToWrite = ActiveSheet.Range("A1").Value

Open strFileName For Binary Access Write As #i
  Put #i, , strToWrite
Close #i
 
Upvote 0
Hi

Are you saving the file down as a csv using SaveAs? If so, you end up with those 'helpful' tags. You need to amend the way you create the file to eg:

Code:
Dim i as Integer, strToWrite As String
Dim strFileName As String
 
i = FreeFile
strFileName = "C:\YourFolder\YourFileToWriteTo.csv"
 
strToWrite = ActiveSheet.Range("A1").Value
 
Open strFileName For Binary Access Write As #i
  Put #i, , strToWrite
Close #i


Hi

Tanks for the help

The next question for me is what if i have eg. a hole sheet that i want to apply to the file. :rolleyes:
 
Upvote 0
Here's a pair of VBA functions you could use to do this:

Code:
Function WriteText(ByVal strPathAndFilename As String, ByRef strToWrite As String, Optional ByVal blnOverWrite As Boolean = False) As Boolean
Dim i As Integer, strTemp
strTemp = Dir(strPathAndFilename)
If Len(strTemp) > 0 Then
    If blnOverWrite Then
        Kill strPathAndFilename
    Else
        WriteText = False
        Exit Function
    End If
End If
i = FreeFile
Open strPathAndFilename For Binary Access Write As #i
    Put #i, , strToWrite
Close #i
WriteText = True
End Function

Function MakeText(ByRef rng As Range, Optional ByVal strDelim As String = ",", Optional ByVal strNewLine As String = vbCrLf) As String
Dim varArray As Variant
Dim i As Long, j As Long
Dim strTemp As String

If rng.Count = 1 Then
    MakeText = rng.Value
    Exit Function
Else
    varArray = rng.Value
    For i = 1 To UBound(varArray, 1)
        For j = 1 To UBound(varArray, 2)
            strTemp = strTemp & varArray(i, j) & strDelim
        Next j
        strTemp = Left(strTemp, Len(strTemp) - 1) & strNewLine
    Next i
    strTemp = Left(strTemp, Len(strTemp) - 1)
    MakeText = strTemp
End If
End Function

Use in code like:

Code:
Sub WriteMySheet()
Dim strMyTextIWantToWrite as string, strFile as string
strMyTextIWantToWrite = maketext(activesheet.usedrange,",")
strFile = "C:\SomeFolder\SomeFile.csv"
WriteText strFile, strMyTextIWantToWrite,True
End Sub
 
Upvote 0
Here's a pair of VBA functions you could use to do this:

Code:
Function WriteText(ByVal strPathAndFilename As String, ByRef strToWrite As String, Optional ByVal blnOverWrite As Boolean = False) As Boolean
Dim i As Integer, strTemp
strTemp = Dir(strPathAndFilename)
If Len(strTemp) > 0 Then
    If blnOverWrite Then
        Kill strPathAndFilename
    Else
        WriteText = False
        Exit Function
    End If
End If
i = FreeFile
Open strPathAndFilename For Binary Access Write As #i
    Put #i, , strToWrite
Close #i
WriteText = True
End Function
 
Function MakeText(ByRef rng As Range, Optional ByVal strDelim As String = ",", Optional ByVal strNewLine As String = vbCrLf) As String
Dim varArray As Variant
Dim i As Long, j As Long
Dim strTemp As String
 
If rng.Count = 1 Then
    MakeText = rng.Value
    Exit Function
Else
    varArray = rng.Value
    For i = 1 To UBound(varArray, 1)
        For j = 1 To UBound(varArray, 2)
            strTemp = strTemp & varArray(i, j) & strDelim
        Next j
        strTemp = Left(strTemp, Len(strTemp) - 1) & strNewLine
    Next i
    strTemp = Left(strTemp, Len(strTemp) - 1)
    MakeText = strTemp
End If
End Function

Use in code like:

Code:
Sub WriteMySheet()
Dim strMyTextIWantToWrite as string, strFile as string
strMyTextIWantToWrite = maketext(activesheet.usedrange,",")
strFile = "C:\SomeFolder\SomeFile.csv"
WriteText strFile, strMyTextIWantToWrite,True
End Sub


Once again tanks i work 100% (y)
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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