Search code examples
vbaexcelgetopenfilename

Copy data from a selected excel file


I am trying to construct a macro that when run will allow me to select a given file and check the data in Column C of that selected file. I am very new to VBA and have only rudimentary skills. I have all the parts of my code working except for the portion where I pull in the data from the variable file and paste it into column A of my macro file for the review functions to perform.

I have cobbled together the below code to populate the data from Column C of any given selected file into Column A of the macro file from what I could piece together from searching through the site, but I am still getting an error 400 after selecting the file to open when running this Sub. Would appreciate any assistance with figuring this portion out.

Thanks!

Sub PopulateUploaderFunds()
'Pull in funds from uploader to be reviewed for custody and mirror accounts
Dim uploadfile As Variant
Dim uploader As Workbook
MsgBox ("Please select uploader file to be reviewed")
uploadfile = Application.GetOpenFilename()
If uploadfile = "False" Then
Exit Sub
End If
Workbooks.Open uploadfile
Set uploader = ActiveWorkbook
With uploader
    Application.CutCopyMode = False
    Range("C1").End(xlDown).Select
    Selection.Copy
End With
Windows("Test Mirror Macro Build Test.xlsm").Activate
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End Sub

Solution

  • It looks like a problem with how you are navigating between workbooks, try this:

    Sub PopulateUploaderFunds()
    'Pull in funds from uploader to be reviewed for custody and mirror accounts
    Dim uploadfile As Variant
    Dim uploader As Workbook
    Dim CurrentBook As Workbook
    
    Set CurrentBook = ActiveWorkbook
    MsgBox ("Please select uploader file to be reviewed")
    uploadfile = Application.GetOpenFilename()
        If uploadfile = "False" Then
            Exit Sub
        End If
    Workbooks.Open uploadfile
    Set uploader = ActiveWorkbook
    With uploader
        Application.CutCopyMode = False
        Range("C:C").Copy
    End With
    CurrentBook.Activate
    Sheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End Sub