I am rookie in this any suggestions for running it
Private Sub CommandButton1_Click()
Dim LU As Double, HG As String, CN As Double
LU = Range("B1").Value
HG = Range("G1").Value
If HG = A Then
Select Case LU
Case Is = 1
CN = 77
Case Is = 3
CN = 67
Case Is = 4
CN = 30
Case Is = 5
CN = 39
Case Is = 6
CN = 49
Case Is = 7
CN = 77
Case Is = 12
CN = 98
Case Else
CN = 0
End Select
ElseIf HG = B Then
Select Case LU
Case Is = 1
CN = 85
Case Is = 3
CN = 78
Case Is = 4
CN = 55
Case Is = 5
CN = 61
Case Is = 6
CN = 62
Case Is = 7
CN = 86
Case Is = 12
CN = 98
Case Else
CN = 0
End Select
ElseIf HG = C Then
Select Case LU
Case Is = 1
CN = 90
Case Is = 3
CN = 85
Case Is = 4
CN = 70
Case Is = 5
CN = 74
Case Is = 6
CN = 74
Case Is = 7
CN = 91
Case Is = 12
CN = 98
Case Else
CN = 0
End Select
ElseIf HG = D Then
Select Case LU
Case Is = 1
CN = 92
Case Is = 3
CN = 89
Case Is = 4
CN = 77
Case Is = 5
CN = 80
Case Is = 6
CN = 85
Case Is = 7
CN = 94
Case Is = 12
CN = 98
Case Else
CN = 0
End Select
ElseIf HG = NA Then
CN = 98
End If
Range("G1").Value = CN
End Sub
Looking at your variable declarations it appears that you want the code to run based on cell values.
Dim LU As Double, HG As String, CN As Double
LU = Range("B1").Value
HG = Range("G1").Value
If HG = A Then
Select Case LU
I'm guessing that you haven't used the Option Explicit directive at the top of your module so VBA is treating A as a variable instead of a string. I think you want this:
Dim LU As Double, HG As String, CN As Double
LU = Range("B1").Value
HG = Range("G1").Value
If HG = "A" Then
Select Case LU
If that's right then you should probably take care to use Trim$ when you pull the range value (unless you do not want the code to match on A followed by spaces).
You also want to turn on Option Explicit. It will help to trap these types of errors in your code.
Related
Is it possible to declare (automatically) variables in vba and with increasing number?
I have to Dim 50 variables, like Var1 till Var50
Is it possible with vba code
All my efforts (for next with concatenate) didn't work
The result should be;
Dim Var1 as integer
Dim Var2 as integer
.
.
Dim Var50 as integer
Use
Dim Var(1 to 50) as integer
Then you can set values for each like so
Var(1) = 1
Var(2) = 2
Var(3) = 3
Var(4) = 4
Var(5) = 5
Var(6) = 6
Var(7) = 7
Var(8) = 8
Var(9) = 9
Var(10) = 10
Var(11) = 11
Var(12) = 12
Var(13) = 13
Var(14) = 14
Var(15) = 15
Var(16) = 16
Var(17) = 17
Var(18) = 18
Var(19) = 19
Var(20) = 20
Var(21) = 21
Var(22) = 22
Var(23) = 23
Var(24) = 24
Var(25) = 25
Var(26) = 26
Var(27) = 27
Var(28) = 28
Var(29) = 29
Var(30) = 30
Var(31) = 31
Var(32) = 32
Var(33) = 33
Var(34) = 34
Var(35) = 35
Var(36) = 36
Var(37) = 37
Var(38) = 38
Var(39) = 39
Var(40) = 40
Var(41) = 41
Var(42) = 42
Var(43) = 43
Var(44) = 44
Var(45) = 45
Var(46) = 46
Var(47) = 47
Var(48) = 48
Var(49) = 49
Var(50) = 50
I'm working with parsing emails for a project i'm working on.
so far I connect to a pop3 mail server, download all of the mail thats there and loop through it getting the sender, subject and body.
I then decode the base64 body, which leaves me with a multi-part MIME message, like the following test email i sent myself...
I need to be able to split this Multipart MIME email body so that I can have one string which contains just the plain text version of the mail and another string which contains the html part.
I'm not interested in anything else the mail might have... attachments and suchlike can all get dropped.
Can anyone point me in the right direction?
If i'm going to be looking at using a 3rd party control, does anyone know of anything freeware that would be able to do this? I would never need to encode, just decode.
Assuming you have the headers in the email which you have extracted so that you can get the string used to identify the part boundaries in the email, you can get some way through the parsing with code like this:
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sampleEmail = File.ReadAllText("C:\temp\SampleEmail.eml")
Dim getBoundary As New Regex("boundary=(.*?)\r\n")
Dim possibleBoundary = getBoundary.Matches(sampleEmail)
Dim boundary = ""
If possibleBoundary.Count = 0 Then
Console.WriteLine("Could not find boundary specifier.")
End
End If
' the boundary string may or may not be surrounded by double-quotes
boundary = possibleBoundary(0).Groups(1).Value.Trim(CChar(""""))
Console.WriteLine(boundary)
boundary = vbCrLf & "--" & boundary
Dim parts = Regex.Split(sampleEmail, Regex.Escape(boundary))
Console.WriteLine("Number of parts: " & parts.Count.ToString())
' save the parts to one text file for inspection
Using sw As New StreamWriter("C:\temp\EmailParts.txt")
For i = 0 To parts.Count - 1
' this is where you would find the part with "Content-Type: text/plain;" -
' you may also need to look at the charset, e.g. charset="utf-8"
sw.WriteLine("PART " & i.ToString())
sw.WriteLine(parts(i))
Next
End Using
Console.ReadLine()
End Sub
End Module
The email I used to test that did not have any base-64 encoding involved.
I would recommend using my free/open source MimeKit library to accomplish this task as opposed to using a regex solution.
I don't really know VB.NET, so the following code snippet might not be quite right (I'm a C# guy), but it should give you the general idea of how to accomplish the task you want:
Dim message = MimeMessage.Load ("C:\email.msg");
Dim html = message.HtmlBody;
Dim text = message.TextBody;
As you can see, MimeKit makes this sort of thing extremely trivial.
A = E1 = 80 =
= B8 = E1 = 80 = 80 = E1 = 80 = BC = E1 = 80-8A = E1 = 80 = BA; = 50 = 61 = 74 = 69 = 65 = 6E = 74 ;;
-PRINTABLE: = 50 = 61 = 74 = 69 = 65-6 E = 74 = 20 = E1 = 80 = 99 = E1 = 80 = 81 = E1 = 80 = 84 = E1 = 80 = BA =
E1 = 81 = 80 =
= E1 = 80 = 84 = E1 = 80 = BA = E1 = 80 = B8 = E1 = 80 = 80 = E1 = 80 = BC = E1 = 80 = 8A = E1 = 80 = BA
B = E1 = 80 = AD = E1 = 80 = AF; = 50 = 61 = 74 = 69 = 65 =
6E = 74 ;;
E1 = 80 = AF =
END: VCARD
In Shadowrun 5e there is a rule concerning explosives in confined spaces called the 'Chunky Salsa Effect'. I am trying to create a program that simulates the 'Chunky Salsa Effect'. I have most of the program working, however, as the size of the space [meters] increases, the number of blast reflections [bounces] increases. It should be the inverse, the blast falloff [minusMeters] should decrease the [bounces]. [walls] determines the number of surfaces the blast reflects off of. [damageDice] is equal to the base damage of the explosive [baseDamageDice] + (([damageDice]/2) per reflection). For some reason [damageDice] and [bounces] do not decrease when [meters] is increased.
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim meters As Integer = 0
Dim dice As Integer = 0
Dim damageDice As Integer = 0
Dim baseDamageDice As Integer = 0
Dim bounces As Integer = 0
Dim hits As Integer = 0
Dim walls As Integer = 0
Dim minusMeters As Integer = 0
Dim explosiveType As Integer = 0
Dim ap As String = "-"
Dim diceRoll As Integer = 0
Randomize()
walls = CInt(txtWalls.Text)
explosiveType = cbExplosiveType.SelectedIndex
If explosiveType < 0 Then
explosiveType = 0
cbExplosiveType.SelectedIndex = 0
End If
Select Case explosiveType
Case 0
minusMeters = 1
baseDamageDice = 18
ap = "+5"
Case 1
minusMeters = 2
baseDamageDice = 16
ap = "-2"
Case 2
minusMeters = 4
baseDamageDice = 24
ap = "-4"
Case 3
minusMeters = 4
baseDamageDice = 24
ap = "-10"
Case 4
minusMeters = 1
baseDamageDice = 23
ap = "+5"
Case 5
minusMeters = 2
baseDamageDice = 21
ap = "-2"
End Select
diceRoll = CInt(Math.Floor((6 - 1 + 1) * Rnd())) + 1
If diceRoll >= 5 Then
hits = hits + 1
End If
dice = baseDamageDice
For x = 1 To walls
meters = CInt(txtMeters.Text)
damageDice = baseDamageDice
Do
damageDice = Math.Round(damageDice / 2)
dice = dice + damageDice
meters = meters - minusMeters
bounces = bounces + 1
Loop While meters >= 0
Next
For i As Integer = 1 To dice
diceRoll = CInt(Math.Floor((6 - 1 + 1) * Rnd())) + 1
If diceRoll >= 5 Then
hits = hits + 1
End If
Next
lblHitsPerBounce.Text = Math.Round(hits / bounces)
lblTimes.Text = bounces
lblResult.Text = dice
lblNumHits.Text = hits
lblAPOutput.Text = ap
End Sub
I received code with an object (we'll call it Obj) with a property named Number. An array of these objects is defined with 40 objects in indexes 0..39. I don't need object at index 0.
The code has a static variable saving the sum of all Number's of Obj's in the array. This variable is initialized only once. Right after the Number values themselves are defined.
It usually works, but sometimes I log an exception (which I haven't been able to reproduce) with values of different objects and I see there the value of NumberAll is wrong (I've recently logged 1722 and 2134. Not that it should matter - but the only common prime factor for them is 2. should be 1554, if you were wondering, you can check me below :) ).
EDIT: The exception is an Index was outside the bounds of the array on the loop brought in the code below - a direct effect of this issue.
I've tried two methods of summing, brought below.
Relevant code follows. There is nowhere else in the code that a value is assigned to NumberAll.
Static NotFirstTime As Boolean, NumberAll As Integer
If NotFirstTime = False Then GoTo Data
sPoint: 'Code and more code
' ...
' ...
breakValue = someValue Mod NumberAll
Sum = 0
I = 1
Try
Do
If Sum + myArray(I).Number >= breakValue Then
Exit Do
End If
Sum = Sum + myArray(I).Number
I = I + 1
Loop
Catch ex As Exception
'log error and variable data - breakValue is larger than Sum will ever get
'hence the loop reaches the point of trying myArray(40) which doesn't exist
End Try
' ...
Data:
NotFirstTime = True
myArray(1).Number = 37
myArray(2).Number = 34
myArray(3).Number = 44
myArray(4).Number = 31
myArray(5).Number = 59
myArray(6).Number = 26
myArray(7).Number = 33
myArray(8).Number = 28
myArray(9).Number = 20
myArray(10).Number = 13
myArray(11).Number = 92
myArray(12).Number = 65
myArray(13).Number = 71
myArray(14).Number = 22
myArray(15).Number = 22
myArray(16).Number = 42
myArray(17).Number = 26
myArray(18).Number = 26
myArray(19).Number = 33
myArray(20).Number = 34
myArray(21).Number = 22
myArray(22).Number = 19
myArray(23).Number = 85
myArray(24).Number = 72
myArray(25).Number = 47
myArray(26).Number = 40
myArray(27).Number = 47
myArray(28).Number = 54
myArray(29).Number = 48
myArray(30).Number = 44
myArray(31).Number = 37
myArray(32).Number = 34
myArray(33).Number = 44
myArray(34).Number = 9
myArray(35).Number = 57
myArray(36).Number = 37
myArray(37).Number = 19
myArray(38).Number = 13
myArray(39).Number = 68
NumberAll = 0
'Only one of the following methods is used. They seem to give the same result. The bug exists in both.
'Method one
For I = 1 To 39 'E מספר המסכתות
NumberAll = NumberAll + myArray(I).Number
Next I
'Method two
Dim myList As List(Of Obj) = New List(Of Obj)(myArray)
NumberAll = myList.Sum(Function(b) b.Number)
NumberAll -= myArray(0).Number
GoTo sPoint
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.