Open Specific folder to select a file

SriDurga

New Member
Joined
Aug 30, 2022
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hi experts,
I need to open a specific folder path: "C:\Desktop\Current\Week" and prompt msg asking to "select a file to open".
After opening a file, few actions needs to be performed in that selected file for eg:"text to columns; delimiter and deleting column A " and file should be saved automatically.
Kindly help me with a VBA code.

Thanks in advance.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this code

VBA Code:
Sub OpenAndProcessFile()
    ' Prompt user to select a file to open
    Dim filePath As Variant
    filePath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", , "Select a file to open", "C:\Desktop\Current\Week.xlsx")
    If filePath = False Then Exit Sub ' User clicked Cancel

    ' Open selected file
    Workbooks.Open filePath

    ' Perform text to columns and delete column A
    With ActiveSheet
        .Columns("A:A").Delete Shift:=xlToLeft ' Delete column A
        .Range("A1").CurrentRegion.TextToColumns _
            Destination:=.Range("A1"), _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=True, _
            Space:=False, _
            Other:=False, _
            FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)) ' Text to columns with comma delimiter
    End With

    ' Save changes to file
    ActiveWorkbook.Save

End Sub
 
Upvote 0
Try this code

VBA Code:
Sub OpenAndProcessFile()
    ' Prompt user to select a file to open
    Dim filePath As Variant
    filePath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", , "Select a file to open", "C:\Desktop\Current\Week.xlsx")
    If filePath = False Then Exit Sub ' User clicked Cancel

    ' Open selected file
    Workbooks.Open filePath

    ' Perform text to columns and delete column A
    With ActiveSheet
        .Columns("A:A").Delete Shift:=xlToLeft ' Delete column A
        .Range("A1").CurrentRegion.TextToColumns _
            Destination:=.Range("A1"), _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=True, _
            Space:=False, _
            Other:=False, _
            FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)) ' Text to columns with comma delimiter
    End With

    ' Save changes to file
    ActiveWorkbook.Save

End Sub
Wow, that helps, but am not able to select the file in this path "C:\Desktop\Current\Week" instead it is opening documents folders and asking me to select a file.
I again need to browse this path (C:\Desktop\Current\Week) to select the file in "Week" folder, any suggestions pls
 
Upvote 0
send me the full code of what you are changed and used.
Sub Format_Files()


' Prompt user to select a file to open
Dim filePath As Variant
filePath = Application.GetOpenFilename("Excel Files (*.csv), *.csv", , "Select a file to open", "C:\Users\tamman\Desktop\PP\Dump\Daily PP.csv")
If filePath = False Then Exit Sub ' clicked Cancel

' Open selected file
Workbooks.Open filePath

' Perform text to columns and delete column
With ActiveSheet
ActiveCell.Offset(0, 2).Columns("A:A").EntireColumn.Select
Selection.Insert Shift:=xlToRight
ActiveCell.Offset(0, -1).Columns("A:A").EntireColumn.Select
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="@", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.Delete Shift:=xlToLeft
ActiveCell.Offset(0, -2).Range("A1").Select

End With

' Save changes to file
ActiveWorkbook.Save

End Sub
 
Upvote 0
Remove
Code:
filePath = Application.GetOpenFilename("Excel Files (*.csv), *.csv", , "Select a file to open", "C:\Users\tamman\Desktop\PP\Dump\Daily PP.csv")
Add
VBA Code:
filePath = "C:\Users\tamman\Desktop\PP\Dump\Daily PP.csv"
 
Upvote 0
Hi SriDurga,

AFAIR the path to Desktop should be part of each individual user. So maybe try the alteration for the first part of the code:

VBA Code:
Sub OpenAndProcessFile()
  Dim strPathFile As String
  Dim strInitial As String
  Dim oWSHShell As Object
 
  Set oWSHShell = CreateObject("WScript.Shell")
  strInitial = oWSHShell.SpecialFolders("Desktop") & "\Current\Week\"
  Set oWSHShell = Nothing
 
  With Application.FileDialog(msoFileDialogFilePicker)
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xl*"
    .InitialFileName = strInitial
    .AllowMultiSelect = True
  If .Show = -1 Then
    strPathFile = .SelectedItems(1)
  Else
    Exit Sub
  End If
  End With
 
  ' Open selected file
  Workbooks.Open strPathFile

And in the future please use code-tags to display your procedures as described in How to Post Your VBA Code

Ciao,
Holger
 
Upvote 0
Solution
Hi SriDurga,

AFAIR the path to Desktop should be part of each individual user. So maybe try the alteration for the first part of the code:

VBA Code:
Sub OpenAndProcessFile()
  Dim strPathFile As String
  Dim strInitial As String
  Dim oWSHShell As Object
 
  Set oWSHShell = CreateObject("WScript.Shell")
  strInitial = oWSHShell.SpecialFolders("Desktop") & "\Current\Week\"
  Set oWSHShell = Nothing
 
  With Application.FileDialog(msoFileDialogFilePicker)
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xl*"
    .InitialFileName = strInitial
    .AllowMultiSelect = True
  If .Show = -1 Then
    strPathFile = .SelectedItems(1)
  Else
    Exit Sub
  End If
  End With
 
  ' Open selected file
  Workbooks.Open strPathFile

And in the future please use code-tags to display your procedures as described in How to Post Your VBA Code

Ciao,
Holger
Thank you so much, it works 😇
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

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