Excel VBA cell character limit - excel

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

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

Insert string into cell with only integers while keeping fonts- VBA

I am trying to append text into an excel cell with VBA. Currently I am using :
Range("A1").Characters(Len(Range("A1")) + 1).Insert ("text")
This works fine for cells with text, but if the cell contains only numbers (integers) it throws an error:
Run-time error '1004' Insert method of Characters class failed
Parts of the text in the cell are in different fonts or bolded and I need to keep them that way.
I have also tried to change the NumberFormat the cell to general, but it does not help:
Range("A1").NumberFormat = "#"
If someone knows a way to append text to a cell containing only integers with different fonts please answer.
Unlike Strings, Cells that contain actual Numbers (but not strings that look like numbers) cannot have format applied individual characters.
Therefore split your processing based on data type1
Dim cell as Range
Dim AppendText as String
AppendText = "text"
Set cell = Range("A1")
If TypeName(cell.Value2) <> "String" Then
cell.Value2 = cell.Value2 & AppendText
Else
cell.Characters(Len(cell.Value2) + 1).Insert AppendText
End If
After this has run, cells thar were numeric will now be strings, and can have individual characters formmated.
Notes:
You might want to consider other data types too: boolean, date, ...

Filepath opening multiple times in VBA

I want to bring data from another excel file, the code below works as intended, XLookup does the job BUT Windows file picker opens three times with this code. Can anyone advise why?
I've got one variable and two references to it in the formula.
Dim FileToOpen As Variant
FileToOpen = Application.GetOpenFilename()
Range("B1").Select
ActiveCell.Formula2R1C1 = _
"=XLOOKUP(RC[-1]:R[79]C[-1],'& FileToOpen &'!R11C1:R57C1,'& FileToOpen &'!R11C2:R57C2,""Not Found"",0)"
You need to distinguish between Excel and VBA. In VBA, you have a variable named FileToOpen that contains a filename. In Excel, this variable is unknown.
What you want to do is to write the content of the variable into a string. That string will then be written as a formula into a cell. Therefore you need to do some string concatenation in VBA. With you current code, VBA sees only one single string. It cannot look into that string, see that a part of a string is a variable name and replace the name with the content. Everything between the double quotes in left untouched by VBA - except if you have a pair of double quotes, this tells VBA that you want a (single) double quote character within the string.
If you want to write a formula with VBA, my advice is always to write the formula into a intermediate string variable that you can easily check with the debugger.
Change your code to
Dim formula As String
formula = "=XLOOKUP(RC[-1]:R[79]C[-1],'" & FileToOpen & "'!R11C1:R57C1,'" & FileToOpen & "'!R11C2:R57C2,""Not Found"",0)"
Debug.Print formula
ActiveCell.Formula2R1C1 = formula
See the difference? Now your formula is build from (5) pieces that VBA sees, and it will conatenate the pieces of the formula and the content of your variable.

VBA formatting number

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.

Tab delimited data in excel cell

I want to build a tab delimited string and insert it in cell "A1". For some reason, it does not identify the tab delimiter. Here is my code:
Worksheets("Sheet1").Range("A1").Value = Join(Application.WorksheetFunction.Transpose(Worksheets("Sheet1").Range("B1:B11")), Chr(9))
I even tried the "vbTab" instead of "Chr(9)" but I still see no tabs in the string. Its inserting a string without any deliminator.
Thanks in advance
The tabs are there but are not being displayed. Try
Dim stg As String
Dim pos As Long
Worksheets("Sheet1").Range("A1").Value = Join(Application.WorksheetFunction. _
transpose(Worksheets("Sheet1").Range("B1:B11")), Chr(9))
stg = Worksheets("Sheet1").Range("A1").Value
For pos = 1 To Len(stg)
Debug.Print Asc(Mid(stg, pos, 1)) & " ";
Next
Debug.Print
The Immediate Window will shows the ASCII values of your strings spearated by nines.
Excel cannot visually display tabs within a cell.
As far as Excel is concerned, tab means to move to the next cell. If you copy a string that has a tab in it and paste the result into Excel, it will put the text before the tab in your selected cell and the text after the tab in the next cell.
If you just need the tab to be there then you can rest assured that it is. If you want it to display visually then modify your question to explain what you are trying to do.

Resources