VBA for pulling values from specific, non-contiguous cells in one workbook to another

beyounge

New Member
Joined
Dec 31, 2016
Messages
2
Hello,
I wanted to create a VBA that will pull the values from a closed workbook into the open workbook and from certain ranges that are non-contiguous. The worksheets are identical in layout. For example, I wanted to copy whatever values were in cells D10, D16:D26, D31:D41 (and the same row/cells in columns J, P, and V) from SHEETx in FILEx, and have the values pulled into the exact same cells/rows into SHEETx in FILEy. I would need the user to be prompted to find FILEx when the macro button is pushed so it knows which file the data is coming from, and it could be located in a different directory. Any experts out there?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Code:
Sub Another_Possibility()
    Dim wb1 As Workbook, wb2 As Workbook, i As Long
    Dim frFile, rngArray
    Application.ScreenUpdating = False
    Set wb1 = ActiveWorkbook
    rngArray = Array("D10", "D16:D26", "D31:D41", "J10", "J16:J26", "J31:J41", "P10", "P16:P26", "P31:P41", "V10", "V16:V26", "V31:V41")

    frFile = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
    , "Please select file to copy from")
    If frFile = False Then Exit Sub

    Set wb2 = Workbooks.Open(frFile)

    For i = LBound(rngArray) To UBound(rngArray)
        wb1.Sheets("Sheet3").Range(rngArray(i)).Value = wb2.Sheets("Sheet1").Range(rngArray(i)).Value
    Next i

    wb2.Close SaveChanges:=False

    Set wb2 = Nothing
    Set wb1 = Nothing
    Application.ScreenUpdating = True
End Sub

Change all references that need changing, like the sheet names, as required.
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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