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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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().
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,693
Members
449,048
Latest member
81jamesacct

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
Back
Top