radsok8199
New Member
- Joined
- Dec 4, 2020
- Messages
- 24
- Office Version
- 2016
- Platform
- Windows
- MacOS
- Mobile
Dear VBA Masters.
I tried to modified code I am using for uploading specific worksheet from another workbook from any location. So far all was quite easy and seems to work fine. It works fine when correct file is being picked. If user will pick workbook that does not contain specific sheet Debug window pops up. I would likeVBA to check first if source workbook contains specific worksheet and then carry on with code, else to call Import_Requirements again. Also if worksheet will be uploaded into target workbook i need to display name of source book in worksheet "Real Time Status" in range "E1"
All my efforts with "If else" failed and just gave up
I tried to modified code I am using for uploading specific worksheet from another workbook from any location. So far all was quite easy and seems to work fine. It works fine when correct file is being picked. If user will pick workbook that does not contain specific sheet Debug window pops up. I would likeVBA to check first if source workbook contains specific worksheet and then carry on with code, else to call Import_Requirements again. Also if worksheet will be uploaded into target workbook i need to display name of source book in worksheet "Real Time Status" in range "E1"
All my efforts with "If else" failed and just gave up
VBA Code:
Public Sub Import_Requirements()
Application.ScreenUpdating = False
'Get workbook...
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ThisWorkbook
' get the customer workbook
Dim Filter As String
Filter = "Text files (*.xlsb),*.xlsb,(*.xlsx),*.xlsx"
Dim Caption As String
Caption = "Please select input Requirements file - only xlsb & xlxs files !!!"
Dim Ret As Variant
Ret = Application.GetOpenFilename(Filter, , Caption)
If VarType(Ret) = vbBoolean And Ret = False Then Exit Sub
' Status bar msg
oldStatusBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
Application.StatusBar = "Uploading Requirements ..."
Dim wb As Workbook
Set wb = Workbooks.Open(Ret)
'copy into a specific worksheet in your target workbook
wb.Worksheets("Requirements").UsedRange.Copy targetWorkbook.Worksheets("Requirements").Range("A1")
'close opened workbook without saving
wb.Close SaveChanges:=False
Sheets("Real Time Status").Range("A1:D2").Merge
Sheets("Real Time Status").Range("A1:D2").Interior.ColorIndex = 10
Sheets("Real Time Status").Range("A1:D2").HorizontalAlignment = xlCenter
Sheets("Real Time Status").Range("A1:D2").VerticalAlignment = xlCenter
Sheets("Real Time Status").Range("A1:D2").Font.ColorIndex = 1
Sheets("Real Time Status").Range("A1:D2").Font.Name = "Arial"
Sheets("Real Time Status").Range("A1:D2").Font.Bold = True
Sheets("Real Time Status").Range("A1:D2").Font.Size = 11
Sheets("Real Time Status").Range("A1:D2").Value = "Requirements uploaded"
Application.ScreenUpdating = True
'End Status bar
Application.StatusBar = False
Application.DisplayStatusBar = oldStatusBar
Result = MsgBox("Requirements uploaded succesfully - please load Extract File", vbOKCancel + vbQuestion)
If Result = vbOK Then
Call Import_Extract
Else
ActiveWorkbook.Close
End If
End Sub