Posted before and got great help...hope someone can come through for me again...
Have some VBA code that imports a selection from a closed workbook. Works great...I highlight my range in my current workbook, hit the macro button, and it imports the selected range from a closed workbook into my current workbook.
I am basically trying to apply the same principle in a new application, however instead of importing a selection, I want to import multiple ranges (they are always the same) from a closed workbook into my workbook.
I have tried every way I know how to modify the code to do this (but I'm a beginner). Gave up after trying and searching different posts. Can't seem to get it right.
Instead of selecting a range, I want to import these ranges when I run the macro (H39:N41, H48:N50, H54:N56)
Can someone help me make the changes I need to make to the code..
Sub TestCopyRangeFromWB()
Dim objTargetRange As Range, strSourceWB As String, strSourceWS As String
' determine the source workbook full filename
If Len(ThisWorkbook.Path) = 0 Then Exit Sub ' this workbook is not saved
strSourceWB = ThisWorkbook.Path & Application.PathSeparator & "Copy of " & ThisWorkbook.Name
' determine the selected cell range
On Error Resume Next
Set objTargetRange = Selection
On Error GoTo 0
If objTargetRange Is Nothing Then Exit Sub ' no cell range selected
' determine the worksheet name
strSourceWS = objTargetRange.Parent.Name
Application.ScreenUpdating = False
If Not CopyRangeFromWB(strSourceWB, strSourceWS, objTargetRange.Address(False, False, xlA1), objTargetRange) Then
MsgBox "Failed to copy information from " & strSourceWB, vbInformation
End If
Application.ScreenUpdating = True
Set objTargetRange = Nothing
End Sub
Function CopyRangeFromWB(strSourceWB As String, varSourceWS As Variant, strSourceRange As String, _
rngTarget As Range, Optional blnCopyFormats As Boolean = False) As Boolean
Dim strWB As String, wb As Workbook, ws As Worksheet, rng As Range
Dim p As Long, blnCloseWB As Boolean
' copies the values from a workbook/worksheet range to a given target range
' varSourceWS can be a worksheet name or a worksheet index number
' it is recommended to turn off screen updating before using this function
CopyRangeFromWB = False
' validate input
If Len(strSourceWB) = 0 Then Exit Function
If Len(varSourceWS) = 0 Then Exit Function
If Len(strSourceRange) = 0 Then Exit Function
If rngTarget Is Nothing Then Exit Function
Application.StatusBar = "Copying data from " & strSourceWB & "..."
blnCloseWB = True
strWB = vbNullString
p = InStrRev(strSourceWB, Application.PathSeparator)
If p > 0 Then
strWB = Mid(strSourceWB, p + 1)
End If
If Len(strWB) > 0 Then
On Error Resume Next
Set wb = Workbooks(strWB) ' check if workbook is open
On Error GoTo 0
End If
If wb Is Nothing Then
On Error Resume Next
Set wb = Workbooks.Open(strSourceWB, , True) ' open a closed workbook, read only
On Error GoTo 0
Else
blnCloseWB = False
End If
If Not wb Is Nothing Then
On Error Resume Next
Set ws = wb.Worksheets(varSourceWS) ' get the source worksheet
On Error GoTo 0
If Not ws Is Nothing Then ' found the source worksheet
On Error Resume Next
Set rng = ws.Range(strSourceRange) ' get the source range
On Error GoTo 0
If Not rng Is Nothing Then
rng.Copy
rngTarget.PasteSpecial xlPasteValues
If blnCopyFormats Then
rngTarget.PasteSpecial xlPasteFormats
End If
Application.CutCopyMode = False
Set rng = Nothing
CopyRangeFromWB = True
End If
Set ws = Nothing
End If
If blnCloseWB Then
wb.Close False ' close workbook without saving any changes
End If
Set wb = Nothing
End If
Application.StatusBar = False
End Function
Bookmarks