VBA to open another workbook and copy data - record the location of other workbook

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have code that allows the user to open another workbook and to copy data from that workbook into the one they currently have open.

Is there a way to modify this code so that when they select the other workbook it "pastes" the address in Sheets("Tracker").Range("C4")?

Code:
Sub Get_Data()

Retriev_BOETab Macro
'
'
    ' Retrieve Current Workbook Name
    Dim Mtrl_Resource As Workbook
    Set Mtrl_Resource = ThisWorkbook

    Application.ScreenUpdating = False
    
    ' Open Dialog Box to Select File to Copy From & Save it's Name in a Variable
    MsgBox "Retrieve the 'Material Estimates' tab from the Material Cost Estimates File - Select the MCE File."
    Dim filePicker As Office.FileDialog
    Set filePicker = Application.FileDialog(msoFileDialogFilePicker)
    With filePicker
        .Filters.Clear
        .Title = "Select an Excel File"
        .AllowMultiSelect = False
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1

        
        Dim selectedFile As String
        
        If .Show = True Then
            selectedFile = .SelectedItems(1)
        End If
    End With
    
    ' Open Selected File & Copy Data
    If selectedFile <> "" Then
        Dim MaterialEstBook As Workbook
        Set MaterialEstBook = Workbooks.Open(selectedFile, ReadOnly:=True)
    
        ' Copy & Paste-Value: BIDDER_INPUT Tab
        Workbooks(MaterialEstBook.Name).Worksheets("BIDDER_INPUT").Range("A1:AC50000").Copy
        Workbooks(Mtrl_Resource.Name).Worksheets("BIDDER_INPUT").Range("A1").PasteSpecial _
            Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    
        ' Clear Clipboard
        Application.CutCopyMode = False
    
        ' Close Extra Workbook
        MaterialEstBook.Close SaveChanges:=False
        Set MaterialEstBook = Nothing
    End If

End Sub

Thank You!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
My apologies. You are absolutely right, I should have responded. No excuses.
 
Upvote 0
I took the liberty of rewriting your code slightly as I would have it if it was my project, I hope you don't mind :)
VBA Code:
Option Explicit
Sub Get_Data_2()

    ' Retrieve Current Workbook Name
    Dim MaterialEstBook As Workbook, FileName

    ' Open Dialog Box to Select File to Copy From & Save it's Name in a Variable
    MsgBox "Retrieve the 'Material Estimates' tab from the Material Cost Estimates File - Select the MCE File."
    
    FileName = Application.GetOpenFilename _
    (filefilter:="Excel files (*.xl*),*.xl*", MultiSelect:=False)
    If FileName = False Then Exit Sub
    Set MaterialEstBook = Workbooks.Open(FileName, ReadOnly:=True)
    
    ' *** This will put the address of the opened file in Tracker sheet cell C4 ***
    ThisWorkbook.Sheets("Tracker").Range("C4") = Application.ActiveWorkbook.FullName
    
    ' Copy & Paste-Value: BIDDER_INPUT Tab
    MaterialEstBook.Worksheets("BIDDER_INPUT").Range("A1:AC50000").Copy
    ThisWorkbook.Worksheets("BIDDER_INPUT").Range("A1").PasteSpecial _
    Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    
    ' Close Extra Workbook
    MaterialEstBook.Close 0

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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