I have a problem with VBA modules.
My code looks like this:
Sub MainFunction()
Call Module1
Call Module2
Call Module3
End Sub
If Module2 fails, Module3 should no longer be executed. How can I achieve this?
Thanks for your help
Related
I am trying to write a simple function (in a module FileIO) which would take an instance of a work book, and just close it. This function is invoked from another module Business.
Below is the code snippet.
Public Function CloseExcelFile(wkBook As Workbook)
If (wkBook Is Not Nothing) Then
wkBook.Save
wkBook.Close
End If
End Function
I invoke this method by using the command FileIO.CloseExcelFile(catWorkBook). Variable catWorkBook is the object reference to the workbook I created (in a step before).
When ever I try too invoke the custom function, I am getting the error
object does not support this method or property
The below command closes the work book with no errors.
catWorkBook.Close
But the same does not happen when I use the custom function. What is going wrong here?
You just have your Not in the wrong place. Try it like this:
Public Function CloseExcelFile(wkBook As Workbook)
If Not wkBook Is Nothing Then
wkBook.Save
wkBook.Close
End If
End Function
As braX pointed out, your Not isn't in the right place.
You also don't need a Function here. Change it to a Sub. In fact, you barely need the Sub when you reduce it to one line like this:
Public Sub CloseExcelFile(wkBook As Workbook)
If Not wkBook Is Nothing Then wkBook.Close(SaveChanges:=True)
End Sub
The error was due to the INCORRECT way I was invoking a sub routine. I invoked the sub routine as:-
FileIO.CloseExcelFile(catWorkBook)
The CORRECT way to invoke a function would have been.
FileIO.CloseExcelFile catWorkBook
I'm making a workbook with a whole lot of different subs, and in an effort to avoid a user accidentally activating a sub that erases the code of a sheet for example, I've tried making all the subs private instead.
My subs can now only be activated by clicking buttons on the worksheet, and it all works as intended. Untill a sub of mine tries to call a private sub in another module of course.
To get around this I used Application.Run rather than Call, which worked and also allows me to call in a variable "NextSub" from the previous sub, which gives me some flexibility that I need, and apparently cant get with the Call.
Eg.
Sub FirstSub()
*Something going on
Application.Run "SecondSub", SomeVariableSub
End sub
Sub SecondSub(Nextsub as String)
If something Then
*Do something
Application.Run NextSub
Else
Application.Run NextSub
I thought that the Application.Run had solved all my problems, but I used to have a line that called an errorhandler, which in turn called a sub. It seems that the program can no longer backtrack to the sub that contained the errorhandler as it could when I used Call.
Does Applciation.Run break this functionality? If yes, can I then use Call with a variable NextSub as I am doing it now? And if I can't use the Call that way, then is all this fixed by adding a On Error GoTo ErrorHandler in the affected subs?
I know that the whole thing about calling Private Subs across modules is probably pretty bad practice, but I was compltely new to this when I started out, and the project is too extensive to fix that without rewriting all of the code.
Instead of making all subs private, either put Option Private Module on top of each module, or add a dummy argument to each routine:
Sub SomeHiddenRoutine(bDummy As Boolean = False)
'Routine can be called as usual using:
SomeHiddenRoutine
End Sub
If I understand you are trying to call a function specified by a string.
The correct way is to use something like this, which allows you to call all the private subs (as long as it is in the same module as the private functions):
Sub CallFunction(FuncName As String)
Select Case FuncName
Case "Func1": Func1
Case "Func2": Func2
Case "Func3": Func3
End Select
End Sub
Hi i tried the following simple code, but it has error of
Object variable or With block variable not set.
Module Module1
Sub main()
Dim ppt As Presentation = Nothing
ppt.LoadFromFile("C:\Users\310238479\Desktop\test.pptx")
End Module
End Sub
I seems that you are not using vb.net only, but spire.presentation.
As the commands are correct, please verify, that you try to access an existing file and you have sufficient rights ...
I'm wondering where to place my function declarations in my project. For this project I am supposed to modularize the coding which currently lies under Button Calculate (previous project) but everytime I try to place it under button calculate it tells me it doesn't belong within the method body. So how would I go about doing this?
Hmm. I dont want to sound condescending at all in writing this, but I'm guessing that you're trying to put a function directly under a line of code that says something like..
private Sub ButtonCalculate()
This is the start of a subroutine definition. Try looking further down the code for the line that says..
End Sub
and put your function declaration under there - leaving a blank line for readability's sake e.g.
private sub ButtonCalculate()
'code for buttoncalculate
'more code for buttoncalculate
'lots more code for buttoncalculate
End Sub
private Function randomFunction(WhateverYourParametersare byval integer) as integer
'your function code
'more function code
End Function
Is it possible to call a function from one Module to another?
I have the following code:
Sub MAIN()
Call IDLE
End Sub
MAIN is located in Module1
IDLE is located in Module2 and defined as: Sub IDLE()
Prefix the call with Module2 (ex. Module2.IDLE). I'm assuming since you asked this that you have IDLE defined multiple times in the project, otherwise this shouldn't be necessary.