I have created a spreadsheet with VBA functions in a PT-BR (comma as decimal) Excel. Everything works fine.
But a client (running it on EN-US) had a weird issue: decimal numbers, such as 3.88888 were copied as hole numbers, like 388888. Numbers with less decimal cases, like 2.5, get copied as 2,5, and not recognized as numbers.
My function is just copying the value, like this:
ResultsSheet.Cells(row, 3).Value=DataSheet.Cells(row, 11).value
Unfortunately, I am unable to reproduce this issue. On either language setting, I get correct results - which is what I would expect.
Anyone having experienced this, or pointing me to some information would be greatly appreciated. I would also help with clues on how to reproduce my client's issue.
As you said, quite tricky to recreate the problem, but type conversion with some checks should work, snippet below.
If InStr(DataSheet.Cells(Row, 11).Value, ",") > 0 Then
'check if text contains a comma, and if so replace with "."
ResultsSheet.Cells(Row, 3).Value = CDec(Replace(DataSheet.Cells(Row, 11).Text, ",", "."))
Else
If IsNumeric(Cells(r, 11).Value) Then
'check if numeric and ensure decimal value on ResultsSheet
ResultsSheet.Cells(Row, 3).Value = CDec(DataSheet.Cells(Row, 11).Text)
Else
'if text then just copy
ResultsSheet.Cells(Row, 3).Value = DataSheet.Cells(Row, 11).Value
End If
End If
Hope this solves your issue. Boa Sorte ^^.
Miguel
Related
I am having an issue with VLookup in my VBA code. When I didn't have the range as a dynamic size using CurrentRegion it worked flawlessly, now for whatever reason, it only works for a single loop over and fails at the first VLookup. I have tried with and without the with block. I have a variable that will replace the 5 in the for loop and works but have kept it as its original so that isn't affecting it.
I apologize if this is a duplicate but I could not find an answer. Any help would be appreciated.
Set jobTypeData = Worksheets("JobType").Range("A1").CurrentRegion
For i = 1 To 5
jobTypeTemp = Worksheets("Employees2").Cells(i + 1, 16).Value
jobRoleTemp = Worksheets("Employees2").Cells(i + 1, 17).Value
'Creates variables relevant to the job type
With Worksheets("JobType")
minHours = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 2))
maxHours = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 3))
minShift = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 5))
maxShift = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 6))
shiftGap = CDec(WorksheetFunction.VLookup(jobTypeTemp, jobTypeData, 7))
End With
There is more code under this but it all works a-okay and worked fine before using the dynamic data size.
The code that works does not change the contents or size of jobTypeData.
I have also checked the values VLookup returns and they are all correct. The only thing I can think of is that it can't find the second jobType however I have checked and they are identical (no hidden spaces).
The answer appears to be related to the way VLookup uses 'approximate match'.
I always thought VLookup would still provide the exact match if it was an option and because I had strict data validation I never thought using approximate match would be a problem. However, turns out, it's not just mildly unpredictable, but actually unreliable.
After checking it was assuming "Role Name" was a closer match to "Team Manager" than "Team Manager". Upon changing it to using 'exact match', everything worked fine. No changes to the code which worked before either.
Well, lesson learned...
When you have an exact match ALWAYS use (no matter how confident you are, use me as an example):
VLOOKUP(arg1, arg2, arg3, FALSE)
Not:
VLOOKUP(arg1, arg2, arg3)
I'm working with Visual basic in Excel and I have a cell with the value "Gotovinski račun".
Another cell should be an English version of that value.
My code is:
If Range("A2").Value = "Gotovinski račun" Then Range("D2").Value = "Cash account"
Problem is that VBA can't recognize the small Lattin letter "č".
Any ideas?
Problem is that the VBA Editor only supports ANSI characters. You could try setting your "language for programs that do not support unicode" setting to Croation (or whichever language) (in administrative language settings).
Note that with that setting, the test string is properly displayed in the VBE:
If that is not an option, then you need to insert any characters with a code > 255 using the Chrw$ function. See the comment by #AlexK for an example.
A simple workaround consists in using a Like comparison with one (ore more) ? wildcards replacing the c plus caron character č:
With Sheet1.Range("A2")
If .Value Like "Gotovinski ra?un" Then
.Offset(0, 3).Value = "Cash account"
Else
.Offset(0, 3).Value = "'" & .Value & "' not found"
End If
End With
hei everyone,
am i missing something here?
i'm trying to compare two cells which are both formatted the same.
the way i compare it is such (never mind the "i" variable, the code is inside a for loop):
If SomeSheet.Cells(i, col).Value <> SomeOtherSheet.Cells(i, 4).Value Then
result = False
End If
I have tried converting the values to doubles, but my macro still sees them as different even thought the values are identical.
When I debug the code and try to see the actual values in the immediat window, they are ideed the same:
? SomeSheet.Cells(i, col).Value
310289286,463803
? SomeOtherSheet.Cells(i, 4).Value
310289286,463803
does anyone have any idea?
thanks!
EDIT: As suggested bu FaneDuru in the comments, I tried to round the values and even though i get the same output in the immediate window, the comparrison is correct now. Don't know why. Thanks!
If result has been defined as a boolean variable, the default value will be false. So unless there is another part of the code where result is set to true, It wil always return false.
Maybe try something like this:
If SomeSheet.Cells(i, col).Value = SomeOtherSheet.Cells(i, 4).Value Then
result = True
End If
I know the Headline sounds odd so I will start off with a screenshot:
As you can see, the problem is that the point suddenly changes to a comma when I look up an ID in the UserForm.
Before recalling Infos, I am saving all Information rather straightforward:
with ws
Range("BH" & lastRow).value = Me.payinfoOnTime
Range("BI" & lastRow).value = Me.payinfo30
Range("BJ" & lastRow).value = Me.payinfo60
Range("BK" & lastRow).value = Me.payinfo90
Range("BL" & lastRow).value = Me.payinfo90more
End with
Recalling the respective info for a searched ID is done by:
Set FoundRange = ws.Range("D4:D500").Find(What:=Me.SearchSuppNo, LookIn:=xlValues)
With ws
Me.SEpayinfoontime = FoundRange.Offset(0, 56)
Me.SEpayinfo30 = FoundRange.Offset(0, 57)
Me.SEpayinfo60 = FoundRange.Offset(0, 58)
Me.SEpayinfo90 = FoundRange.Offset(0, 59)
Me.SEpayinfo90more = FoundRange.Offset(0, 60)
end with
The Problem is that later calculations for scores are depending on those textboxes and I constantly get an error, unless I always manually change the commas back to points.
Any ideas how I can fix this?
The line:
Me.SEpayinfoontime = FoundRange.Offset(0, 56)
is in fact:
Me.SEpayinfoontime.Value = FoundRange.Offset(0, 56).Value
When you populate an MSForms.TextBox using the .Value property (typed As Variant), like you implicitly do, and providing a number on the right side, the compiler passes the value to the TextBox as a number, and then the value is automatically converted to string inside the TextBox.
Exactly how that conversion happens does not appear to be documented, and from experiment, it would appear there is a problem with it.
When you freshly start Excel, it would appear assigning .Value will convert the number using the en-us locale, even if your system locale is different. But as soon as you go to the Control Panel and change your current locale to something else, .Value begins to respect the system locale, and changes its result depending on what is currently selected.
It should not be happening and I would see it as an Excel bug.
But if you instead assign the .Text property, the number is converted to string using the current system decimal dot, and that conversion happens outside of the TextBox, because the compiler knows .Text is a string, so it converts the right-hand side number to string beforehand.
So in your situation I would:
Make sure I always use the .Text property explicitly:
Me.SEpayinfoontime.Text = ...
Make sure I explicitly use the correct kind of functions to convert between text and numbers:
Me.SEpayinfoontime.Text = CStr(FoundRange.Offset(0, 56).Value)
MsgBox CInt(Me.SEpayinfoontime.Text) / 10
although this step is optional and represents my personal preference. Given that it's a string on the left side of the assignment, VB will use CStr automatically.
Go to Excel's settings to make sure the "Use system separators" tick is set.
Check what locale is selected in the Control Panel - Language and Regional settings.
If it is not En-Us, I would select En-Us to make sure the decimal separator is a dot there.
Restart Excel.
This is really bugging me as it seems pretty illogical the way it's working.
I have a macro to format a cell as a currency using a bit of code to obtain the currency symbol.
Here is the code involved:
Dim sym As String
sym = reportConstants(ISOcode)
'Just use the ISO code if there isn't a symbol available
If sym = "" Then
sym = ISOcode
End If
With range(.Offset(0, 3), .Offset(3, 3))
.NumberFormat = sym & "#,##0;(" & sym & "#,##0)"
Debug.Print sym & "#,##0;(" & sym & "#,##0)"
End With
reportConstants is a dictionary object with currency symbols defined as strings. E.g. reportConstants("USD") = "$". This is defined earlier in the macro.
When the macro runs it gets the ISO code and should then format the cell with the corresponding currency symbol.
When I run it in one instance the ISO code is "USD" - so sym is defined as "$" - but it still formats the cell with a pound sign (£). When I debug.print the format cell string it shows $#,##0;($#,##0) so, as long as I got my syntax correct, it should use a dollar sign in the cell. But it uses a £ sign instead. (I am running a UK version of excel so it may be defaulting to £-sign, but why?)
Any help greatly appreciated.
I just recorded a macro to set the format to $xx.xx and it created this: [$$-409]#,##0.00. Looks like the -409 localises the currency to a particular country; it works without it - try changing yours to .NumberFormat = "[$" & sym & "]#,##0.00"
Btw guess I read your question somewhat after posting ;) Excel is well influenced by the regional settings of your computer for currency, language, dates... Using numberformat can force it to keep the sign you require. if it is a matter of rounding up you can try to: On Excel 2010, go to File - Options - Advanced and scroll down to "When calculating this workbook" and click on the "set precision as displayed" and OK out.
Try this: given your values are numerics/ integers/decimals....
Range("a2").Style = "Currency"
Or you can use format:
Format(value, "Currency")
Format(Range(a2).value, "Currency")
References:
http://www.mrexcel.com/forum/excel-questions/439331-displaying-currency-based-regional-settings.html
http://www.addictivetips.com/microsoft-office/excel-2010-currency-values/
(PS: I am on mobile, you may try these two links)