简体   繁体   English

Excel VBA-从可变数量的用户窗体复选框创建数组

[英]Excel VBA - Create array from variable number of userform checkboxes

I think this is less complicated than I am making it but being a novice with VBA I haven't been able to find the answer after a couple days of googling and playing with different code. 我认为这没有我做的那么复杂,但是作为VBA的新手,我经过几天的谷歌搜索和使用不同的代码后仍无法找到答案。

I have a macro that: 我有一个宏:

  1. Opens a file in whatever folder path is named 以任何文件夹路径打开文件
  2. Searches for specific text in the file to find the start of a specific section, ie A100 在文件中搜索特定文本以查找特定部分的开始,即A100
  3. Finds the end of this section, ie A110 (variable in length depending on the file) 查找本节的末尾,即A110(长度随文件而异)
  4. Copy and pastes the cells in this range to another specific range, ie O1:O10 将此范围内的单元格复制并粘贴到另一个特定范围,即O1:O10
  5. Populates a userform with a checkbox for each cell in this new variable length range 在此新的可变长度范围内,为用户表格填充一个复选框,用于每个单元格

I now need the user to click which checkboxes they want and then the captions for these checkboxes to be saved as an array that I can then call on later in the macro. 现在,我需要用户单击所需的复选框,然后将这些复选框的标题另存为数组,以便稍后在宏中调用。

ie if they clicked dog, cat, and bird from the checkboxes, the output would be dog,cat,bird 例如,如果他们单击复选框中的狗,猫和鸟,则输出为狗,猫,鸟

Because of the variable length of the range and number of checkboxes, I can't figure out how to have it loop through each one and concatenate the correct values. 由于范围的可变长度和复选框的数量,我无法弄清楚如何让它遍历每个复选框并连接正确的值。

I think there is probably a way to cut out the copy pasting of the values to populate the userform with also, but this was the only way I could figure out that part given the variable length of the range. 我认为可能有一种方法可以剪切出值的副本粘贴内容,以也填充用户窗体,但这是鉴于范围的可变长度,我可以弄清楚该部分的唯一方法。

Below is the code that generates the userform after the range has been copy pasted. 下面是复制范围后生成用户窗体的代码。

Private Sub UserForm_Initialize()

Dim curColumn   As Long
Dim i           As Long
Dim codeRow     As Long
Dim chkBox      As msforms.CheckBox

curColumn = 15
codeRow = Range("O20").End(xlUp).Row

For i = 1 To codeRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets(1).Cells(i, curColumn).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i

End Sub

Insert moduel code. 插入模块代码。

Public vCheck()

Bellows in form code. 波纹管的形式代码。

Private Sub UserForm_Initialize()

Dim curColumn   As Long
Dim i           As Long
Dim codeRow     As Long
Dim chkBox      As msforms.CheckBox

curColumn = 15
codeRow = Range("O20").End(xlUp).Row

ReDim vCheck(0)
For i = 1 To codeRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)

ReDim Preserve vCheck(1 to i)
vCheck(i) = Worksheets(1).Cells(i, curColumn).Value
chkBox.Caption = vCheck(i)
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i

End Sub

Listbox was the way to go. 列表框是要走的路。 Here is the updated code for others looking for help with the same issue: 这是其他人寻求同一问题的更新代码:

Private Sub UserForm_Initialize()

Dim myLBox      As msforms.ListBox
Dim codeRow As Long
codeRow = Range("O20").End(xlUp).Row

ListBox1.RowSource = "O1:O" & codeRow

End Sub

Private Sub CommandButton1_Click()
Dim rRange As Range
Dim lCount As Long   'Counter

On Error GoTo ErrorHandle

Set rRange = Range("P1")

With ListBox1
   For lCount = 0 To .ListCount - 1
      If .Selected(lCount) = True Then
         rRange.Offset(lCount, 0).Value = .List(lCount)
      End If
   Next
End With

BeforeExit:
Set rRange = Nothing
Unload Me

Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM