I open a workbook by using TASK MANAGER. Sometimes the previous task is not completed i.e. the macro is already running in another workbook.
I want to check if any other workbook is already open or running macro; close current opened workbook by task manager.
I have simple code as per below:
If thisworkbook.name <> activeworkbook.name then
call sendemail ' don't need code
else
Call Run_Dashboard
End if
Dim wb as Workbook
On Error Resume Next '//this is VBA way of saying "try"'
Set wb = Application.Workbooks(wbookName)
If err.Number = 9 then '//this is VBA way of saying "catch"'
'the file is not opened...'
End If
Function Is_Workbook_Open(byval WB_Name as String) as Boolean
Dim WB as Workbook
For each WB in Application.Workbooks
if WB.Name = WB_Name then
Workbook_Open = True
Exit Function
End if
Next WB
End Function
Answers True if the Workbook is opened, no error handling.
Related
Currently making a quick macro that opens a bunch of other workbooks in new instances:
Sub open_files()
Dim Path As String
Dim Fname As String
Dim xlApp As Object
MyFiles = Dir("C:\my_folder\*xls*")
Do While MyFiles <> ""
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open ("C:\my_folder\" & MyFiles)
MyFiles = Dir
Loop
End Sub
This works fine on sheets that don't have any ExcelAnt functions in Workbook_Open, but for those that do, I get a popup that says: "Run-time error '1004': cannot run the macro 'Connect'. The macro may not be available in this workbook or all macros may be disabled. "
I've tried forcing in the add-in before running the "connect" part of the code but to no avail.
Public Sub Workbook_Open()
Dim TestWkbk As Workbook
Set TestWkbk = Nothing
On Error Resume Next
Set TestWkbk = Workbooks("ExcelAnt-AddIn64.xll")
On Error GoTo 0
If TestWkbk Is Nothing Then
Set TestWkbk = Workbooks.Open("C:\ExcelAnt\ExcelAnt-AddIn64.xll")
End If
Dim hostenv As String
hostenv = Left(Environ("computername"), 3)
Application.Run "Connect", "prd"
End Sub
To clarify, the sheet if opened manually works fine.
Any help would be appreciated. Thanks in advance.
Use RegisterXLL with the new instance of Excel (xlApp).
xlApp.RegisterXLL "C:\ExcelAnt\ExcelAnt-AddIn64.xll"
I have a function that looks for open workbooks and pulls them into a master workbook. It works just fine when I'm importing a workbook created with a modern version of office, but it doesn't seem to detect workbooks that open in compatibility mode. Only the left 24 characters are constant for the workbook in question.
For a wide variety of reasons I've gone over in other posts downloading the workbook in question is not an option.
Here is the function.
Public Sub FindReport()
Debug.Print "Finding Report"
On Error GoTo Failed
Dim rName() As String
Dim wb As Workbook
Dim tWb As Workbook
rName(0) = "Case Detail"
rName(1) = "Disability_Claim_Status_"
'rName(2) = "placeholder"
For Each wb In Workbooks
'This line gives no output when I have the function try to find a workbook
'that has opened in compatibility mode
Debug.Print wb.Name
If Left(wb.Name, 11) = rName(0) Then
Set tWb = wb
ImportReport tWb
tWb.Close
CaseFAS
Exit For
End If
If Left(wb.Name, 24) = rName(1) Then
Set tWb = wb
ImportReport tWb
tWb.Close
'CaseFAS
Exit For
End If
'If Left(wb.Name, 11) = rName(2) Then
' Set tWb = wb
' ImportReport tWb
' tWb.Close
' 'CaseFAS
' Exit For
'End If
Next wb
Failed:
End Sub
EDIT for clarification:
I had another version of this code that pulled in a specific worksheet that opens from a website, now that I need to expand it to handle another sheet I modified the declarations accordingly, and screwed up declaring the array.
Before assuming something complicated is the problem make sure you didn't screw up something basic!
Here is the answer to my question....
Declare and Initialize String Array in VBA
I have written macro which saves current workbook after every ten second,
Option Explicit
Public ONTIMER_S As Date
Public Sub SaveBook()
ThisWorkbook.Save
ONTIMER_S = Now() + TimeValue("00:00:10")
Application.OnTime ONTIMER_S, "SaveBook"
End Sub
this works fine for one workbook, I want to run this macro on multiple workbooks, when I try to add this macro to any other workbook I get error " ambigous name detected Savebook "
Is there any way to run this macro on multiple workbooks at same time, either by writing this macro to each workbook, or somehow running this macro on multiple workbooks
You can set n workbooks in one main code in one main workbook like below
Option Explicit
Public ONTIMER_S As Date
Public Sub SaveBook()
dim wb1 as workbook, wb2 as workbook
dim name1 as string, name2 as string
name1 ="name1"
name2 = "name2"
if IsWorkBookOpen(name1 )=true then
set wb1= wborkbooks(name1 )
wb1.save
end if
if IsWorkBookOpen(name2 )=true then
set wb2= wborkbooks(name2 )
wb2.save
end if
set wb1 =nothing
set wb2 = nothing
ONTIMER_S = Now() + TimeValue("00:00:10")
Application.OnTime ONTIMER_S, "SaveBook"
End Sub
function to check is wb open
Function IsWorkBookOpen(Name As String) As Boolean
Dim xWb As Workbook
On Error Resume Next
Set xWb = Application.Workbooks.Item(Name)
IsWorkBookOpen = (Not xWb Is Nothing)
End Function
The following code used to work, but now returns a Runtime Error 1004.
Sub ResizeListObject_DateRange()
' Do Some things ...
Call HubLink.HubLaunch
Application.Run "'" & MacroHub & "'!" & "ResizeListObject", TableName, LstRow
Call HubLink.HubClose
End Sub
Below is the HubLaunch sub. It opens a second workbook that includes a macro that is then asked to run.
I can avoid the runtime error by commenting out the line "Call HubLink.HubLaunch" and instead manually open the MacroHub.xlsm file (via File > Open > [Choose My File]) before executing sub "ResizeListObject_DateRange".
In the immediate window I verified that ?Workbooks.Count = 2. I do not understand why my HubLaunch sub is opening the MacroHub.xlsm workbook in such a way that its Macros are inaccessible to the first workbook.
Can anyone explain the error?
Thanks.
' Opens the Workbook that stores all common macros
Sub HubLaunch()
' The current workbook
Dim WkBk As String
WkBk = ActiveWorkbook.Name
Dim WkBk_Hub As Workbook
' If not already launched, open the Common workbook that stores all common macros
On Error Resume Next
Set WkBk_Hub = Workbooks(MacroHub_Path & MacroHub_FName)
On Error GoTo 0
If WkBk_Hub Is Nothing Then
Set WkBk_Hub = Workbooks.Open(MacroHub_Path & MacroHub_FName)
End If
' Reselect the calling workbook
Workbooks(WkBk).Activate
End Sub
I have a simple question (I think). Let's say I set a global variable in a standard module in Book1 to an open (different) workbook:
Public Sub InitGlobals()
Set gwkb = Workbooks("book2.xlsx")
End Sub
Now I close book2.xlsx manually. What is the status of gwkb? This is what I get when I run various tests in the immediate window:
?gwkb is nothing
False
?isempty(gwkb)
False
?isnull(gwkb)
False
However, if I ask for any property of gwkb, such as gwkb.Name, I get an automation error.
My question is: is there a way to test for this condition, without resorting to some sort of "On Error Resume Next" test?
Thanks for your help.
If Book2 is initially open and may be closed then looping over the open books will not find it:
Public Sub InitGlobals()
Dim gwkb As Workbook, wb As Workbook
Set gwkb = Workbooks("Book2.xlsx")
gwkb.Close
For Each wb In Workbooks
If wb.Name = "Book2" Then
MsgBox wb.Name & " is open "
End If
Next wb
End Sub