PDA

View Full Version : [SOLVED:] Reading items



Malak
04-16-2023, 02:41 AM
Hello,
how must this code be updated so that the A1 value appears in the textbox on a first click, then it gets replaced by the A2 value on the second click etc.


Private Sub CommandButton1_Click()
'Dim iCount As Integer
'TextBox1.Value = Cells(?,1).Value
End Sub

Malak

rollis13
04-16-2023, 03:36 AM
This could be a solution for this specific request:
Option Explicit
Dim iCount As Integer
Private Sub CommandButton1_Click()
If iCount = 0 Then iCount = 1
TextBox1.Value = Cells(iCount, 1).Value
iCount = iCount + 1
End Sub

arnelgp
04-16-2023, 05:42 PM
Private Sub CommandButton1_Click()
Static iCount As Integer
iCount = iCount + 1
TextBox1.Value = Cells(iCount,1).Value
End Sub

Malak
04-16-2023, 08:11 PM
Hello,
thank you for this interesting solution. I amsurprised to learn that this works with iCount being declared outside the clickevent. Does it work because by default, the variable iCount declared outside ofthe click event is considered private and can only be accessed within the sameprocedure or must this be explained in another way?

Malak
04-16-2023, 08:12 PM
Hello rollis 13,
Hello,
thank you for this interesting solution. I amsurprised to learn that this works with iCount being declared outside the clickevent. Does it work because by default, the variable iCount declared outside ofthe click event is considered private and can only be accessed within the sameprocedure or must this be explained in another way?

Malak
04-16-2023, 08:27 PM
Thank you arnelgp arnelgp for this solution. :yes
Malak

arnelgp
04-16-2023, 08:42 PM
iCount on Rollis13 post is Declared Module-wise.
meaning it is visible in any sub/function within the Module.
the problem is that any sub/function can modify it's value.

Declaring it Static within the sub/function means only that
sub/function, where it was declared, can modify the value of iCount (much safer and much encapsulated).

Malak
04-18-2023, 04:59 PM
Thank you arnelgp!:yes This is clear and very helpful!
Malak