Hi all
I would like to open another workbook & retrieve a worksheet equal to a cell value using VBA.
I have two codes which each do part of the job but I’m having trouble stitching them together.
The following code opens the workbook & retrieves the sheet but requires the specific file name, however the file name changes each time the parent file is accessed -
The next code opens the latest version of the relevant file but will not import the required worksheet with the code copied from above.
I tried adding the following code - after 'Workbooks.Open MyPath & LatestFile' - along with the variables from/to the above to copy the relevant sheet but trips at the second line –
"Run-time error “91”
Object variable or With block variable not set."
I have also tried (the code button has stopped working?)
With MyFile
.Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
End With
As well as
With MyFile
wb..Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
End With
Both of these produce the following error –
"Compile error:
With object must be user-defined type, Object or Variant"
& also tried
With wb
.Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count
Which causes a "Run-time Automation error."
I am sure the solution is fairly straightforward & equally sure it will be a long time before I manage to work it out.
Any help is much appreciated.
With thanks,
Mark
I would like to open another workbook & retrieve a worksheet equal to a cell value using VBA.
I have two codes which each do part of the job but I’m having trouble stitching them together.
The following code opens the workbook & retrieves the sheet but requires the specific file name, however the file name changes each time the parent file is accessed -
Code:
Sub OpenWorkbookCopySheet()
Dim wb As Workbook
Dim activeWB As Workbook
Dim FilePath As String
Dim oWS As String
Set activeWB = Application.ActiveWorkbook
FilePath = "C:\Users\15309mng\Documentum\Viewed\Labour report 2013_4.xlsx"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
oWS = Sheets(1).Range("A1").Value
Set wb = Application.Workbooks.Open(FilePath)
wb.Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
activeWB.Activate
wb.Close False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
The next code opens the latest version of the relevant file but will not import the required worksheet with the code copied from above.
Code:
Sub OpenLatestFile()
Dim wb As Workbook
Dim activeWB As Workbook
Dim oWS As String
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Set activeWB = Application.ActiveWorkbook
MyPath = "C:\Users\15309mng\Documentum\Viewed\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "Labour report*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub
Code:
oWS = Sheets(1).Range("A1").Value
wb.Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
activeWB.Activate
wb.Close False
"Run-time error “91”
Object variable or With block variable not set."
I have also tried (the code button has stopped working?)
With MyFile
.Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
End With
As well as
With MyFile
wb..Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count)
End With
Both of these produce the following error –
"Compile error:
With object must be user-defined type, Object or Variant"
& also tried
With wb
.Worksheets(oWS).Copy After:=activeWB.Sheets(activeWB.Sheets.Count
Which causes a "Run-time Automation error."
I am sure the solution is fairly straightforward & equally sure it will be a long time before I manage to work it out.
Any help is much appreciated.
With thanks,
Mark