Help with VBA Script code!

Jemmick

New Member
Joined
Mar 25, 2013
Messages
8
i have a excel file that grabs data from an odbc drive then compiles the data into a csv file. I want to update the script below to save the file to the users desktop (its fine if i have to modify the username code) with no prompts. I stole this script from someone else's example and can't figure out how to modify it to save to the desktop versus "save as". Please help so I can wrap up this project!


===================================
Sub ExportRangetoFile()
' -------------------------------------------------------------------------------------------------------------------
'
' General function: exports strings contained in a range (1 column wide, varying height) into a text file
'
' -------------------------------------------------------------------------------------------------------------------
Const ForAppending = 8
Const rStartCell = "macroSwitch_OutputTextBelow"
Const strFileNamePrefix = "Valor Teamworx Export "
Dim fs, f
Dim saveFile As String
Dim WorkRng As Range
Dim iHeightOfDataRange


On Error Resume Next


' figure out how many rows to select and export:
' start at range "macroSwitch_OutputTextBelow"
' look down to last filled in row to count number of rows with actual check data
' then use that height as height for range to be exported
iHeightOfDataRange = Range(rStartCell).End(xlDown).Row - Range(rStartCell).Row


Set WorkRng = Application.Selection
Set WorkRng = Range(rStartCell).Offset(1, 0).Resize(iHeightOfDataRange, 1)
Application.ScreenUpdating = False
Application.DisplayAlerts = False


' create file to export to
Set fs = CreateObject("Scripting.FileSystemObject")
saveFile = Application.GetSaveAsFilename(InitialFileName:=strFileNamePrefix & Format(Now(), "yyyy-mm-dd"), _
filefilter:="Comma Separated Text (*.CSV), *.CSV")
Set f = fs.createTextFile(saveFile, ForAppending, TristateFalse)


' and write export range line by line
For Each Row In WorkRng.Rows
f.writeline Row
Next Row
f.Close


Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True




End Sub
==========================================
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Do you mean you want to alter this so instead of the user selecting the filename and path to save it's hard coded to save to their desktop?
Code:
saveFile = Application.GetSaveAsFilename(InitialFileName:=strFileNamePrefix & Format(Now(), "yyyy-mm-dd"), _
filefilter:="Comma Separated Text (*.CSV), *.CSV")

If so, what filename would you want to use?
 
Upvote 0
Do you mean you want to alter this so instead of the user selecting the filename and path to save it's hard coded to save to their desktop?
Code:
saveFile = Application.GetSaveAsFilename(InitialFileName:=strFileNamePrefix & Format(Now(), "yyyy-mm-dd"), _
filefilter:="Comma Separated Text (*.CSV), *.CSV")


If so, what filename would you want to use?

Yes I would want it hardcoded to save as TeamWorxUpload.csv
 
Upvote 0
Try this.
Code:
Sub ExportRangetoFile()
' -------------------------------------------------------------------------------------------------------------------
'
' General function: exports strings contained in a range (1 column wide, varying height) into a text file
'
' -------------------------------------------------------------------------------------------------------------------
Const ForAppending = 8
Const TristateFalse = 0
Const rStartCell = "macroSwitch_OutputTextBelow"
Const strFileNamePrefix = "Valor Teamworx Export "
Dim fs, f
Dim saveFile As String
Dim WorkRng As Range
Dim iHeightOfDataRange
Dim strDeskTopPath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    On Error Resume Next

    ' get path to user's desktop
    strDeskTopPath = CreateObject("Wscript.Shell").SpecialFolders("Desktop")
    
    ' figure out how many rows to select and export:
    ' start at range "macroSwitch_OutputTextBelow"
    ' look down to last filled in row to count number of rows with actual check data
    ' then use that height as height for range to be exported
    iHeightOfDataRange = Range(rStartCell).End(xlDown).Row - Range(rStartCell).Row
    
    
    Set WorkRng = Application.Selection
    Set WorkRng = Range(rStartCell).Offset(1, 0).Resize(iHeightOfDataRange, 1)

    ' create file to export to
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    saveFile = strDeskTopPath & "\TeamWorxUpload.csv"
    
    Set f = fs.createTextFile(saveFile, ForAppending, TristateFalse)
        
    ' and write export range line by line
    For Each Row In WorkRng.Rows
        f.writeline Row
    Next Row
    
    f.Close
        
    Application.CutCopyMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Try this.
