Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

How can I use this VBA code to import multiple selections from multiple sheets into an identical location in another workbook?

$
0
0

The below code allows the user to select a data range in another workbook and then paste it in another selection in the starting workbook.

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"
    .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", Type:=8)
        wkbCrntWorkBook.Activate
        Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
        rngSourceRange.Copy rngDestination
        rngDestination.CurrentRegion.EntireColumn.AutoFit
        wkbSourceBook.Close False
    End If
End With

However, the location of the data I am importing is in the exact same location but it is on different sheets within the imported workbook. All of the sheets are the same in both workbooks but the idea is to import new data into the old workbook because there is embedded formulas within the old workbook. How can I modify this Macro so that all of the sheets can be imported at the same time & replace the old data in the old workbook?

I tried altering the code to directly copy the data range of the first sheet and paste it into the same location in the old workbook but ended up with an "Subscript Out of Range" error. The idea was to create a loop to do this to the respective locations as a loop so that the macro would copy and paste all of the new values from each sheet (There are 8 sheets in both) into the old workbook. See below:

 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
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count > 0 Then
        Workbooks.Open .SelectedItems(1)
        Set wkbSourceBook = ActiveWorkbook
        Set rngSourceRange = ActiveWorkbook.Sheets("Sheet1").Range("B4:K53").Value
        wkbCrntWorkBook.Activate
        Set rngDestination = ActiveWorkbook.Sheets("Sheet1").Range("B4:K53").Value
        wkbCrntWorkBook.Activate
        rngSourceRange.Copy rngDestination
        rngDestination.CurrentRegion.EntireColumn.AutoFit
        wkbSourceBook.Close False
    End If
End With

I apologize for being long-winded but wanted to show that I did make an effort to solve this. Please help!


Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>