Vba excel convert cell to Hebrew currency (ILS) - vba

I am trying to convert cells with numbers to Hebrew currency format, instead of getting the sign of currency I get question mark sign like in an example attached.
I've rerecorded a macro but it just doesn't seem to work
Range("I4", "I" & LastRow).NumberFormat = _
"_ [$?-40D] * #,##0.00_ ;_ [$?-40D] * -#,##0.00_ ;_ [$?-40D] * ""-""??_ ;_ #_ "
Thank in advance, I would appreciate a lot If someone could answer my question

The VBA editor doesn't recognize the Hebrew currency symbol so you can use an ASCII character instead. The one your looking for is ChrW(8362). I found the correct character code from this website.
Example:
ActiveCell.NumberFormat = _
"_ [$" & ChrW(8362) & "-10D] * #,##0.00_ ;_ [$" & ChrW(8362) & "-10D] * -#,##0.00_ ;_ [$" & ChrW(8362) & "-10D] * ""-""tt_ ;_ #_ "
Result:

Related

Inputbox is not accepting double number VBA excel

I have a declaration like number= InputBox("Number for:", "Number:"), number is declared as Dim number As Double but when I enter a double number, for example 5.4, into the Inputbox and transmit it into a cell, the cell shows me 54, it deletes the point.
How can I fix this?
THX
If you want to detect which settings your Excel uses for the Decimal seperator, try the code below:
MsgBox "Excel uses " & Chr(34) & Application.DecimalSeparator & Chr(34) & " as a decimal seperator"
if you want to change it to ., then use the line below:
Application.DecimalSeparator = "."
Unfortunately, VBA is horrible at handling differences in decimal seprators. In your case, you should probably use a comma (,), instead of a punctuation/dot (.).
Edit: Using the Application.DecimalSeparator method, it now works regardless of regional settings. Be aware though, it seems to cause some issues if you change the comma separator settings for Excel (it seems that VBA somewhat ignores this setting). If you do not change that however, the example should work in all other cas
Sub GetNumberFromInputBox()
Dim val As String
Dim num As Double
'Get input
val = InputBox("Number for:", "Number:")
Debug.Print Application.DecimalSeparator
If IsNumeric(val) Then
'Try to convert to double as usual
num = CDbl(val)
'If the dot is removed automatically, then
'you will se a difference in the length. In
'those cases, replace the dot with a comma,
'before converting to double
If Len(val) <> Len(num) Then
If Application.DecimalSeparator = "," Then
num = CDbl(Replace(val, ".", ","))
Else
num = CDbl(Replace(val, ",", "."))
End If
End If
'Pring the number
Debug.Print "You selected number: " & num
Else
'If its not a number at all, throw an error
Debug.Print "You typed " & val & ", which is not a number"
End If
End Sub

recordset.GetString in Access VBA Query returns an extra character after the result

I have a query that I execute through VBA in Access 2010. The result of the query should be AFR, but it returns AFR with an extra line below it. I have added the "'" character to make the extra line visible.
TempHold = rs.GetString
Debug.Print "'" & TempHold & "'"
Returns this:
'AFR
'
But should return this:
'AFR'
I have tried using the below code, but none of the If statements evaluate as True. The code should check for a " ", a vbNewLine, or vbCrLf character but none evaluate as true. Does anyone know of any additional characters that would result in a new line?
If Right(TempHold, 1) = " " Then
TempHold = Left(TempHold, Len(TempHold) - 1)
ElseIf Right(TempHold, 2) = vbNewLine Or Right(TempHold, 2) = vbCrLf Then
TempHold = Left(TempHold, Len(TempHold) - 2)
End If
Use:
Asc(Right(TempHold, 1))
to get the Ascii character code.
Once you've found the character code (which, as you wrote in your comment, was 13), you can use your code to remove it:
If Right(TempHold, 1) = Chr(13) Then
TempHold = Left(TempHold, Len(TempHold) - 1)
End If
In this case, you can also use vbCr, which is the same as Chr(13).
The best way to get rid of the carriage return in my opinion is to stop it being created in the first place. This method is a lot tidier than having to remove the last character.
In the .GetString method there is a parameter for RowDelimiter which by default is set to be a carriage return. However you can change this to be whatever you want including a zero length string as follows:
rs.GetString(, , , "")
If you run your debug again with this code:
rs.GetString(, , , "")
Debug.Print "'" & TempHold & "'"
You will get this result:
'AFR'
Remember if you want something different to be placed between rows then just change the zero length string to whatever you need.

Excel show degree symbol incorrectly

I have a worksheet which allows user to change the temperature unit, there is a dropdown box containing "° C" and "° F" and then I use VBA to do the rest.
The problem is, I have this code:
Dim UnitString As String
Set UnitRange = Worksheets("Units of Measure").Cells
UnitString = UnitRange(1, 1)
MsgBox UnitString
and it gives me "? C" or "? F"
The next problem, when I call:
UnitRange(1, 1) = "° C"
I got "ฐ C" in that cell. (ฐ is one of Thai's characters)
These problems broke my sheet's logic, can anyone help me?
Regards,
Sarun
You might want to use Unicode within your VBA routines. For example, here is a simple routine that formats cells to degrees Centigrade :
Sub centigrade()
Const g = "General"
dq = Chr(34)
cent = " " & ChrW(8451)
s = g & dq & cent & dq
Selection.NumberFormat = s
Selection.Font.Name = "Arial Unicode MS"
End Sub

What does "& _" mean in VB?

I'm copying some query statements from a legacy VB app to a C# app. I am not familiar with VB, although looking at it makes me want a VB (Victoria Bitter). I have come across queries constructed like this:
*SELECT dp_duckbill_accounts.platypus_no AS duckbill, t_accounts.name AS Name " & _
"FROM t_accounts INNER JOIN dp_duckbill_accounts ON t_accounts.account_no = dp_duckbill_accounts.account_no " & _
"ORDER BY dp_duckbill_accounts.platypus_no*
The "& _" give me pause. If it was just "&" I would think it corresponds to "+" in C# to concatenate strings. But what in the world is the point of the underscore? Note the ampersand and the underscore are separated by a space.
The underscore is the line continuation character. It allows the concatenation to include a different line. Like so:
x = "Hello " & "World"
x = "Hello " & _
"World"
'this won't compile (pre vb.net 2010, anyway)
x = "Hello " &
"World"
Line Continuation on MSDN
How to: Break and Combine Statements in Code (Visual Basic)
_ means continue the statement on the following line.
so ... & _ means continue concatenating the string on the following line.
text = "One line string"
text = "Two line " & _
"string"
That is just a line continuation character that lets you continue to the next line.
& - is used for string concatenation in same line.
example - sConcatenatedString = "First" & "Second"
& _ - is used For string concatenation in different lines.
example - sConcatenatedString = "First" &_
"Second"

VBA Runtime error 1004 on Cells(..).Formula

I can't make this simple command work:
Cells(l, 7).Formula = "=" & var1 & " * " & var2 & " * " & var3 & " / 252"
I can paste the inspected formula string value in Excel and it works as expected.
Cells(l,7) is a proper reference, as I can inspect its value.
Inspected formula on debug:
"=86710597,9409 * 0,02 * 0,35 / 252"
The problem is when a variable is represented as string it will have the system's decimal point. In your case it is a comma so you need to convert it to dot, for example using replace() function.