Code:
Sub ExportRangetoFile()
' -------------------------------------------------------------------------------------------------------------------
'
' General function: exports strings contained in a range (1 column wide, varying height) into a text file
'
' -------------------------------------------------------------------------------------------------------------------
Const ForAppending = 8
Const TristateFalse = 0
Const rStartCell = "macroSwitch_OutputTextBelow"
Const strFileNamePrefix = "Valor Teamworx Export "
Dim fs, f
Dim saveFile As String
Dim WorkRng As Range
Dim iHeightOfDataRange
Dim strDeskTopPath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    On Error Resume Next

    ' get path to user's desktop
    strDeskTopPath = CreateObject("Wscript.Shell").SpecialFolders("Desktop")
    
    ' figure out how many rows to select and export:
    ' start at range "macroSwitch_OutputTextBelow"
    ' look down to last filled in row to count number of rows with actual check data
    ' then use that height as height for range to be exported
    iHeightOfDataRange = Range(rStartCell).End(xlDown).Row - Range(rStartCell).Row
    
    
    Set WorkRng = Application.Selection
    Set WorkRng = Range(rStartCell).Offset(1, 0).Resize(iHeightOfDataRange, 1)

    ' create file to export to
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    saveFile = strDeskTopPath & "\TeamWorxUpload.csv"
    
    Set f = fs.createTextFile(saveFile, ForAppending, TristateFalse)
        
    ' and write export range line by line
    For Each Row In WorkRng.Rows
        f.writeline Row
    Next Row
    
    f.Close
        
    Application.CutCopyMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
        
End Sub

That got it you are awesome!
 
Upvote 0
Try this.
Code:
Sub ExportRangetoFile()
' -------------------------------------------------------------------------------------------------------------------
'
' General function: exports strings contained in a range (1 column wide, varying height) into a text file
'
' -------------------------------------------------------------------------------------------------------------------
Const ForAppending = 8
Const TristateFalse = 0
Const rStartCell = "macroSwitch_OutputTextBelow"
Const strFileNamePrefix = "Valor Teamworx Export "
Dim fs, f
Dim saveFile As String
Dim WorkRng As Range
Dim iHeightOfDataRange
Dim strDeskTopPath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    On Error Resume Next

    ' get path to user's desktop
    strDeskTopPath = CreateObject("Wscript.Shell").SpecialFolders("Desktop")
    
    ' figure out how many rows to select and export:
    ' start at range "macroSwitch_OutputTextBelow"
    ' look down to last filled in row to count number of rows with actual check data
    ' then use that height as height for range to be exported
    iHeightOfDataRange = Range(rStartCell).End(xlDown).Row - Range(rStartCell).Row
    
    
    Set WorkRng = Application.Selection
    Set WorkRng = Range(rStartCell).Offset(1, 0).Resize(iHeightOfDataRange, 1)

    ' create file to export to
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    saveFile = strDeskTopPath & "\TeamWorxUpload.csv"
    
    Set f = fs.createTextFile(saveFile, ForAppending, TristateFalse)
        
    ' and write export range line by line
    For Each Row In WorkRng.Rows
        f.writeline Row
    Next Row
    
    f.Close
        
    Application.CutCopyMode = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
        
End Sub


WHAT IF I WANT TO NAME THE FILE NAME "TeamworkUpload010131.csv" where the 010131 is equal to yesterdays date
 
Upvote 0
Try this.
Code:
saveFile = strDeskTopPath & "\TeamWorxUpload" & Format(Date()-1, "ddmmyyyy") & ".csv"
Change 'ddmmyyyy' to get the date formatting you want.
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,203
Members
448,951
Latest member
jennlynn

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