VBA formatting number - excel

I building a macro in excel that reads values from my workbook and fill a online form on a web-page, in one of cells I have a number, 126,25 (using comma to separate the decimal part) but when a submit this number to my online form the field display the value, 126.25
How can I format the number to use the comma format? I already try read the value as text but the result is the same
'Read value from workbook
Number = Range("C6").Value
'Trying to format
Number = Format(Number, "###,##0")

Perhaps:
Sub dural()
Number = Range("C6").Value
Number = Replace(Number, ".", ",")
MsgBox Number
End Sub

I am dealing with those issues constantly as I am in European format like you.
I stopped wondering and simply do this systematically :
dim strNumber as string
'Read value from workbook
Number = Range("C6").Value
'Trying to format
strNumber = Format(Number, "###,##0")
strNumber = replace(strNumber,".",",")
Move the number properly formatted in a string
Replace any . with , in that string
That's maybe not the most elegant solution, but that's for me at least the easiest way to remind how to trick it.

Related

Replace number format in Word

I am using a excel vba code to paste some variable data to word document (using replace all function). But I cannot paste it in correct format.
I have a word document template and get some parametric value from excel and replace it with a spesific text in word document.
Set myRange = docWord.Content
myRange.Find.Execute FindText:="TEXT_TEMPLATE", ReplaceWith:=amountvariable, Replace:=wdReplaceAll, MatchCase:=True
For example; in excel file:
amountvariable = Range("A1")
My Word Document has a sentence: "The amount is TEXT_TEMPLATE."
The code takes the value A1 cell and replace the phrase "TEXT_TEMPLATE" with "amountvariable".
However the format of the number is like 10000. I want to see it as 10.000,00. How can I make it with the right number format.
Just format the string variable according your version of "right" ;-)
Option Explicit
Sub format10k()
Dim dbl10k As Double
Dim str10k As String
dbl10k = 10000#
str10k = Format(dbl10k, "#,##0.00")
Debug.Print str10k
End Sub

VBA Issue with Number formatted as text when numbers include comma

