Pointer in VBA?

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Hi, I was wondering if there's a pointer in VBA language?
just like in C language.

For example,
Move(&c)

Function Move (int *c){
//Your code
}
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

gsbelbin

Active Member
Joined
Aug 29, 2008
Messages
336
Hi, I was wondering if there's a pointer in VBA language?
just like in C language.

For example,
Move(&c)

Function Move (int *c){
//Your code
}

VBA and VB can use pointers and essentially does when it passes variables ByRef. There are also functions for accessing memory locations of variables; VarPtr(), StrPtr() and ObjPtr().
 

Akihito Yamashiro

Board Regular
Joined
Jun 2, 2010
Messages
60
Hi.
As gsbelbin has said, VB can use pointers.
Here's some sample.
Using ByRef,
Code:
'In Module1
Sub Main1()
    Dim c As Long
    c = 3
    Move1 c
    'c becomes 4
End Sub
Sub Move1(ByRef i As Long) 'Equivalent to void Move1(int& c)
    i = i + 1
End Sub

Using VarPtr,
Code:
'In module2
Private Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
(ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Sub Main2()
    Dim c As Long
    c = 3
    Move2 VarPtr(c) 'Equivalent to Move2(&c)
    'c becomes 4
End Sub
Sub Move2(ByVal pointerOfi As Long) 'Equivalent to void Move2(int *c)
    'Equivalent to *c = *c +1
    Dim tempValue As Long
    MoveMemory VarPtr(tempValue), pointerOfi, 4
    tempValue = tempValue + 1
    MoveMemory pointerOfi, VarPtr(tempValue), 4
End Sub
 

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Wow! I had no idea! It makes VBA programming is much more powerful than I've thoughtbefore!!

Thanks for the help, gsbelbin and Akihito Yamashiro
 

Watch MrExcel Video

Forum statistics

Threads
1,133,715
Messages
5,660,446
Members
418,580
Latest member
JP82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top