I must have done something wrong with my computer. For some reasons, many program think the decimal separator is , instead of.
So I run this
'do stupid things
Dim c = System.Globalization.CultureInfo.DefaultThreadCurrentCulture
'Dim d = System.Globalization.NumberFormatInfo.NumberDecimalSeparator
Dim hello = JObject.Parse("{""volume"": 1.5}")
Dim hello1 = hello.Item("volume")
Dim hello2 = hello1.ToString
Dim d = Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator
LogEvents("Result of Hello2 =" + hello2)
LogEvents("Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator =" + d)
End ' end program
21/03/2022 17:34:27 Result of Hello2 =1,5
21/03/2022 17:34:27 Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator =.
Curious, I run the exact program on another computer.
The result is
3/21/2022 6:09:06 PM Result of Hello2 =1.5
3/21/2022 6:09:06 PM Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator =.
So for some unknown reason, there is a setting in my computer that makes program think the decimal separator is , instead of .
However, the value of Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator is always "." in BOTH computers.
I wonder what settings I should have changed
Oh it turns out, in my computer hours are written in 24 hours format too. I wonder why?
If I go to control panel I see a settings where decimal separator is , instead of .
So I reset the whole settings. After resetting the decimal separator is still ,.
I have no idea why
I chose region english USA and english Indonesia. Decimal separator always ends up in comma.
I suppose I can change that manually to dot but why the hell it's default to comma in the first place?
Related
My software creates PAIN001.XML files directly from an Access financial database. The decimal separator must always be a dot. The numbers are formatted with:
MyText = Format(MyNumber, "#0.00")
However, the format string's dot is automatically replaced by the system decimal separator, which might be "," instead of "." !
In Excel there are easy solutions, for example:
Application.DecimalSeparator = "."
...
However, MS Access doesn't recognize this application property.
Is there a simple way to define a decimal separator within Access vba code ?
Of course, one can create a function which scans each MyText number for wrong decimal separators and replaces them with a dot, but this function would have to be called separately for each number, slowing down the code quite a lot…
The decimal separator must always be a dot.
Then use Str:
MyText = Str(MyNumber)
To convert such a string to a number use Val:
MyNumber = Val(MyText)
I guess the problem is not solveable with the decimal separator Application.DecimalSeparator = ".", even if it was supported by the Access library. It is a rather complicated issue, for the non-US users, as we are used to have , as a decimal separator.
In general, VBA considers only . as a decimal separator. Without taking care of the application default separator, the location of the user and their settings. Thus, some interesting cases could happen:
Sub TestMe()
Dim myText As String
myText = "123,42"
Debug.Print Replace(Format(myText, "#0.00"), ",", ".")
End Sub
A possible solution, that I have implemented some time ago was to use Replace() and to replace as in the gif above. It could be a bit slow indeed, but taking into account the usage of VBA and Access, extreme speed is not something the app could achieve anyway.
In VB.net, I am sending SCPI commands via GPIB-to-USB to an agilent/keysight device. I am using this sample to write data to instruments. https://www.keysight.com/main/editorial.jspx?cc=BR&lc=por&ckey=492255&nid=-33170.1090751.08&id=492255
Here is a portion of my code that I am having trouble with.
Dim P As Integer
Dim Power As Integer
For P = -50 To 0 Step 10
System.Threading.Thread.Sleep(500)
Power = P
instrument.WriteString("POW" & Str$(P) & "dbm")
Console.WriteLine("Power" & P) 'Testing if this portion of the for loop is reached
My code builds successfully, but the device I am communicating with displays an error message saying "invalid separator." I believe the problem is that P starts as a negative number. If I were to do something like, For P=0 to 10 step 1, it would work on the device receiving the command. However, Str$ does not seem to be work with negative numbers. Using Console.WriteLine, I do see that my loop is working correctly in the console output. Based on the console output, the structure of the loop is not the problem.
How could I make this work with P starting as a negative number? I tested the code and device to see if it can take negative numbers by simply using instrument.WriteString("POW -5 dbm") and that works. I really believe the problem involves Str$. I also tried doing ("POW" & P.ToString() & "dbm") but that did not work either. I also replaced Str$ with CStr but that did not work either. Also note that the sleep/delay is not an issue because I implemented that in another for loop and it worked with no errors.
I just figured it out. There has to be a space after POW. It should look like "POW "
I have reduced my problem to the following code example. I am using a German Excel version in which separators in normal Excel formulas are semicolons ";" instead of "," (e.g. =SUMME(A1;A3) instead of =SUM(A1,A3)).
Now the code which works different from time to time:
Sub CommasDoNotWorkAnymore()
Dim a()
Dim i%
a = Array("A1,A3,A5", "B1", "B2")
i = 0
Debug.Print Sheets(1).Range(a(i)).Address
End Sub
Normally, when starting Excel, this code works. But sometimes Excel seem to switch the accepted separators used in the Range() to semicolons untill I restart Excel. This occurs most times when rerunning the code after a runtime error.
Is this a general Excel bug? Does anybody know what is behind this behaviour? Is there some Excel-wide "local option" for the Range class?
EDIT: I just tried to convert the a(i) with CStr(a(i) but this does also not work. So no ByRef kind of problem...
If you want to control it, check first what separator is currently in use. What I guess is that you want to know the list separator:
Application.International(xlListSeparator)
Check other separators here:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-international-property-excel
The other time I had a problem with identifying decimal separator in VBA. Finnally I was able to get it in this way:
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function
Changing separator not always works. Please see this: Changing decimal separator in VBA (not only in Excel)
The best solution is to check/change locale, even temporary.
Application.LanguageSettings.LanguageID(msoLanguageIDUI)
gives the LCID which would be 1033 for English (US)
Initially it is important to note that I use MS Excel in Brazilian Portuguese.
That's my code:
Dim C1 As Integer
Dim C2 As Integer
Dim Cod As String
C1 = Worksheets("Dictionary").Range("D1").Value
C2 = Worksheets("Dictionary").Range("D2").Value
Cod = C1 & "." & C2
Worksheets("Dictionary").Range("D3").Value = Cod
Cells:
D1 = 1
D2 = 2
I expected that the output would be "1.2", but it's "1,2"!
What am I doing wrong?
And I apologize for my rusty English
Thank you!
Try this before outputting the value to the range:
Worksheets("Dictionary").Range("D3").NumberFormat = "#"
This changes the format of the cell to Text which prevents Excel to interpret the value you write to it as a number.
You use your Excel in "Brazilian Portuguese". The change may be due to the International Settings in the Windows Control Panel.
If your International Settings define the "," as the decimal separator, then Excel will convert the dot to the comma when you make the assignment in Visual Basic. This behavior makes the VBA code independent of locale settings and so makes the code portable in the international context (I assume that is the intention of this behavior).
If Brazilian Portuguese needs the dot as separator, then set your computer to use the dot as decimal separator, however, it shouldn't matter because when you send the spreadsheet to Brazil, the Excel there will use the correct decimal separator (I assume numbers are stored as binary floating point so dot or comma is only a matter of presentation).
EDIT: strikethrough of text above that would seem a workaround, as setting the international seting/decimal separator makes no difference for the end result.
Sub Replace()
Sheet1.Range("B1").Value = "=SUBSTITUTE(A1,""."","","")"
End Sub
You can change your range accordingly.
Example - Range("B1:B3").Value = "=SUBSTITUTE(A1:A3,""."","","")"
I have a text file that has multiple blank lines and Im trying to return all the lines between two of them specifically
so if I have a file that looks like this:
____________________________
1########################
2##########################
3
4########################
5##########################
6#######################
7
8#########################
9##########################
10#######################
11####################
12########################
13#########################
14
15##########################
----------------------------
I would like to grab lines 8-13. Unfortunately, it might not always be 8-13 as it could be 9-20 or 7-8, but it will however always be between the 2nd and 3rd line break.
I know how to trim characters and pull out singular lines, but I have no idea how to trim entire sections.
Any help would be appreciated, even if you just point me to a tutorial.
Thanks in advance.
The basic idea here is to get the entire thing as a string, split it into groups at the double line breaks, and then reference the group you want (in your case, the third one).
Dim value As String = File.ReadAllText("C:\test.txt")
Dim breakString As String = Environment.NewLine & Environment.NewLine
Dim groups As String() = value.Split({breakString}, StringSplitOptions.None)
Dim desiredString As String = groups(2)
MsgBox(desiredString)
Edit:
In response to the question in your comment -
Environment.NewLine is a more dynamic way of specifying a line break. Assuming you're running on windows - you could use VbCrLf as well. The idea is that if you were to compile the same code on Linux, it Environment.NewLine would generate a Lf instead. You can see here for more information: http://en.wikipedia.org/wiki/Newline
The reason I used Environment.NewLine & Environment.NewLine is because you want to break your information where there are two line breaks (one at the end of the last line of a paragraph, and one for the blank line before the next paragraph)
What I ended up doing was trimming the last part and searching for what I needed in the first part (I know I didnt include the searching part in the question, but I was just trying to figure out a way to narrow down the search results as it would have had repeated results). Im posting this incase anyone else stumbles upon this looking for some answers.
Dim applist() = System.IO.File.ReadAllLines("C:\applist.txt")
Dim findICSName As String = "pid"
Dim ICSName As New Regex("\:.*?\(")
Dim x = 0
Do Until applist(x).Contains("Total PSS by OOM adjustment:")
If applist(x).Contains(findICSName) Then
app = ICSName.Match(applist(x)).Value
app = app.TrimStart(CChar(": "))
app = app.TrimEnd(CChar("("))
ListBox1.Items.Add(app)
End If
x = x + 1
Loop
End If
How this works is that it looks through each line for the regex until it reaches first word in the breakpoint "Total PSS by OOM adjustment:"