ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA syntax (https://www.excelbanter.com/excel-programming/371574-vba-syntax.html)

Arne Hegefors

VBA syntax
 
Hello! I have become somewhat unsure of certian things regarding the syntax
in vba. therefor I would truly appreciate answers to the following questions:

1) In the beginning of a sub that takes two arguments do you have to write:
Private Sub checkReturnDifference(ByVal rng1 As Range, ByVal rng3 As Range)
or can you write ....(ByVal rng1, rng3 As Range)

2) When declaring new variables is it necessary to write:
Dim i As Integer
Dim k As Integer
.....
or is there a shorter way of doing this?

I know that these questions does not seem very important but when you have
meny variables then it does actually make a difference. thus, any help
appreciated! thank you all very much!

[email protected]

VBA syntax
 
Hi
1. I would do
checkReturnDifference(ByVal rng1 As Range, ByVal rng3 As Range)

every time.
I would also put in plenty of comments about what the variables mean
and what the calling sub is.

2. You can do
Dim i as integer, k as integer, Rng1 as Range

but nothing shorter.

regards
Paul

Arne Hegefors wrote:
Hello! I have become somewhat unsure of certian things regarding the syntax
in vba. therefor I would truly appreciate answers to the following questions:

1) In the beginning of a sub that takes two arguments do you have to write:
Private Sub checkReturnDifference(ByVal rng1 As Range, ByVal rng3 As Range)
or can you write ....(ByVal rng1, rng3 As Range)

2) When declaring new variables is it necessary to write:
Dim i As Integer
Dim k As Integer
....
or is there a shorter way of doing this?

I know that these questions does not seem very important but when you have
meny variables then it does actually make a difference. thus, any help
appreciated! thank you all very much!



Bob Phillips

VBA syntax
 

"Arne Hegefors" wrote in message
...
Hello! I have become somewhat unsure of certian things regarding the

syntax
in vba. therefor I would truly appreciate answers to the following

questions:

1) In the beginning of a sub that takes two arguments do you have to

write:
Private Sub checkReturnDifference(ByVal rng1 As Range, ByVal rng3 As

Range)
or can you write ....(ByVal rng1, rng3 As Range)



You have to write it as the former, otherwise rng1 will assume a type of
Variant, not Range.


2) When declaring new variables is it necessary to write:
Dim i As Integer
Dim k As Integer


You can write

Dim i As Integer, k As Integer

but if you write

Dim i , k As Integer

again i will assume a type Variant.

You could use use the DefInt statement to default all variables that begin
with a certain letter(s) to type of Integer

DefInt A-K

but I must admit to not liking this approach, I don't like implicit
definitions in the code.



NickHK[_3_]

VBA syntax
 
Arne,
Others have mentioned the type definition part, but AFAIK declaring the
objects as "ByVal" is pointless as all objects are passed by reference, the
default.
Unless I'm mistaken, but as a quick test :

Private Sub CommandButton1_Click()
Range("B3").Value = "Initial value"
Call TestByValObject(Range("B3"))
Debug.Print Range("B3").Value

Dim TestStr As String
TestStr = "Initial value"
Call TestByValStr(TestStr)
Debug.Print TestStr
End Sub

Function TestByValObject(ByVal argObject As Range)
argObject.Value = "If really ByVal, you cannot see this change."
End Function

Function TestByValStr(ByVal argString As String)
argString = "If really ByVal, you cannot see this change."
End Function

NickHK

"Arne Hegefors" ...
Hello! I have become somewhat unsure of certian things regarding the
syntax
in vba. therefor I would truly appreciate answers to the following
questions:

1) In the beginning of a sub that takes two arguments do you have to
write:
Private Sub checkReturnDifference(ByVal rng1 As Range, ByVal rng3 As
Range)
or can you write ....(ByVal rng1, rng3 As Range)

2) When declaring new variables is it necessary to write:
Dim i As Integer
Dim k As Integer
....
or is there a shorter way of doing this?

I know that these questions does not seem very important but when you have
meny variables then it does actually make a difference. thus, any help
appreciated! thank you all very much!





All times are GMT +1. The time now is 05:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com