Is there a way of amending the VBA code below to do the following: If the last active…
- User
Is there a way of amending the VBA code below to do the following: If the last active sheet was 'PREQUEST' then use this line in the code 'filePath = ThisWorkbook.Path & "\zzzz ServProvDocs\PurchaseRequest.docm" 'New docm for this code call' Else use this line 'filePath = ThisWorkbook.Path & "\zzzz ServProvDocs\TaskRequest.docm" 'Original docm for this code call
Public Sub TaskRequest()
'USED TO OPEN WORD DOC FOR TASK REQUEST - DO NOT DELETE
If Not Module9.sheetJobRequestActive Then Exit Sub ' if TRUE it allows the module to run 'USED TO OPEN WORD DOC FOR TASK REQUEST - DO NOT DELETE
codeDim wdApp As Object Dim wdDoc As Object Dim WsdShell As Object Set wdApp = CreateObject("Word.Application") Dim filePath As String 'filePath = ThisWorkbook.Path & "\zzzz ServProvDocs\TaskRequest.docm" 'Original docm for this code call filePath = ThisWorkbook.Path & "\zzzz ServProvDocs\PurchaseRequest.docm" 'New docm for this code call Set wdDoc = wdApp.Documents.Open(filePath) wdApp.Visible = True'wordApp.Visible = True 'Application.Wait (Now + TimeValue("0:00:01")) 'doc.Activate
Set WsdShell = CreateObject("WScript.Shell") WsdShell.AppActivate wdApp.Caption End Sub
- Assistant
You can amend the VBA code to check if the last active sheet was 'PREQUEST' and set the file path accordingly. Here's how you can modify the code:
vbaPublic Sub TaskRequest() ' Check if the last active sheet was 'PREQUEST' Dim lastActiveSheet As String lastActiveSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name Dim filePath As String If lastActiveSheet = "PREQUEST" Then filePath = ThisWorkbook.Path & "\zzzz ServProvDocs\PurchaseRequest.docm" ' New docm for 'PREQUEST' Else filePath = ThisWorkbook.Path & "\zzzz ServProvDocs\TaskRequest.docm" ' Original docm for other sheets End If ' Open Word document Dim wdApp As Object Dim wdDoc As Object Set wdApp = CreateObject("Word.Application") Set wdDoc = wdApp.Documents.Open(filePath) wdApp.Visible = True ' Activate Word application Dim WsdShell As Object Set WsdShell = CreateObject("WScript.Shell") WsdShell.AppActivate wdApp.Caption End Sub - User
I tried the code but for some reason it appears not to have detected the previous excel sheet that was open. When I am in sheet 'PREQUEST', I have a VBA code that activates another excel sheet. When this other sheet is activated, it triggers the VBA event 'Public Sub TaskRequest()'. I want the event 'Public Sub TaskRequest()' to identify if the sheet 'PREQUEST' instigated the sheet activation.
- Assistant
To identify if the sheet 'PREQUEST' instigated the activation of the 'TaskRequest' code, you can pass a parameter to the 'TaskRequest' subroutine when calling it from the other sheet.
Here's an example of how you can modify your code to achieve this:
In the 'PREQUEST' sheet module:
vbaPublic Sub ActivateOtherSheet() Application.Run "TaskRequest", "PREQUEST" End SubIn the 'TaskRequest' module:
vba