I am generating an email that shows trades for the day in the body of an email. Currently the only way I can do this is with an If then statement for specific numbers of trades. If we traded 10, I need an if then statement with 10 as the variable criteria, but if I only have 9, then I get an error. I want a dynamic method instead. I can do a For Loop that will list all the trades in debug.print, but in the email, it each trade overwrites the prior trade and I show only one line. I also need an intro like "Hi, today today's trades are: " followed by each trade listed below line by line.
I tried this and it works but only if I have the right number of trades and a variable that matches it. For example, if I had 23 trades, I need an If statement with 23 as a variable value in this case m. I used Arrays as VBA does not let me create a list. Unfortunately, I cannot pull the whole array, I need to pull line by line. If I could pull the array, I could just have a simple loop.
If m = 23 Then
.Body = "Hi Chris," & vbLf & vbLf & "The following trade(s) was completed today:" & vbLf & vbLf & ArrayValues(0) & vbLf & ArrayValues(1) & vbLf & ArrayValues(2) & vbLf & ArrayValues(3) & vbLf & ArrayValues(4) & vbLf & ArrayValues(5) & vbLf & ArrayValues(6) & vbLf & ArrayValues(7) & vbLf & ArrayValues(8) & vbLf & ArrayValues(9) & vbLf & ArrayValues(10) & vbLf & ArrayValues(11) & vbLf & ArrayValues(12) & vbLf & ArrayValues(13) & vbLf & ArrayValues(14) & vbLf & ArrayValues(15) & vbLf & ArrayValues(16) & vbLf & ArrayValues(17) & vbLf & ArrayValues(18) & vbLf & ArrayValues(19) & vbLf & ArrayValues(20) & vbLf & ArrayValues(21) & vbLf & ArrayValues(22) & vbLf & vbLf & "Thanks"
End if
I want to use something like:
For b = 1 To LastRow
If Trades.Range("H" & b) = TDate Then
Debug.Print (Range("B" & b) & " " & Range("C" & b) & " " & Range("D" & b) & " " & Range("F" & b) & " " & Range("G" & b))
End If
Next b
This way it does not matter how may trades I have, one formula would over it all. Each Range has a trade characteristic.
If I do a debug.Print in the immediate window I get a list just like I want, but in the email, each line overwrites the prior trade.
I am a rookie at this and appreciate any help. Thanks
Sounds like you need to build a string which can be assigned to the message body. For such tasks you can use String Functions available in VBA where you can prepare the correct string and then assign to the message body.
If m = 23 Then
.Body = "Hi Chris," & vbLf & vbLf & "The following trade(s) was completed today:" & vbLf & vbLf & ArrayValues(0) & vbLf & ArrayValues(1) & vbLf & ArrayValues(2) & vbLf & ArrayValues(3) & vbLf & ArrayValues(4) & vbLf & ArrayValues(5) & vbLf & ArrayValues(6) & vbLf & ArrayValues(7) & vbLf & ArrayValues(8) & vbLf & ArrayValues(9) & vbLf & ArrayValues(10) & vbLf & ArrayValues(11) & vbLf & ArrayValues(12) & vbLf & ArrayValues(13) & vbLf & ArrayValues(14) & vbLf & ArrayValues(15) & vbLf & ArrayValues(16) & vbLf & ArrayValues(17) & vbLf & ArrayValues(18) & vbLf & ArrayValues(19) & vbLf & ArrayValues(20) & vbLf & ArrayValues(21) & vbLf & ArrayValues(22) & vbLf & vbLf & "Thanks"
End if
Your strategical mistake is that you are trying to set up the Body property for a specific case. Instead, consider concatenating the string with the required piece of data (another string) which makes sense for a particular case. And only when you are done adding all the bits to the result string, you can assign it to the message body.
Be aware, the Body property is a plain text string and doesn't deliver any formatting. Instead, I'd suggest using the HTMLBody property instead. The HTMLBody property should be an HTML syntax string. Setting the HTMLBody property will always update the Body property immediately.
I'm creating a utility for a game that creates spreadsheets of data by reading the game's code.
One of the most basic aspects of this is being able to read the game's code efficiently. The game is written in C#. I'm using VB.Net for my program because my mind can't grasp the abstraction of more complex languages. Historically the way I had my program recognise code blocks in the code text file is by counting the brackets that encapsulated them and working off of that. This seems a bit clunky, though. Is there any simpler way to help the program figure out code blocks?
For Example, code in the text file might look like this:
6673={
key="d_example_duchy_1"
de_facto_liege=12
variables={
data={ {
flag="traditional_de_jure_kingdom"
tick=0
data={
type=lt
identity=6632
}
}
}
}
de_jure_liege=6632
de_jure_vassals={ 6674 6678 6682 6686 }
holder=558
name="Example Duchy 1"
date=1253.1.1
heir={ 880 }
history={ 1253.1.1=558 }
capital=6674
history_government="tribal_government"
coat_of_arms_id=2320
}
6674={
key="c_example_county_1"
variables={
data={ {
flag="influx_culture"
tick=0
data={
type=culture
identity=11
}
}
{
flag="influx_faith"
tick=0
data={
type=faith
identity=57
}
}
{
flag="fog_resistance"
tick=0
data={
type=value
}
}
}
}
de_jure_liege=6673
de_jure_vassals={ 6675 6676 6677 }
holder=558
name="Example County 1"
date=1243.6.26
heir={ 880 }
capital=6674
coat_of_arms_id=2323
}
6675={
key="b_example_barony_1"
de_facto_liege=6674
de_jure_liege=6674
holder=558
name="Example Barony 1"
date=1254.1.1
capital_barony=yes
duchy_capital_barony=yes
capital=6674
coat_of_arms_id=2328
}
6676={
key="b_example_barony_2"
de_facto_liege=6674
de_jure_liege=6674
holder=11609
name="Example Barony 2"
date=1253.7.28
capital=6674
coat_of_arms_id=2332
}
6677={
key="b_example_barony_3"
de_facto_liege=6674
de_jure_liege=6674
holder=558
name="Example Barony 3"
date=1232.2.8
heir={ 880 }
capital=6674
theocratic_lease=yes
coat_of_arms_id=2334
}
Thanks in advance. :)
The solution I eventually settled on, for discerning the overall code-blocks (e.g. all the data nested under 6674={ ... }) was basically:
Dim Code As String = File.ReadAllText("PATH/TO/CODE/FILE.txt")
Dim Output As List(Of String) = Code.Split(vbCrLf & "}" & vbCrLf).ToList
So in plain English, reading all the code into a single string, then splitting the string according to how the original code's language distinguished the initial nest of code (i.e. a carriage return followed by a curly bracket followed by another carriage return).
The list at the end would look something like this:
Output(0)
6673={" & vbCrLf & " key=""d_example_duchy_1""" & vbCrLf & " de_facto_liege=12" & vbCrLf & " variables={" & vbCrLf & " data={ {" & vbCrLf & " flag=""traditional_de_jure_kingdom""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=lt" & vbCrLf & " identity=6632" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " de_jure_liege=6632" & vbCrLf & " de_jure_vassals={ 6674 6678 6682 6686 }" & vbCrLf & " holder=558" & vbCrLf & " name=""Example Duchy 1""" & vbCrLf & " date=1253.1.1" & vbCrLf & " heir={ 880 }" & vbCrLf & " history={ 1253.1.1=558 }" & vbCrLf & " capital=6674" & vbCrLf & " history_government=""tribal_government""" & vbCrLf & " coat_of_arms_id=2320
Output(1)
6674={" & vbCrLf & " key=""c_example_county_1""" & vbCrLf & " variables={" & vbCrLf & " data={ {" & vbCrLf & " flag=""influx_culture""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=culture" & vbCrLf & " identity=11" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " {" & vbCrLf & " flag=""influx_faith""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=faith" & vbCrLf & " identity=57" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " {" & vbCrLf & " flag=""fog_resistance""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=value" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " de_jure_liege=6673" & vbCrLf & " de_jure_vassals={ 6675 6676 6677 }" & vbCrLf & " holder=558" & vbCrLf & " name=""Example County 1""" & vbCrLf & " date=1243.6.26" & vbCrLf & " heir={ 880 }" & vbCrLf & " capital=6674" & vbCrLf & " coat_of_arms_id=2323
And so on with the remaining three.
In vb.net in debugging you can watch a variable and right click.
There are options between copy and copy value.
There is little differences. I basically got this
"[" & vbCrLf & " {" & vbCrLf & " ""id"": 22812485751," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1539721209920," & vbCrLf & " ""price"": 2.726E-05," & vbCrLf & " ""quantity"": 1733.83246147," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 8.508E-05," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1539727280096" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19661583751," & vbCrLf & " ""currencyPair"": ""ORME/ETH""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_BUY""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1537113087231," & vbCrLf & " ""price"": 0.00110001," & vbCrLf & " ""quantity"": 1733.83246562," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 0.003433," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1538092923124" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19669018551," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1537118640003," & vbCrLf & " ""price"": 4.302E-05," & vbCrLf & " ""quantity"": 1140.08057366," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 8.829E-05," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1537119048771" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19666104251," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""PARTIALLY_FILLED_AND_CANCELLED""," & vbCrLf & " ""issueTime"": 1537116476248," & vbCrLf & " ""price"": 4.25E-05," & vbCrLf & " ""quantity"": 7487.1," & vbCrLf & " ""remainingQuantity"": 1140.08057366," & vbCrLf & " ""commissionByTrade"": 0.00048563," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1537116507585" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19661490851," & vbCrLf & " ""currencyPair"": ""ETH/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_BUY""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1537113025233," & vbCrLf & " ""price"": 0.03357901," & vbCrLf & " ""quantity"": 1.91066607," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 0.00011548," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1537113028429" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 17637942051," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1535545801765," & vbCrLf & " ""price"": 4.5E-05," & vbCrLf & " ""quantity"": 2289.92000001," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 0.00018916," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1535545801765" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 17637532551," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""PARTIALLY_FILLED_AND_CANCELLED""," & vbCrLf & " ""issueTime"": 1535545474694," & vbCrLf & " ""price"": 4.78E-05," & vbCrLf & " ""quantity"": 2487.1," & vbCrLf & " ""remainingQuantity"": 2289.92000001," & vbCrLf & " ""commissionByTrade"": 1.697E-05," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1535545515636" & vbCrLf & " }" & vbCrLf & "]"
And that sucks and hard to read. If a string contains a json, I want to see the json as it actually is. I want to be able to copy that to notepad.
How do I do so?
Here is what you can do when debugging.
put mouse over your variable. Popup menu will show up and there you will see magnifying lens
click that
Right-click on your variable, select "Quick Watch" in the menu a screen will show
click magnifying lens
I am making a form that the user fills out and when complete, they click a command button and the text they inputted is saved as a text file in a folder in the C: drive called Survey Results.
Currently, the user has to create the file "Survey Results" manually, and if it does not exist in the C: drive, there is an error in Word. I want some way to check if the destination exists before this error occurs and either prompt the user to create the folder or automatically create the folder.
What would I include in my while loop to check for this?
do while ()
messagebox1.open
loop
Private Sub CommandButton1_Click()
Dim FS As FileSystemObject
Set FS = New FileSystemObject
Dim MyFile As TextStream
Dim i As Integer
Dim FilePrefix As String, FileName As String, Extension As String
If CommandButton1.Enabled = True Then
FilePrefix = "C:\SurveyResults\"
Extension = ".txt"
i = 1
FileName = FilePrefix & comop & Trim(Str(i)) & Extension
Do While (FS.FileExists(FileName))
i = i + 1
FileName = FilePrefix & comop & Trim(Str(i)) & Extension
Loop
Set MyFile = FS.CreateTextFile(FileName)
MyFile.Write num1 & vbNewLine & num2 & vbNewLine & _
num3 & vbNewLine & num4 & vbNewLine & _
num5 & vbNewLine & num6 & vbNewLine & _
num7 & vbNewLine & num8 & vbNewLine & num9 & vbNewLine & num10 & vbNewLine & num11 _
& vbNewLine & num12 & vbNewLine & num13 & vbNewLine & num14 & vbNewLine _
& num15 & vbNewLine & num16 & vbNewLine & num17 & vbNewLine & num18 & vbNewLine & num19 & vbNewLine _
& num20 & vbNewLine & num21 & vbNewLine & num22 & vbNewLine & num23 & vbNewLine _
& num24 & vbNewLine & num25 & vbNewLine & num26 & vbNewLine & num27 & vbNewLine & num28 & vbNewLine _
& num29 & vbNewLine & num30 & vbNewLine & num31 & vbNewLine & num32 & vbNewLine & num33 & vbNewLine _
& com1a & vbNewLine & com1b & vbNewLine & com1c & vbNewLine & com1d & vbNewLine & com1e & vbNewLine _
& com1f & vbNewLine & com1g & vbNewLine & com1i & vbNewLine & com1j & vbNewLine & com1k & vbNewLine _
& com2a & vbNewLine & com2b & vbNewLine & com2c & vbNewLine & com2d & vbNewLine & com2e & vbNewLine _
& com2f & vbNewLine & com2g & vbNewLine _
& com3a & vbNewLine & com3b & vbNewLine & com3c & vbNewLine & com3d & vbNewLine & com3e & vbNewLine _
& com3f & vbNewLine _
& com4a & vbNewLine & com4b & vbNewLine & com4c & vbNewLine & com4d & vbNewLine & com4e & vbNewLine _
& com4f & vbNewLine & com4g & vbNewLine & com4h & vbNewLine & com4i & vbNewLine & com4j & vbNewLine & com4k & vbNewLine & com4l & vbNewLine _
& com5a & vbNewLine & com5b & vbNewLine & com5c & vbNewLine & com5d & vbNewLine & com5e & vbNewLine _
& com5f & vbNewLine & com5g & vbNewLine & com5h & vbNewLine & com5i & vbNewLine & com5j & vbNewLine _
& com6a & vbNewLine & com6b & vbNewLine & com6c & vbNewLine & com6d & vbNewLine _
& com1hb & vbNewLine & com1hd & vbNewLine _
& com1hf & vbNewLine & com1hh & vbNewLine & com1hj & vbNewLine & comop
End If
End Sub
Do something like the below snippet:
Dim FilePrefix$, fs As New Scripting.FileSystemObject
FilePrefix = "C:\SurveyResults\"
If Dir(FilePrefix, vbDirectory) <> "" Then
'directory exists
Else
'directory does not exist
fs.CreateFolder FilePrefix ' create the dir
End If
When I try to write text to a file, I get an error saying "FormatException was unhandled"
Here's the code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ChampPath As String = FolderBrowserDialog1.SelectedPath & "\League of Legends\Config\Champions"
Dim NamePath As String = ChampPath & "\" & SelectChampion.Text
Dim FilePath As String = NamePath & "\" & SelectChampion.Text & ".json"
Dim Map As String
Dim Mode As String
System.IO.Directory.CreateDirectory(NamePath)
System.IO.File.Create(FilePath).Dispose()
If (SelectMap.Text = "Any") Then
Map = "any"
ElseIf (SelectMap.Text = "Summoners Rift") Then
Map = "1"
ElseIf (SelectMap.Text = "Twisted Treeline") Then
Map = "10"
ElseIf (SelectMap.Text = "Crystal Scar") Then
Map = "8"
ElseIf (SelectMap.Text = "Proving Grounds") Then
Map = "3"
End If
If (SelectMode.Text = "Any") Then
Mode = "any"
ElseIf (SelectMode.Text = "Classic") Then
Mode = "CLASSIC"
ElseIf (SelectMode.Text = "Dominon") Then
Mode = "ODIN"
ElseIf (SelectMode.Text = "Proving Grounds") Then
Mode = "ARAM"
End If
If (System.IO.File.Exists(FilePath)) Then
Using Writer As StreamWriter = New StreamWriter(FilePath)
Writer.Write("{" & vbNewLine &
" ""champion"":""" & SelectChampion.Text & """," & vbNewLine &
" ""title"":""" & TitleBox.Text & "", " " & vbNewLine &
" ""type"":""" & TypeBox.Text & "", " " & vbNewLine &
" ""map"":""" & Map & "", " " & vbNewLine &
" ""mode"":""" & Mode & "", " " & vbNewLine &
" ""priority""" & SelectPriority.Text & "", " " & vbNewLine &
" ""blocks"":[ " & vbNewLine &
"{" & vbNewLine &
" ""type"":""starting"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""1001"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3010"", " & vbNewLine &
" ""count"":3 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""type"":""essential"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""3001"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3089"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""type"":""offensive"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""3100"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3128"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3135"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""type"":""defensive"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""3140"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3157"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}")
End Using
End If
End Sub
I can't find where the error is actually coming from...
The text also needs to be formatted like it is, with the quotes and what not. Thanks for any help.
Assumed VB Net .. Better you change like this
Writer.Write("{" & vbNewLine & _
" champion : " & SelectChampion.Text & "," & vbNewLine & _
" title : " & TitleBox.Text & "," & vbNewLine & _
" type : " ........
...... etc