VBA to get folder name

imskin

New Member
Joined
Mar 11, 2013
Messages
21
I want to open and save multiple csv as excel file. Below is the macro that i found. but this macro only can use in my own computer as it is hard coded to "e:\new foler\" and "c:\testing2\". How can make mypath and mypath2 as variant with get application.foldername?

Code:
Sub TransformCSVToXls()
Application.DisplayAlerts = False
MyFile = ActiveWorkbook.Name
myPath = "[FONT=arial black]E:\New Folder\[/FONT]"
mypath2 = "[FONT=arial black][B]C:\Testing2\[/B][/FONT]"
WorkFile = Dir(myPath & "*.CSV")


Do While WorkFile <> ""
   Application.StatusBar = "Now working on " & WorkFile
   Workbooks.Open Filename:=myPath & WorkFile
    ActiveWorkbook.SaveAs Filename:=mypath2 & _
      Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4), FileFormat:=xlNormal
   ActiveWorkbook.Close
   Windows(MyFile).Activate
   WorkFile = Dir()
Loop


Application.StatusBar = False


End Sub[CODE]
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
Sub TransformCSVToXls()
    Application.DisplayAlerts = False
    MyFile = ActiveWorkbook.Name
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    InitialFoldr$ = "E:\" '<<< Startup folder
    With Application.FileDialog(msoFileDialogFolderPicker) 'User input for folder to look at
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder source"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        mfolder = .SelectedItems(1) & "\"
    End With
    myPath = "mfolder
    InitialFoldr$ = "C:\" '<<< Startup folder
    With Application.FileDialog(msoFileDialogFolderPicker) 'User input for folder to look at
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder target"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        mfolder = .SelectedItems(1) & "\"
    End With
mypath2 = mfolder
WorkFile = Dir(myPath & "*.CSV")
Do While WorkFile <> ""
   Application.StatusBar = "Now working on " & WorkFile
   Workbooks.Open Filename:=myPath & WorkFile
    ActiveWorkbook.SaveAs Filename:=mypath2 & _
      Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4), FileFormat:=xlNormal
   ActiveWorkbook.Close
   Windows(MyFile).Activate
   WorkFile = Dir()
Loop
Application.StatusBar = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,055
Latest member
excelhelp12345

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