I currently have this code
Private Sub Worksheet_Change(ByVal Target As Range)
WorksheetChanged(Target, Range("AB3").CurrentRegion, Range("B18:B19"))
WorksheetChanged(Target, Range("AE3").CurrentRegion, Range("B20:B21"))
End Sub
But it throws me a "Compile Error: Expected =", I have no idea why and I don't know where an = would go, any help? Thank you in advance.
If you want to use brackets, you need to assign to a variable or in some cases, use Call, if you do not need to assign, skip the brackets:
WorksheetChanged Target, Range("AB3").CurrentRegion, Range("B18:B19")
Related
Please Help,
I have written different procedures on different worksheet in a Workbook. I want to call a particular procedure depending on the specific cell value in a worksheet.
I tried with defining a variable
Example:
Private Sub CommandButton1_Click()
pbk = Me.Range("L1").Value
Call pbk
End Sub
but I'm getting errors like this:
Microsoft Visual Basic for Applications
Compile error:
Expected Sub, Function, or Property
value Range L1 is changing as per contents it has array of total 15 contents, so I have written 15 procedures different. i just need to call each procedure depending on the value of 'L1'.
Its total 15 Procedures, i can write with the IF condition like this:
Private Sub CommandButton1_Click()
pbk = Me.Range("L1").Value
If pbk = "PBK_Kirim" Then
Call PBK_Kirim
End If
If pbk = "PBK_ke_Masa" Then
Call PBK_ke_Masa
End If
'and so on
End Sub
but it will be too bulky so i'm trying for some easy method. thanks before
I agree with Rory, use "run pbk". Only other alternative to clean it up without using run that I can think of is build another sub routine passing in the pbk value and using select case for the different calls.
I want to call a Sub I declared at it gives a compilation error saying that it expects a =.
The Sub call is in a UserForm_Initialize event procedure.
The code is as follows.
In a module:
Public Sub FillCb(Ar() As String, Cb As ComboBox)
Cb.Clear
For I = 1 To Application.CountA(Ar)
Cb.AddItem (Ar(I))
Next I
End Sub
In the UserForm code:
Private Sub UserForm_Initialize()
LblDate.Caption = Date
FillCb(LibrosNoPrestados, CbLibro)
End Sub
This code is giving me error.
I analized the code line by line using the debugger and commenting the las line inside the Initialize event, and it works fine up to that point. The error is thrown at compile time in the
FillCb(LibrosNoPrestados, CbLibro)
The rest of the code is not needed here since as I said it works fine, but the syntax in that last line must be wrong and I can't see the mistake.
A VBA "feature". If you are calling a sub routine without the "Call" keyword then don't use parentheses, if you use the "Call" keyword then you need the parentheses.
Eg
Call FillCb(LibrosNoPrestados, CbLibro)
Or
FillCb LibrosNoPrestados, CbLibro
Here's Microsoft's documentation:
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/call-statement
I'm pretty new to this so apologies in advance
I'm half way through a userform in Excel and I'm trying to cut some fat off my code by using Call - I have 12 buttons that all do the same thing, the only difference is that each buttons sub is dependant on the buttons caption. My problem is that I can't figure out a way to use a String I've already declared in the Buttons Sub, then use it in the called Sub. I know you can do it, but my googling skills have failed me :(
Please could someone show me how to do this? Hope that all makes sense...
Here is a very small snippet of my code, but you get the jist:
Public Sub CommandButton4_Click()
Dim Name As String
Name = CommandButton4.Caption
Call Sort1
End Sub`
And the other one (Also tried this as function for the sake of trial and error)
Public Sub Sort1(Name As String)
Label11.Caption = Name
Sheets(Name).Select
End Sub
What you're referring to is passing an argument to another subroutine or function. Let's say you want to use a function a lot of times to get the first letter of a string. A sample of this is:
Function LeftOne(StrSample As String) As String
LeftOne = Left(StrSample, 1)
End Function
The above function can be used inside another function or subroutine provided you meet its requirement: StrSample. By declaring StrSample As String in the arguments field of the function, you are basically requiring that any calls to this should require a string to be passed to it. Anything else would throw an error.
The full line LeftOne(StrSample As String) As String can be read as: "I am function LeftOne. Pass me a string and I'll return to you a string after doing something with it." Note that the name StrSample is an arbitrary name.
Anyway, calling the above is as simple as:
Sub MsgInABox()
Dim StrToFeed As String
StrToFeed = "BK201"
MsgBox LeftOne(StrToFeed) 'Returns B.
End Sub
In your example, if you want to pass Name to Sort1, your attempt is absolutely correct.
Let us know if this helps.
You hat to give your sort1 procedure the parameter name.
call sort1(name)
or
call sort1(CommandButton4.Caption)
Why would this be a syntax error? it is asking for an = sign
cyberlinkcompute("A1","B2")
private sub cyberlinkcompute ( a as string, b as string)
end sub
isnt a sub function something that does not need an =?
thanks in advance
Remove the parentheses or put the word Call in front of the procedure name
Its the function-like parentheses it dislikes so either;
cyberlinkcompute "A1","B2"
or less advisably:
call cyberlinkcompute("A1","B2")
E.g. Given Sheet 1 contains:
Ref: Do things
How can I direct a code in Module 1 to GoTo Ref? If I were in the Sheet1 code moduke then I could simply use a
Goto Ref
But this doesn't work across different modules
Your question is not clear and you didn't provide any code, so this is a guess.
GoTo is used to jump to different locations within the same sub/function. You cannot use it to jump to parts of other sub routines or functions, which it sounds like you might be trying to do.
Also, "NapDone:" is not called a reference, it's formally called a line label. :)
To help expand on the other answers.. Like they said you shouldn't use GoTo for anything in VBA except error handling.
What you should be doing is calling a public sub/function from another module. For example in Module 1 you would have the following
Sub TestMod1()
Dim MyNumber As Integer
MyNumber = GetSquare(6)
'MyNumber returns from the function with a value of 36
End Sub
and on Module 2 you have
Public Function GetSquare(ByVal MyNumber As Integer)
GetSquare = MyNumber * MyNumber
End Function
So now you know how to avoid it. GoTo is not very good programming practice as you'll have things flying all over the place. Try to break down code you're repeating into multiple Subs and just call them when needed, or functions whatever be the case. Then you'll get into classes, which are just wrapped up to represent an object and it'll do all the work for that object.
This should get you on the right track.