I use a vba script to open another workbook. This workbook is in format .XLS and the content from a DB was stored like:
"Name1" "0" "7,98"
"Name2" "5" "1"
"Name3" "2" "7,1"
When opening the workbook with a VBA script, every cell that includes a comma, is interpreted as text and shows this warning:
The number in this cell is formatted as text or is preceded by an apostrophe
Strangely, if I open the file by double clicking, the numbers are just formatted as text and don't show any error. I guess, Excel is doing some interpretting, which doesn't work.
My code is:
Dim WorkBookImport As Workbook
Name = Application.GetOpenFilename()
If Name <> False Then
Set WorkBookImport = Workbooks.Open(Filename:=Name)
End If
I tried everything from:
Range(mycolumn).Value = Range(mycolumn).Value
For loop with CDbl(.Value)
Range(mycolumn).TextToColumns ...
Nothing works ;( Please help!
Option Explicit
Sub edksg()
Dim c As Range
Set c = Sheet1.Range("D3")
c.Value2 = CDbl(c.Value2)
End Sub
Works fine for me when cell D3 of the worksheet is formatted as text.
Of course, my location uses the comma as decimal separator, so if your location uses a different standard, the issue might be there. In that case, doing something like CDbl(Replace(c.Value2, ",", Application.DecimalSeparator, 1, -1, vbBinaryCompare)) might solve that part of the issue.
If you want to loop through the entirety of some column, the end result might look something like this, assuming the values you want to convert to numbers are in column C.
Option Explicit
Sub edksg()
Dim c As Range
Dim r As Range
Set r = Sheet1.Range("C1:C" & Sheet1.Range("C" & Sheet1.Rows.Count).End(xlUp).Row)
For Each c In r
If Len(c) > 0 Then
c.Value2 = CDbl(Replace(c.Value2, ",", Application.DecimalSeparator, 1, -1, vbBinaryCompare))
End If
Next c
End Sub
It´s a problem with the formating of the cells. You should use NumberFormat to change it.
Sub FormatNumber()
ThisWorkbook.Worksheets("YourWorkbook").Range("YourRange") = CDbl(ThisWorkbook.Worksheets("YourWorkbook").Range("YourRange"))
ThisWorkbook.Worksheets("YourWorkbook").Range("YourRange").NumberFormat = "General"
End Sub
With NumberFormat you can also change it to text if you have the opposite problem, in this case you would use NumberFormat = "#"

Paste a value in Excel exactly as is visible

I have a simple request, to paste the data exactly as visible in Excel.
I have a list of dates in mm/yyyy format, but Excel keeps adding mm/dd/yyyy which is throwing off my analysis. It's formatted to show simply mm/yyyy but the actual cell value keeps getting set to mm/01/yyyy.
How can I simply copy/paste the value to be mm/yyyy.
I've tried Range("A1").Value = Range("A1").Value, but of course that just keeps the same info.
Yes, in my case since it's dates, I can do a kludgy function that takes the left three characters, and combines with the rightmost four. However, that really just gets the date number returned. I tried on G4 and get 4171730. Plus, I'd like to know how to do this with other types of cell values too (strings, numbers, etc.).
save the value and the format then set the cell as text and assign the formatted value:
Sub test()
Dim t As Variant
t = Range("A1").Value2
Dim x As String
x = Range("A1").NumberFormat
Range("A1").NumberFormat = "#"
Range("A1").Value = Format(t, x)
End Sub
This also works
Sub test()
Dim t As String
t = Range("A1").Text
Range("A1").NumberFormat = "#"
Range("A1").Value = t
End Sub
Range("A1").Value = Format(yourdate, "mm/yyyy")

Excel VBA inserting date into cell produces incorrect format

Hoping someone can help with a funny issue I'm having. Using the following Excel VBA code to update a cell with a date in a sheet. The cell is formatted to "DD/MM"YYYY" my local PC is set to this also.
Dim SelectedDate As String
SelectedDate = "05/02/2018"
Sheets("CONTROL").Range("F36").NumberFormat = "dd/mm/yyyy"
Sheets("CONTROL").Range("F36").Value = Format(SelectedDate,"dd/mm/yyyy")
MsgBox Format(SelectedDate,"dd/mm/yyyy") ' Returns 05/02/2018 - Correct
MsgBox Sheets("CONTROL").Range("F36").Value ' Returns 02/05/2018 - Incorrect
Any help would be greatly apprecaited.
Easy to get confused with Excel, VBA and date formats. Even easier when you are using strings instead of dates in VBA and/or on the worksheet. And it will be easier to figure out what is going on if you format your output to unambiguous dates eg: dd-mmm-yyyy.
You are entering a string into F36. VBA is US Centric and thinks the string represents May 2, 2018
To have the string interpreted the same as your windows short date format, you can use the Datevalue function and treat your entries as dates and not as strings.
So one alternative to your code would be:
Sub marine()
Dim SelectedDate As Date
SelectedDate = DateValue("05/02/2018") 'will convert to 5-Feb-2018
Sheets("CONTROL").Range("F36").NumberFormat = "dd/mm/yyyy"
Sheets("CONTROL").Range("F36").Value = SelectedDate
MsgBox Format(SelectedDate, "dd/mm/yyyy")
MsgBox format(Sheets("CONTROL").Range("F36").Value,"dd/mm/yyyy")
End Sub
The NumberFormat properties just define the way the underlying value is visually represented, but it doesn't change the underlying value itself. Instead of printing the Value property, use Text (which holds the formatted representation of the underlying value):
MsgBox Sheets("CONTROL").Range("F36").Text
Alternatively, if you want to stick on the Value property of the cell, you are forced to print it as follows:
MsgBox Format(Sheets("CONTROL").Range("F36").Value, "dd/mm/yyyy")

Excel VBA cell character limit

I am building a macro to export data from a custom outlook form to an excel workbook. The data will be extracted in the string format from user-defined fields in the outlook form. The string data will then be entered into the values of the excel cells. The strings may contain a huge number of characters.
I understand that each excel cell can hold 32,767 characters. What happens if I try to enter a string with more than 32,767 characters in an excel cell? What will happen to the excess characters?
I realize that the characters do not appear, but can these lost characters be recovered somehow?
They are simply lost and there is no way to recover them. You can test this out if you like:
Sub test()
Dim i As Long
Dim text As String
For i = 1 To 32767
text = text & "a"
Next
text = text & "end"
Range("A1").Value = text
'"end" will be lost
End Sub

Resources