Hi All,
I have a Userform that sucessfully populates a pair of Source Combo Boxes and a pair of Target Combo Boxes with open Workbook and Worksheet names that I am having trouble using in a VBA formulas.
cboSourceBook, cboSourceSheet, cboTargetBook and cboTargetSheet are the names of the properly populated combo boxes.
The SetSourceWorkbook .... Line generates a Run-time error '424': Object Required error.
Private Sub cmdImport_Click()
cpyRange
End Sub
Sub cpyRange()
Dim SourceSheet As Excel.Worksheet
Dim SourceWorkbook As Excel.Workbook
Dim RangeName As String
Dim TargetWorkbook As Excel.Workbook
Dim TargetSheet As Excel.Worksheet
Application.ScreenUpdating = False
Set SourceWorkbook = cboSourceBook.Value 'Instead of = Workbooks.Open("C:\Users\~User\~Folder\~File.xlsm")
Set SourceSheet = cboSourceSheet.Value
SourceSheet.Range("Ride_Ht_LF").Copy
Set TargetWorkbook = ThisWorkbook
Set TargetSheet = cboTargetSheet.Value
TargetSheet.Range("Ride_Ht_LF").PasteSpecial Paste:=xlPasteValues
'
'
SourceWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
MsgBox ("done")
End Sub
This code works as a macro but I wanted to set the Source and Target Workbooks and Worksheets with the values in the Combo Boxes and not have to use the "Workbooks.Open" and drop the "SourceWorkbook.Close" statements.
Sub cpyRange()
Dim SourceSheet As Excel.Worksheet
Dim SourceWorkbook As Excel.Workbook
Dim RangeName As String
Dim TargetWorkbook As Excel.Workbook
Dim TargetSheet As Excel.Worksheet
Application.ScreenUpdating = False
Set SourceWorkbook = Workbooks.Open("C:\Users\~User\~Folder\~File.xlsm")
SourceWorkbook.Sheets("S1").Range("Ride_Ht_LF").Copy
Set TargetWorkbook = ThisWorkbook
Set TargetSheet = TargetWorkbook.Sheets("Interface")
TargetSheet.Range("Ride_Ht_LF").PasteSpecial Paste:=xlPasteValues
SourceWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
MsgBox ("done")
End Sub
Been trying many options to understand why cboSourceBook.Value isn't an Object but I'm lost.
TIA
Bud
Bookmarks