根据选择的选项按钮,将不同的数组加载到一系列单元格中(参见下面的示例)。我想通过旋转按钮添加扩展和缩短单元格范围(以及反过来的数组)的功能。我该怎么做呢?
基本上我要问的是:有没有办法在这个数组中添加另一个元素并将其反映在正在使用的单元格范围内?
加上:有一个代表这个单元格范围的图表,我该如何使该图表系列动态化? (随范围增加/减少)
Private Sub OptionButton4_Click()
With Application
.ScreenUpdating = False
End With
Dim rng As Range
Dim cell As Range
Dim counter As Long
OptionButton4.Height = 26.25
OptionButton4.Width = 87
OptionButton4.Left = 330.75
OptionButton4.Top = 408
Set rng = Range("B2", "AF2")
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
答案 0 :(得分:1)
未测试!
但要决定范围,你可以做这样的事情
i = InputBox("Range")
Set Rng = Range(Cells(1, 2), Cells(i, 2))
然后将列设置为他们输入的NUMBER,如果他们输入3等于A2:C2。
至于旋转按钮我不知道100%如何整合它,但这至少可以帮助你开始。
答案 1 :(得分:1)
从你发布的代码中,看起来你正在使用静态数组。你需要一个动态数组。 Chip Pearson很棒write up on Arrays.我鼓励你查看。
因此,对于这种情况,您需要执行以下操作:
Option Base 1 '<~~ This sets the lower bound to 1 instead of the default 0
Sub Main()
Dim pwmArray() As Long
ReDim pwmArray(1 To 31)
'Fill your array with values here
'Now if an element needs to be added to the array (and you want to keep _
' the current values) do the following:
ReDim Preserve pwmArray(1 To 32)
pwmArray(32) = x
End Sub
请注意Preserve
关键字。这将使值保持在数组中,同时增加的大小最后一个维度。
现在,ReDim
语句处于性能饥饿状态,因此我通常会尝试在代码中尽可能ReDim
一次(例如,尽可能将ReDim
保留在循环之外)。< / p>
答案 2 :(得分:0)
'Imagine the range as a rectangle.
'This crops it by the amounts specified and returns cropped range.
'Use negative numbers to expand the range
Public Function mCropRange(pRange As Range, pFromTop As Long, pFromLeft As Long, _
pFromBottom As Long, pFromRight As Long) As Range
Dim lNumRows As Long, lNumCols As Long
lNumCols = pRange.Columns.Count
lNumRows = pRange.Rows.Count
With pRange
Set mCropRange = Range(.Cells(1 + pFromTop, 1 + pFromLeft), _
.Cells(lNumRows - pFromBottom, lNumCols - pFromRight))
End With
End Function
?mCropRange(Range(“ B2:Y9”),-1,-1,-1,-1)。地址
$ A $ 1:$ Z $ 10
?mCropRange(Range(“ A1:Z20”),2,2,2,2)。地址
$ C $ 3:$ X $ 18