VBA - Modify existing code that exports range to file, to prompt for location to save file

AGrayson84

New Member
Joined
Mar 21, 2017
Messages
18
Hi everyone, I have some VBA code that is used to export a specific cell range to a .PS1 file, which basically makes a PowerShell output file that I can run to do some scripts. I have the below VBA that designates the filename and the file location, but I would like to alter the script to prompt the user for the file location, while retaining the filename designation along with everything else in the VBA code. Would anyone mind helping me alter my below code to prompt for the file location? Thank you in advance.

Code:
Sub ExportToPS1()
If Len(Dir("N:\NDA\1_Add_To_Scanning", vbDirectory)) = 0 Then

   MkDir "N:\NDA\1_Add_To_Scanning"

End If
    Dim r As Range, c As Range
    Dim sTemp As String
    Dim AddToScanningFilename As String
    Dim AddToScanningPath As String
    AddToScanningPath = "N:\NDA\1_Add_To_Scanning\"
    AddToScanningFilename = Worksheets("Input").Range("A8").Value & "_" & Worksheets("Input").Range("B8").Value & "_" & Format$(Date, "mmddyyyy") & "_" & "Add_To_Scanning_VLAN_999" & ".ps1"

    Open AddToScanningPath & AddToScanningFilename For Output As #1
    For Each r In Range("B2:B200")
        sTemp = ""
        For Each c In r.Cells
            sTemp = sTemp & c.Text & Chr(9)
        Next c

        'Get rid of trailing tabs
        While Right(sTemp, 1) = Chr(9)
            sTemp = Left(sTemp, Len(sTemp) - 1)
        Wend
        Print #1, sTemp
    Next r
    Close #1
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,
see if this update to your code does what you want

Code:
Sub ExportToPS1()
    Dim r As Range, c As Range
    Dim sTemp As String
    
    Dim AddToScanningFilename As String
    Dim AddToScanningPath As Variant
    
    'AddToScanningPath = "N:\NDA\1_Add_To_Scanning\"
    
    AddToScanningPath = GetFolder
'cancel pressed
    If IsError(AddToScanningPath) Then Exit Sub
    
    AddToScanningFilename = Worksheets("Input").Range("A8").Value & "_" & Worksheets("Input").Range("B8").Value & "_" & Format$(Date, "mmddyyyy") & "_" & "Add_To_Scanning_VLAN_999" & ".ps1"


    Open AddToScanningPath & AddToScanningFilename For Output As #1
    For Each r In Range("B2:B200")
        sTemp = ""
        For Each c In r.Cells
            sTemp = sTemp & c.Text & Chr(9)
        Next c


        'Get rid of trailing tabs
        While Right(sTemp, 1) = Chr(9)
            sTemp = Left(sTemp, Len(sTemp) - 1)
        Wend
        Print #1, sTemp
    Next r
    Close #1
End Sub


Copy function to a standard module

Code:
Function GetFolder(Optional ByVal FolderPath As Variant) As Variant
    Dim Folder As FileDialog
    Dim Item As Variant
    
    If IsMissing(FolderPath) Then FolderPath = ThisWorkbook.Path
    Set Folder = Application.FileDialog(msoFileDialogFolderPicker)
    
    With Folder
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .ButtonName = "Select Folder"
        .InitialFileName = FolderPath
'cancel pressed
        If .Show <> -1 Then Item = CVErr(10): GoTo ExitFunction
'folder selected
        Item = .SelectedItems(1) & "\"
    End With
    
ExitFunction:
    GetFolder = Item
    Set Folder = Nothing
End Function

Function has option to specify default path otherwise the path for the workbook is used.

Hope helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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