I run different scripts using VBA on my German computer with an English Excel 2013 version installed on it. My local decimal settings are set to be international, e.g. 123123 is displayed as 123,123.00.
However, when I program with VBA, the decimal changes.
For example:
sub decimal_problem()
dim sDecSep as string
dim sThSep as string
sDecSep = Application.International(xlDecimalSeparator) ' sDecSep = "."
sThSep = Application.International(xlThousandsSeparator) ' sThSep = ","
Fmt= "#" & sThSep & "##0" & sDecSep & "00"
'Fmt looks like "#,##0.00" which is what I want
Msgbox(Format(123123,Fmt)) 'Fmt="#,##0.00"
end sub
The number in the Msgbox is 123.123,00 which is the German number format and definitely not the format I specified before.
When I change the format in a Spreadsheet I get the correct separators (e.g. "#,##0.00" returns 123,123.00).
Do you have a solution for that issue?
As I need to calculate with the formatted number, I try to avoid to change the number to a string and use the replace function.
Thanks for the help!
The code you mentioned above is showing the output you wanted that is 123,123.00 and not 123.123,00.
Also as soon as you will assign this value to a range in a sheet this Text value will get converted to numeric automatically. for ex - thisworkbook.sheets(1).range("a1").value = Format(123123, Fmt)--This will assign cell a1 walue as 123,123.00 which will be in numeric
please let me know if I have not understood your scenario.
Related
I have an API that I want to update dynamically so that the user can enter a start date and an end date on a spreadsheet and my macro will pull back data for that particular date range.
The issue I'm having is that within the API URL the StartDate and EndDate parameters must be in the format yyyy-mm-dd as a string.
I've tried URL = "https:// ...&StartDate = Format(Date(),"yyyy-mm-dd") & EndDate=Format(Date(),"yyyy-mm-dd")&..." (the ... is for the things before and after the URL).
An example of the type of URL I'm looking at is:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces
I've also played around with adding in extra quotes within the URL string but I can't seem to get it to work.
I keep getting told that the dates aren't being recognised and therefore I can only get the code to run if I hardcode dates.
Any suggestions?
I noticed a few issues in the code posted. The & is the concatenation operator in VBA. You need to enclose that in "" to make sure you are returning the ampersand as a string, and not joining strings together.
I've added some sample code which hopefully illustrates the idea and get's you back up and running. The code should print out True if the createdURL and testURL are equal, or False if not.
Code
Option Explicit
Public Sub FormatExample()
'This is the example provided
Dim testURL As String
testURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&" & _
"startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces"
'This is a built string example
Dim createdURL As String
createdURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345" & _
"&startdate=" & Format(#10/1/2008#, "yyyy-mm-dd") & _
"&end-date=" & Format(#10/31/2008#, "yyyy-mm-dd") & _
"&metrics=ga:sessions,ga:bounces"
'Print out if they are equal
Debug.Print createdURL = testURL
End Sub
I've been searching for two days to solve my issue but so far nothing.
There are many (Very many) vba excel tools developed where I work and our regional settings in PC-s determine comma as decimal separator but reports and data downloaded from our systems have dot as decimal separator. In these tools, when needed we just have set UseSystemSeparators = False then DecimalSeparators = "." and at the end of the macro reverted back.
Now new people have been getting windows 10 pcs and some of the tools run into errors. I got a Win10 pcs from IT to test and found out that no matter how I set in Excel settings VBA Macro uses PC regional settings while on the sheet it still uses what is determined in Excel settings. Same file, same test in my win7 pc and if set then both on sheet and vba macro use local application settings.
Does anyone know what is the reason and how it could be fixed? I can figure many workarounds but all of these mean that the tools need to be re-coded and there are just so many of these that I still keep finding out new tools used and what were developed before I joined the company in March. Changing all ~300 PC-s regional setting is not an option because it needs to be comma as decimal separator normally.
Edit: just to make it bit more clear I'll add some code:
Sub test()
Application.UseSystemSeparators = False
Application.DecimalSeparator = "."
variable = "10.1"
MsgBox CDbl(variable)
End Sub
Under Windows 7 - no problem. The variable containing a string can be converted.
Under Windows 10 - Run-time error, Type mismatch. Both PCs have "," as system separator. The funny thing is that when I change "10.1" to "10,1" then that works on both PC-s. Settings for both PC-s are same. System uses "," as separator and Excel is set to use ".".
This answer is based on your system (default) DecimalSeparator being a comma (,) and ThousandsSeparator being a point (.) - using Windows 10
Explanation:
I've done a little testing and found that the following functions only change the DecimalSeparator within Excel, not within VBA:
Application.UseSystemSeparators = False
Application.DecimalSeparator = "."
Application.ThousandsSeparator = ","
This means that when you change the DecimalSeparator by using these lines of code, it will simply update the values in your workbooks to be displayed in the format you are specifying. For example if your system format is:
100.000.000,99
Then the lines of code above would convert the values to:
100,000,000.99
However, when you run the code above and try to use a string that uses the non-system format for the DecimalSeparator then VBA will not recognise it.
Example:
(Where Application.DecimalSeparator = "," when Application.UseSystemSeparators = True)
Sub TestSeparator()
Application.UseSystemSeparators = False
Application.DecimalSeparator = "."
Application.ThousandsSeparator = ","
Debug.Print CDbl("100.99")
End Sub
This code will return 10099, not the expected double of 100,99 (in your system format) because VBA does not see the DecimalSeparator of "." as a DecimalSeparator.
Solution:
When referencing doubles within VBA you will need to always use the computer's system separators for thousands and decimals. If the doubles are being imported as strings you will need to use Replace to convert the string into a format that uses system separators.
Solution Example:
(Where Application.DecimalSeparator = "," when Application.UseSystemSeparators = True)
If you have the string of "100,000,000.99" in cell A1 and use the following code:
CDbl(Range("A1").Value)
It will produce a "Type mismatch" error. However if you convert this into a string that uses your system separators then no error will be given. You will need to use two Replace functions though, one to remove the ThousandsSeparators and one to convert the DecimalSeparators to your system ones:
CDbl(Replace(Replace(Range("A1").Value, ",", ""), ".", ",")
Perhaps this:
Set ws = ThisWorkbook.Worksheets("Name of Worksheet")
ws.Cells.Replace What:="", Replacement:="", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Set ws = Nothing
I've been lurking here for a while but this is my first post so let me know if I need to change something. Anyways, here goes:
I'm trying to create a macro that will add hyperlinks to cells in a worksheet. The problem is that after running the macro, I notice that the folder location of my spreadsheet has been prepended to the address that I specified. Is there something I need to do in order to indicate that this is a webpage and not a local file? Excerpt from the macro is below.
Dim IGQ As Range
Dim IGQno As String
Dim IGQno1 As String
For Each IGQ In Range("A2:A10") 'Actual range is much larger
IGQno = IGQ.Value
IGQno1 = Left(IGQ, 1)
Sheets("Cameron DCDA").Hyperlinks.Add Anchor:=IGQ, _
Address:="""http://xxxx""&IGQno1&""xxx""&IGQno&""xxxxx""" 'It's a company website so they probably don't want me to share it
Next
The result is that a hyperlink is created for each cell but it links to file:///C:\Users\John.Doe\Documents\"http://xxxx"&IGQno1&"xxx"&IGQno&"xxxxx"
I've tried using fewer quotation marks in the address since it seems like overkill but I get the compile error "Expected: end of statement"
Do you guys have any suggestions?
Too many quotes
Address:="http://xxxx " & IGQno1 & "xxx" & IGQno & "xxxxx"
Also - be sure to leave a space before your & otherwise it will be interpreted as a variable type suffix:
What are possible suffixes after variable name in VBA?
Decimal numbers (in our regional settings, a comma is the decimal symbol) are changed to semicolons when used in formula.
Let's say I have a number (I actually just parse it from a field but I need it in a variable):
c = 10,5
If I do:
sheet.Cells(1,1).Formula = "=SUM(" & c & ",10)"
The whole formula becomes:
=SUM(10;5;10)
The comma is always changed to a semi-colon which ruins the double number.
I can use .FormulaLocal and then the semi-colon separator works in the formula but the comma still works too so that does not solve it.
Changing the regional settings does not work since I need to distribute it and won't be able to get everyone to change the regional settings.
Anything that could save me from this?
if c i as string containing a double value in local format, you can use:
sheet.Cells(1,1).Formula = "=SUM(" & Str(CDbl(c)) & ",10)"
CDblwill convert the value from the locale string to the Double format, and Str will make a String of it using . as decimal separator.
You can also replace the DecimalSeparator directly:
sheet.Cells(1,1).Formula = "=SUM(" & Replace(c, Application.DecimalSeparator, ".") & ",10)"
I am creating an Excel file that helps people to automatically process data. Within this sheet, there are numbers that have to be parsed (and converted to other units, etc. Simple calculations).
The numbers come in US format which means they look like:
0,000,000.00
In Germany, numbers are displayed like this:
0.000.000,00
In VBA, I actually don't know, which localized Version of Excel is in use (German or English).
Question:
Is there an easy way to parse the US format numbers into data type Double regardless of the Excel localization, that is used by the user?
Some notes. I did not change locale to test.
'http://msdn.microsoft.com/en-us/library/office/bb177675(v=office.12).aspx
Debug.Print Application.International(xlCountryCode) 'Excel locale
Debug.Print Application.International(xlCountrySetting) 'Windows locale
sDecimal = Application.International(xlDecimalSeparator)
sThousand = Application.International(xlThousandsSeparator)
sNumber = "1,000,000.00"
If sThousand <> "," Then
If sDecimal <> "." Then
sNumber = Replace(sNumber, ",", "")
sNumber = Replace(sNumber, ".", sDecimal)
End If
End If
Debug.Print sNumber
A possible solution may be:
cells(y, x) = Val(Replace(cells(y, x).Text, ",", ""))
Val always uses the . as decimal mark (CDbl for example uses cultural settings, tested in a German Excel).