I've got 2 different UserForms (UserForm & DataMap) with multiple modules in them.
1. Here's the code of my first Form UserForm. Where I'm allowing the user to select a spreadsheet and assign the file name to Importworkbook.
2. Here's the code for my second Form DataMap, where I'm replicating the same set of codes from above. Instead, I just need to get the name of Importworkbook from my earlier code which is assigned in different userform/module. Please assist.
1. Here's the code of my first Form UserForm. Where I'm allowing the user to select a spreadsheet and assign the file name to Importworkbook.
VBA Code:
Private Sub UserForm_Initialize()
'DataMap.Show vbModeless
Dim LR, IB As Long
Dim FileLocation As String
Sheets("Input").Select
Range("A1").Select
Sheets("Sheet3").Range("R1").Value = 0
file = ActiveWorkbook.Name
LR = Cells(Rows.Count, 1).End(xlUp).Row
If LR = 1 Then Else GoTo ok
[B]FileLocation = Application.GetOpenFilename("(*.xlsx),")
If FileLocation = "False" Then
MsgBox "No file selected to import.", 48
Unload Me
Exit Sub
End If
End If
Set Importworkbook = Workbooks.Open(Filename:=FileLocation)[/B]
If Importworkbook.Sheets.Count > 1 Then
reIB:
IB = Application.InputBox("Enter worksheet number", "Worksheet selection", , , , , , 1)
If IB > Sheets.Count Then
MsgBox "Invalid Sheet Input, Try Again.", 48, "Entry Required"
GoTo reIB
Else
Sheets(IB).Select
End If
GoTo hi
Else: End If
hi:
ActiveSheet.Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Copy
Workbooks(file).Activate
Sheets("Sheet3").Select
Range("P2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Application.CutCopyMode = False
Range("P1").Select
ok:
End Sub
2. Here's the code for my second Form DataMap, where I'm replicating the same set of codes from above. Instead, I just need to get the name of Importworkbook from my earlier code which is assigned in different userform/module. Please assist.
VBA Code:
Sub DataImport()
Dim LR As Long
Dim FileLocation As String
Dim A, A1 As Integer
LR = Cells(Rows.Count, 1).End(xlUp).Row
If LR = 1 Then Else GoTo ok
[B] FileLocation = Application.GetOpenFilename("(*.xlsx),")
If FileLocation = "False" Then
MsgBox "No file selected to import.", 48
Exit Sub
End If
Set Importworkbook = Workbooks.Open(Filename:=FileLocation)[/B]
ThisWorkbook.Worksheets(2).Activate
SH = ThisWorkbook.Worksheets(4).Cells(Rows.Count, 19).End(xlUp).Row
For A = 2 To SH
'MsgBox DataMap!mastrImportworkbook
Importworkbook.Worksheets(1).Range(Sheets("Sheet3").Range("T" & A).Value).Copy ThisWorkbook.Worksheets(2).Cells(1, A - 1)
Next A
Application.ScreenUpdating = True
ThisWorkbook.Worksheets(3).Range("A1:u1").Copy
ThisWorkbook.Worksheets(2).Range("A1:u1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Importworkbook.Close
Application.ScreenUpdating = True
ThisWorkbook.Worksheets(4).Select
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("S2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("R1").Select
Selection.ClearContents
ThisWorkbook.Worksheets(2).Select
Range("A1").Select
ok:
UserForm1.Show
End Sub