简体   繁体   English

VBA Excel:扩展和缩短单元格范围

[英]VBA Excel: Extending & Shortening Cell Range

Depending on which option button is selected, a different array is loaded into a range of cells (see example below). 根据选择的选项按钮,将不同的数组加载到一系列单元格中(请参见下面的示例)。 I want to add the ability to extend and shorten that range of cells (and in turn the array) through a spin button. 我想添加通过旋转按钮扩展和缩短该单元格范围的能力(并进而缩短该范围)。 How would I go about doing that? 我将如何去做?

Basically what I'm asking: is there a way to add another element to this array and have that be reflected in the range of cells being used? 基本上,我要问的是:有没有一种方法可以向该数组添加另一个元素,并将其反映在所使用的单元格范围内?

Add on: there is a graph which represents this range of cells, how would I make that graph's series dynamic? 附加:有一个代表该单元格范围的图形,我将如何使该图形的系列动态化? (increase/decrease with the range) (随范围增加/减少)

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

Untested! 未经测试!

But to dictate the range you could do something like this 但是要决定范围,您可以执行以下操作

i = InputBox("Range")
Set Rng = Range(Cells(1, 2), Cells(i, 2))

This would then set the column to what ever NUMBER they enter so if they put in 3 that would be equal to A2:C2. 然后,这会将列设置为他们输入的NUMBER,因此如果他们输入3等于A2:C2。

As for a spin button I do not know 100% how to integrate that but this at least gives you a start. 至于旋转按钮,我不知道100%如何将其集成,但这至少为您提供了一个起点。

From the code you've posted, it looks like you're using a static array. 从您发布的代码来看,您似乎正在使用静态数组。 You need a dynamic array for this. 为此,您需要一个动态数组。 Chip Pearson has a great write up on Arrays. Chip Pearson 在Arrays上有出色的著述。 I encourage you to check it out. 我鼓励您检查一下。

So for this scenario, you'd want to do something like this: 因此,对于这种情况,您需要执行以下操作:

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

Note the Preserve keyword. 请注意Preserve关键字。 This will keep the values in an array while increasing the size of only the last dimension. 这将使值保留在数组中,同时增加最后一个维度的大小。

Now, the ReDim statement is performance hungry, so I generally try to only ReDim once if possible in the code (ie keeping the ReDim outside of loops when possible). 现在, ReDim语句会降低性能,因此我通常会尝试在代码中仅在可能的情况下仅执行一次ReDim (即,尽可能将ReDim保持在循环之外)。

'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

Testing in the Immediate Window 立即窗口中的测试

?mCropRange(Range("B2:Y9"), -1, -1, -1, -1).Address ?mCropRange(Range(“ B2:Y9”),-1,-1,-1,-1)。地址

$A$1:$Z$10 $ A $ 1:$ Z $ 10

?mCropRange(Range("A1:Z20"), 2, 2, 2, 2).Address ?mCropRange(Range(“ A1:Z20”),2,2,2,2)。地址

$C$3:$X$18 $ C $ 3:$ X $ 18

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

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