Is there a faster way to define the following variables. notAct is the name, after the name comes the cells positions. The "b" is for the column and the number refers to the row. The value is 0. Very single cell has to be defined so that I can use the variables.
Dim notAct_b1, notAct_c1, notAct_d1, ..., notAct_d20 As Integer
notAct_b1 = 0
notAct_c1 = 0
notAct_d1 = 0
notAct_b2 = 0
notAct_c2 = 0
notAct_d2 = 0
notAct_b3 = 0
notAct_c3 = 0
notAct_d3 = 0
notAct_b4 = 0
notAct_c4 = 0
notAct_d4 = 0
notAct_b5 = 0
notAct_c5 = 0
notAct_d5 = 0
notAct_b6 = 0
notAct_c6 = 0
notAct_d6 = 0
notAct_b7 = 0
notAct_c7 = 0
notAct_d7 = 0
notAct_b8 = 0
notAct_c8 = 0
notAct_d8 = 0
notAct_b9 = 0
notAct_c9 = 0
notAct_d9 = 0
notAct_b10 = 0
notAct_c10 = 0
notAct_d10 = 0
notAct_b11 = 0
notAct_c11 = 0
notAct_d11 = 0
notAct_b12 = 0
notAct_c12 = 0
notAct_d12 = 0
notAct_b13 = 0
notAct_c13 = 0
notAct_d13 = 0
notAct_b14 = 0
notAct_c14 = 0
notAct_d14 = 0
notAct_b15 = 0
notAct_c15 = 0
notAct_d15 = 0
notAct_b16 = 0
notAct_c16 = 0
notAct_d16 = 0
notAct_b17 = 0
notAct_c17 = 0
notAct_d17 = 0
notAct_b18 = 0
notAct_c18 = 0
notAct_d18 = 0
notAct_b19 = 0
notAct_c19 = 0
notAct_d19 = 0
notAct_b20 = 0
notAct_c20 = 0
notAct_d20 = 0
First of all, you don't need to initialize an Integer variable in VBA - it will be 0 after it is Dimmed.
However, instead of defining 4x20 variables, better use an array:
Dim notAct(1 to 20,1 to 43) As Integer
If you need to initialize or reset all values, you can use a small loop:
Dim col As Integer, row As Long
'Initialize/reset
For col = 1 To 4
For row = 1 To 20
notAct(row, col) = 0 'setting 0 not required after Dim
Next row
Next col
If you need to assign the value of a variable to a cell, use this syntax:
'Assigns a value from the array to B3
Cells(3, 2).Value = notAct(3, 2)
And best of all, if you want to assign all 4x20 cells with their values, use this one line of code:
Range("A1:D20").Value = notAct
Related
Looking to count bits in a sequence.
1's are counted as 1, Zero's are counted together in a sequence between 1's
If you look at this picture
The output should be 2,1,5,1,1,3,2,1
Here is my code it works on 80% of numbers sometimes it messes up.
dim bitmask() as byte
redim bitmask(15)
bitmask(0) = 0
bitmask(1) = 0
bitmask(2) = 1
bitmask(3) = 0
bitmask(4) = 0
bitmask(5) = 0
bitmask(6) = 0
bitmask(7) = 0
bitmask(8) = 1
bitmask(9) = 1
bitmask(10) = 0
bitmask(11) = 0
bitmask(12) = 0
bitmask(13) = 1
bitmask(14) = 0
bitmask(15) = 1
Public Function GetBitCount() As Byte()
Dim count As Byte = 0
Dim bitcounts As New List(Of Byte)
Dim indexOfNext As Integer = 0
Dim totalCounted As Integer = 0
While True
indexOfNext = Array.IndexOf(bitmask, CByte(1), indexOfNext + 1)
If indexOfNext > 0 Then
If indexOfNext - totalCounted = 1 Then
bitcounts.Add(2)
bitcounts.Add(1)
ElseIf indexOfNext - totalCounted > 0 Then
bitcounts.Add(IIf(totalCounted > 0, indexOfNext - totalCounted, indexOfNext))
ElseIf indexOfNext - totalCounted > 1 Then
bitcounts.Add(2)
bitcounts.Add(1)
Else
bitcounts.Add(1)
bitcounts.Add(1)
End If
If totalCounted = 0 Then bitcounts.Add(1)
totalCounted = indexOfNext + 1
Else
Exit While
End If
End While
If totalCounted - 1 > 2 AndAlso totalCounted - 1 < bitmask.Length - 1 Then
bitcounts.Add(1)
bitcounts.Add((bitmask.Length - 1) - (totalCounted - 1))
ElseIf totalCounted - 1 <= 2 AndAlso totalCounted - 1 < bitmask.Length - 1 Then
bitcounts.Add((bitmask.Length - 1) - (totalCounted - 1))
Else
bitcounts.Add(1)
End If
If count > 0 Then bitcounts.Add(count)
Return bitcounts.ToArray()
End Function
Solution
Public Function GetBitCount() As Byte()
Dim i As Integer = 0
Dim count As Byte = 0
Dim bitcounts As New List(Of Byte)
For i = 0 To bitmask.Length - 1
If bitmask(i) = 1 Then
bitcounts.Add(count)
count = 0
End If
count += 1
Next
If count > 0 Then bitcounts.Add(count)
Return bitcounts.ToArray()
End Function
I have to know what language is used here. In which language do we define a variable as $value = x
Does this language have loop controls? I need this information, can any one provide?
Public sub Annual_Leave()
$Decimal_Output1 = 0 //variables
$Decimal_Output2 = 0
IF #ServiceMonths# = 1 OR #ServiceMonths# = 2 THEN
$Decimal_Output1 = 0
ELSEIF #ServiceMonths# => 3 AND #ServiceMonths# <= 12 THEN
IF #ServiceMonths# = 3 THEN
$Decimal_Output1 = 1.7
END IF
ELSE
$Decimal_Output1 = 21
$Decimal_Output2 = 0
END IF
End sub
I'm trying to send this to SerialPort to command the devices' LED to lit on.
SData(0) = 85
SData(1) = 170
SData(2) = 36
SData(3) = 1
SData(4) = 2
SData(5) = 0
SData(6) = 1
SData(7) = 0
SData(8) = 0
SData(9) = 0
SData(10) = 0
SData(11) = 0
SData(12) = 0
SData(13) = 0
SData(14) = 0
SData(15) = 0
SData(16) = 0
SData(17) = 0
SData(18) = 0
SData(19) = 0
SData(20) = 0
SData(21) = 0
SData(22) = 39
SData(23) = 1
SerialPort1.Open()
For i = 0 To 23
SerialPort1.Write(Chr(SData(i)))
Next
SerialPort1.Close()
I don't get response from the device or anything. Then i noticed that the module freezes for 2 seconds, i think it's the loop.
I think i need to know the steps of how a SerialPort runs from open to close.
Btw, there's a original copy of this but it was in VB6
here's whats working: MSComm1.Output = Chr(SData(i))
Dim group11_0_count = 0
Dim group11_1_count = 0
Dim group11_2_count = 0
Dim m As Integer = 0
Dim n As Integer = 0
Dim increment2 As Integer
For m = 0 To machings2.Items.Count - 1
For n = 0 To 3
If machings2.Items(m).ToString.Chars(n) = "1" Then
increment2 = increment2 + 1
End If
Next
If (increment2 = 0) Then
group11_0_count = group11_0_count + 1
group11_1_0.Items.Add(machings2.Items(m))
End If
If (increment2 = 1) Then
group11_1_count = group1_1_count + 1
group11_1_1.Items.Add(machings2.Items(m))
End If
If (increment2 = 2) Then
group11_2_count = group1_2_count + 1
group11_1_2.Items.Add(machings2.Items(m))
End If
increment2 = 0
Next
If (group11_0_count > 0 AndAlso group11_1_count > 0) Then
Dim result = ""
Dim index As Integer = 0
Dim gg As Integer = 0
Dim hh As Integer = 0
Dim i As Integer = 0
For hh = 0 To group11_1_count - 1
For gg = 0 To group11_0_count - 1
result = ""
index = 0
For i = 0 To 3
If group11_1_0.Items(gg).ToString.Chars(i) <> group11_1_1.Items(hh).ToString.Chars(i) Then
result &= "-"
index = index + 1
Else
result &= group11_1_0.Items(gg).ToString.Chars(i)
End If
Next
If (index = 1) Then
machings3.Items.Add(result)
End If
Next
Next
End If
I am comparing the items of two combobox items like that
combobox1 items
0000
combobox items
0001
0010
the result will be like that in machings3 combobox
000-
00-0
Here the differnce between two items indicated by - sign
But i am getting InvalidArgument=Value of '2' is not valid for 'index'.
I Can't make sense out of your source and where the IndexOutOfRangeException occurs. But you know that you need 3 Items in a Combobox to access Item with Index 2?! Every collection starts with 0.
I am new to VB.net and facing a strange situation. I have a structure that contains another structure. The inner structure is nullable. Now wht I do is something like the following. I create a new instance of the inner structure, set the member variables and assign the whole structure to the inner structure of the parent. But it is giving an error. The assignment is not successful. If I try to peek into the structure in the watch window, it says "property evaluation failed" for the HasValue and Value properties.
Dim testData As List(Of TestData) = Nothing
Dim testData_List1 As New TestData
With testData_List1.commonTestParam
.AccuchekActiveEnergy = 2.56
.AccuchekActiveEnergyUnit = ActiveEnergyUnit.KiloWattHour
.AccuchekApparentEnergy = 34.56
.AccuchekApparentEnergyUnit = ApparentEnergyUnit.VoltAmpereHour
.AccuchekFrequency = 1
.AccuchekRange = "20474 ewr 34324"
.AccuchekType = AccuchekType.AccuchekOne
.ActiveLoadRPhase = 145
.AvgActiveLoad = 2.56
.AvgActiveLoadUnit = ActiveLoadUnit.Watt
.AvgPowerFactor = 0
.AvgPowerFactorType = PowerFactorType.PFLag
.ConditionalFlag1 = 0
.ConsumerNo = "343122050242"
.CurrentRPhase = 1
.GeneralFlag1 = 0
.InstantaneousPFRPhase = 2
.ManufacturingYear = 2009
.MeterActiveEnergy = 258.89
.MeterActiveEnergyUnit = ActiveEnergyUnit.KiloWattHour
.MeterActiveError = 20
.MeterApparentError = 14
.MeterConstant = 3200
.MeterConstantUnit = MeterConstantUnit.RevsPerkVAh
.MeterMake = "DS"
.MeterSNo = "6563402"
.MeterTypeAndClass = MeterTypeAndClass.MTCElectorMechWith20
.MTFID = "123456789"
.NoofTestRevolutions = 100
.PulseCriteria = 0
.RatedBasic = 25
.RatedMax = 30
.RatedVoltage = 15
.ReactiveCurrentRPhase = 145
.RemarkID = 0
.TestDateAndTime = "100320101545"
.TestDuration = 2145
.TesterCode = 0
.TestID = "147852"
.TestMode = TestMode.TMOpticalScanner
.TestNumber = 0
.VoltageRPhase = 145
End With
Dim accuchek3TestParameters1 As New Accuchek3PhaseTestParameters
With accuchek3TestParameters1
.AccuchekReactiveLagEnergy = 2.46
.AccuchekReactiveLagEnergyUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.AccuchekReactiveLeadEnergy = 2.56
.AccuchekReactiveLeadEnergyUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.ActiveLoadBPhase = 14
.ActiveLoadYPhase = 15
.AvgApparentLoad = 10
.AvgApparentLoadUnit = ApparentLoadUnit.KiloVoltAmpere
.AvgReactiveLagLoad = 14
.AvgReactiveLagLoadUnit = ReactiveLoadUnit.KiloVoltAmpereReactive
.AvgReactiveLeadLoad = 15
.AvgReactiveLeadLoadUnit = ReactiveLoadUnit.KiloVoltAmpereReactive
.ConditionalFlag2 = 0
.ConditionalFlag3 = 0
.CTRatio = 1.23
.CurrentBPhase = 10
.CurrentYPhase = 11
.InstantaneousPFBPhase = 0
.InstantaneousPFYPhase = 1
.MeterApparentEnergy = 1.01
.MeterApparentUnit = ApparentEnergyUnit.KiloVoltAmpereHour
.MeterReactiveLagEnergy = 1.25
.MeterReactiveLagError = 1.25
.MeterReactiveLagUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.MeterReactiveLeadEnergy = 1.45
.MeterReactiveLeadError = 1.56
.MeterReactiveLeadUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.PercentageLoad = 1
.PTRatio = 1
.ReactiveCurrentBPhase = 10
.ReactiveCurrentYPhase = 11
.VoltageBPhase = 10
.VoltageYPhase = 10
End With
testData_List1.accuchek3TestParameters = accuchek3TestParameters1
testData.Add(testData_List1)
Can somebody please guide me?
Your first line is:
Dim testData As List(Of TestData) = Nothing
Then at the bottom you do
testData.Add(testData_List1)
I can't see anywhere in between where you do something like:
testData = New List(Of TestData)()
Though it's slightly hard to read since you've got both a variables and types with TestData as their name (or part of their name) so I might just be missing that.