Macro to open specific folder

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
Hi Everyone,

I have the macro below on a button in a worksheet and I want it to open "C:\Users\HOME\Dropbox\DSR DAILY IMPORT" so we can select a file
which it does BUT it also opens "My Documents" folder as well? I'm not seeing why that is happening?

Thank you!


Code:
​[COLOR=#333333]Sub Macro1()[/COLOR]<code style="font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; line-height: 12px;">'
' Macro1 Macro()

Dim MyFolder As String
MyFolder = "C:\Users\HOME\Dropbox\DSR DAILY IMPORT"
ActiveWorkbook.FollowHyperlink MyFolder
Dim wkbCrntWorkBook As Workbook
Dim wkbSourceBook As Workbook
Dim rngSourceRange As Range
Dim rngDestination As Range
Set wkbCrntWorkBook = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.Filters.Clear
.Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa; *.csv"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
Workbooks.Open .SelectedItems(1)
Set wkbSourceBook = ActiveWorkbook
Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1:I400", Type:=8)
wkbCrntWorkBook.Activate
Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
rngSourceRange.Copy rngDestination
wkbSourceBook.Close False
End If
End With
End Sub</code>
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,638
Try:
Code:
Sub Buns()
    Dim MyFolder As FileDialog, FileChosen As Integer, FileName As String, wkbSourceBook As Workbook, wkbCrntWorkBook As Workbook
    Dim rngSourceRange As Range, rngDestination As Range
    Set wkbCrntWorkBook = ActiveWorkbook
    Set MyFolder = Application.FileDialog(msoFileDialogFilePicker)
    MyFolder.InitialFileName = "C:\Users\HOME\Dropbox\DSR DAILY IMPORT\"
    FileChosen = MyFolder.Show
    FileName = MyFolder.SelectedItems(1)
    Set wkbSourceBook = Workbooks.Open(FileName)
    Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1:I400", Type:=8)
    wkbCrntWorkBook.Activate
    Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
    rngSourceRange.Copy rngDestination
    wkbSourceBook.Close False
End Sub
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
Mumps,

ABSOLUTE Genius!! I've been beating my noggin against the wall for a day on that macro.

Thank you very much!!
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138

ADVERTISEMENT

Hi Mumps,

The macro below is very similar to the one from yesterday and I tried editing it but no luck. Wonder if you might have a look at it please?
There are multiple sheets in this workbook so the destination sheets are key.

Thank you!!



Code:
Sub IMPORT_PLUs()    Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
    Dim rngSourceRange As Range
    Dim rngDestination As String
    Set wkbCrntWorkBook = ActiveWorkbook
    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
        .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa; *.csv"
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            Workbooks.Open .SelectedItems(1)
            Set wkbSourceBook = ActiveWorkbook
            Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1:H500", Type:=8)
            wkbCrntWorkBook.Activate
            rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1")
            rngSourceRange.Copy Sheets("POS IMPORT").Range(rngDestination)
            Sheets("POS IMPORT").Columns.AutoFit
            wkbSourceBook.Close False
        End If
    End With
End Sub
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,638
Please explain in detail how it is not working for you. Do you get any error messages. If so what is the message and which line of code is highlighted when you click "Debug"?
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138

ADVERTISEMENT

Hi Mumps,
Sorry I probably wasn't clear. The code I posted last works with the exception of opening the folder we want to pick the file from
which is "C:\Users\HOME\Dropbox\LOTTERY-PLU DAILY IMPORT".

Thank you!
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,638
Does the macro I suggested not work for you? In that macro you can set the desired folder with this line of code:
Code:
MyFolder.InitialFileName = "C:\Users\HOME\Dropbox\LOTTERY-PLU DAILY IMPORT\"
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
Hi Mumps,

The macro is hanging up on the the 2 lines

Code:
"rngSourceRange.Copy Sheets("POS IMPORT").Range(rngDestination)  
  Sheets("POS IMPORT").Columns.AutoFit"
 
Last edited:

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,638
This seems to work:
Code:
Sub Buns()
    Dim MyFolder As FileDialog, FileChosen As Integer, FileName As String, wkbSourceBook As Workbook, wkbCrntWorkBook As Workbook
    Dim rngSourceRange As Range, rngDestination As Range
    Set wkbCrntWorkBook = ActiveWorkbook
    Set MyFolder = Application.FileDialog(msoFileDialogFilePicker)
    MyFolder.InitialFileName = "C:\Users\HOME\Dropbox\LOTTERY-PLU DAILY IMPORT\"
    FileChosen = MyFolder.Show
    FileName = MyFolder.SelectedItems(1)
    Set wkbSourceBook = Workbooks.Open(FileName)
    Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1:I400", Type:=8)
    wkbCrntWorkBook.Activate
    Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
    rngSourceRange.Copy rngDestination
    Sheets("POS IMPORT").Columns.AutoFit
    wkbSourceBook.Close False
End Sub
 

Forum statistics

Threads
1,147,481
Messages
5,741,400
Members
423,657
Latest member
Medrok2021

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
Top