vb中的按钮点击事件
我是VB新手。我想在单击第一个按钮时将值存储在数组中,并在单击第二个按钮时显示结果。我成功地将值存储在数组中。但我无法在第二个按钮单击事件中访问相同的数组。
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
任何人都可以帮助我。
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
Any one can help me please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Nimmy
我的帖子不是为了回答你的主要问题:) 如果你看看 Ken 和 Cody 的评论,那么你会自动意识到答案是什么;)
当我看到你的代码和你的声明时,我忍不住发表评论新手。我记得当我学习编码时,像 SO 这样的论坛实际上帮助我提高了编码技能。因此,您可以将其视为回报:-D
1) 在您的情况下,可以将 i 作为整数变暗,但是当您处理以下行时会发生什么更大,例如 32768 行。在 VBA Excel 中工作时,将 i as long 调暗是安全的。
2) .Select 是使用 VBA 时出现错误的主要原因,更不用说它们会减慢代码速度。相同的代码也可以写成下面给出的代码。我假设第一行和最后一行之间没有空白值。
HTH,是的,快乐编码;)
Sid
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.
HTH and yes, Happy Coding ;)
Sid