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
}
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,198
Messages
5,509,755
Members
408,752
Latest member
KrisF

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top