vb中的按钮点击事件

发布于 2024-12-29 23:12:39 字数 765 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2025-01-05 23:12:39

Nimmy

我的帖子不是为了回答你的主要问题:) 如果你看看 Ken 和 Cody 的评论,那么你会自动意识到答案是什么;)

当我看到你的代码和你的声明时,我忍不住发表评论新手。我记得当我学习编码时,像 SO 这样的论坛实际上帮助我提高了编码技能。因此,您可以将其视为回报:-D

1) 在您的情况下,可以将 i 作为整数变暗,但是当您处理以下行时会发生什么更大,例如 32768 行。在 VBA Excel 中工作时,将 i as long 调暗是安全的。

2) .Select 是使用 VBA 时出现错误的主要原因,更不用说它们会减慢代码速度。相同的代码也可以写成下面给出的代码。我假设第一行和最后一行之间没有空白值。

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,是的,快乐编码;)

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文