Wednesday 10 February 2016

VBA Excel: Adding/Removing Elements to Array

I have a spin button which increases and decreases the dimensions of a given array. Now I need to add and remove elements depending on the dimensions of the array. I'm totally lost right now. Any help is appreciated.



Here is the function which fills a range of cells with the array:




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


Here is one of the two spin button functions:



Private Sub SpinButton2_SpinUp()


Dim dataCell As Range
Set dataCell = Range("E23")

dataCell.Value = dataCell.Value + 1

i = dataCell.Value

End Sub

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...