我有一个旋转按钮,可以增加和减少给定数组的尺寸。现在我需要根据数组的尺寸添加和删除元素。我现在完全迷失了。任何帮助表示赞赏。
这是用数组填充一系列单元格的函数:
Private Sub OptionButton4_Click()
With Application
.ScreenUpdating = False
End With
Dim rng As Range
Dim cell As Range
Dim counter As Long
ReDim Preserve pwmArray(i)
OptionButton4.Height = 26.25
OptionButton4.Width = 87
OptionButton4.Left = 330.75
OptionButton4.Top = 408
Set rng = Range("B2", Range("B2").Offset(0, i))
counter = 0
pwmArray(0) = "0"
pwmArray(1) = "10"
pwmArray(2) = "0"
pwmArray(3) = "10"
pwmArray(4) = "10"
pwmArray(5) = "0"
pwmArray(6) = "10"
pwmArray(7) = "10"
pwmArray(8) = "10"
pwmArray(9) = "0"
pwmArray(10) = "10"
pwmArray(11) = "10"
pwmArray(12) = "10"
pwmArray(13) = "10"
pwmArray(14) = "0"
pwmArray(15) = "10"
pwmArray(16) = "10"
pwmArray(17) = "10"
pwmArray(18) = "10"
pwmArray(19) = "10"
pwmArray(20) = "0"
pwmArray(21) = "10"
pwmArray(22) = "10"
pwmArray(23) = "10"
pwmArray(24) = "10"
pwmArray(25) = "10"
pwmArray(26) = "10"
pwmArray(27) = "0"
pwmArray(28) = "0"
pwmArray(29) = "0"
pwmArray(30) = "0"
If OptionButton4.Value = True Then
For Each cell In rng
cell.Value = pwmArray(counter)
counter = counter + 1
Next cell
End If
With Application
.ScreenUpdating = True
End With
End Sub
以下是两个旋转按钮功能之一:
Private Sub SpinButton2_SpinUp()
Dim dataCell As Range
Set dataCell = Range("E23")
dataCell.Value = dataCell.Value + 1
i = dataCell.Value
End Sub
答案 0 :(得分:0)
Redim将删除任何多余的元素。我猜你正在寻找重新填充余下的元素(无论是更多还是更少),使用你已经显示的模式。以下代码应该为您执行此操作。
Dim k As Long
Dim j As Long
Dim tens As Long
k = -1
j = 0
Do While j <= UBound(pwmArray)
For tens = 0 To k
If j <= UBound(pwmArray) Then
pwmArray(j) = "10"
j = j + 1
End If
Next
k = k + 1
If j <= UBound(pwmArray) Then
pwmArray(j) = "0"
j = j + 1
End If
Loop
答案 1 :(得分:0)
您似乎正在尝试将两个独立的程序连接在一起。换句话说,您正在尝试使用微调器过程来设置您希望在选项按钮过程中使用的i
的值。
我建议做这样的事情:
<强>未测试强>
Option Base 1
Private Sub OptionButton4_Click()
Const lDefault As Long = 31
Call Set_Array_And_Range(lDefault) '<~~ this number would be the default _
' number of cells/elements when the option _
' button is pressed.
'I'm assuming you'll place this code in the same sheet module as the spin
'button code
Me.SpinButton2.Value = lDefault '<~~ Resets the spin button value
End Sub
Sub Set_Array_And_Range(i As Long)
If i = 0 Then Exit Sub
With Application
.ScreenUpdating = False
End With
Dim rng As Range
Dim cell As Range
Dim counter As Long
Dim pwmArray() As String
'ReDim the array. The array doesn't have any values yet, so we don't have to Preserve.
ReDim pwmArray(1 To i)
'Resize the range
Set rng = Range("B2").Resize(0, i)
counter = 0
'Code for filling your array goes here... but you can use something like _
'Greg posted
If OptionButton4.Value Then
For Each cell In rng
cell.Value = pwmArray(counter)
counter = counter + 1
Next cell
End If
With Application
.ScreenUpdating = True
End With
End Sub
'This code is in the worksheet module, not a regular module.
'So if your button is on sheet2, put this code in the Sheet2 module.
Private Sub SpinButton2_SpinUp()
Call Set_Array_And_Range(Me.SpinButton2.Value)
End Sub