Evento de clic de botón en vb

I am newbie in VB. I want to store the values in an array when I am clicking the first button and show the result when I am clicking the second button. I am successfully stored the values in an array. But i cant access the same array in the second button click event..

Dim i As Integer
Dim ag(0 To 7000) As String
Dim bg(0 To 7000) As String

Private CommandButton1_Click()
  i = 0

  Sheets("New").Select
  Range("B2").Select

  While Not IsEmpty(ActiveCell)
    ag(i) = ActiveCell.Value
    i = i + 1
    ActiveCell.Offset(1, 0).Select
  Wend

  i = 0

  Sheets("New").Select
  Range("D2").Select

  While Not IsEmpty(ActiveCell)
    bg(i) = ActiveCell.Value
    i = i + 1
    ActiveCell.Offset(1, 0).Select
  Wend
End Sub

Private CommandButton2_Click()
  UserForm1.Hide
End Sub

Private Sub Cell_Click()
End Sub

Private Sub CommandButton1_Click()
End Sub

Private Sub CommandButton2_Click()
End Sub

Cualquiera puede ayudarme por favor.

preguntado el 01 de febrero de 12 a las 03:02

When you're posting your question, you can preview it (especially the code formatting) in real-time just below the point where you're typing it. It's a good idea to do so, so that your post is more clear to those people trying to read it to help you. (If you format it properly, it also means others don't have to spend their time doiong so.) Clear questions (including clear code formatting) help you get an answer more quickly. :) -

Also, the problem in accessing them in the second button click is that you're doing nothing to do so. Your CommandButton2_Click() handler simply hides Userform - how is that supposed to access the array created by the `CommandButton1_Click()? -

"But i cant access the same array in the second button click event." ¿Por que no? -

On reading, it should work - the array is in the scope for both routines. The array may be cleared out if you click the "stop" button in the IDE, which removes all running state from your program. Are you doing this? -

Code looks wrong. Eg: Private CommandButton1_Click() is not valid (where's the Sub?) Also you have duplicate method declarations (if you fix the broken ones) -

1 Respuestas

Nimmy

My post is not about answering your main question :) If you look at Ken's and Cody's comment then you will automatically realize what the answer is as ;)

I couldn't help comment when I saw your code and your statement that you are a newbie. I remember my days when I was learning coding and forums like SO actually helped me enhance my coding skills. So you can consider this as a payback :-D

1) In your case it is ok that you have dimmed i as integer but what happens when you are dealing with rows which are much bigger for example 32768 rows. It's safe to dim i as long when working in VBA Excel.

2) .Select are a major cause of errors when working in VBA and not to mention that they slow down your code. The same code can also be written as the code given below. I am assuming that there is no blank values in between the 1st row and the last row.

Dim i As Long
Dim ag(0 To 7000) As String
Dim bg(0 To 7000) As String
Dim ws As Worksheet

Private CommandButton1_Click()
    Set ws = Sheets("New")

    With ws
        For i = 2 To .Range("B" & .Rows.Count).End(xlUp).Row
            ag(i) = .Range("B" & i).Value
        Next
        For i = 2 To .Range("D" & .Rows.Count).End(xlUp).Row
            bg(i) = .Range("D" & i).Value
        Next
    End With
End Sub

HTH and yes, Happy Coding ;)

Sid

Respondido 02 Feb 12, 06:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.