How to remove the ' for Cstr(combox1.value) - vba

I use this to change the combox value in string type and paste on cells.
cells(i,1)=Cstr(combox1.value)
On Excel column A ,I have set the datatype as String .
For example , I input 017``123 098 065
However , i find that the exact value put on cells(i,1) is '017 '098 '056 123 .
The code starting with 0 contains ' at the beginning .
* Any way to improve my code without appearing the ' on exact cells ,for those codes starting with 0 *

The ' is probably a formatting character, which is a hangover from Lotus 1-2-3 where we used a leading ' to signify left-aligned and a leading " to signify right-aligned. (I think I remember that correctly.)
If it is just the formatting character (if it is, it won't be included in the character count returned by LEN) then you can safely just ignore it.
If you want to get rid of it for aesthetic reasons, you will need to delete the current contents of the cell, format the cell as General format, then format the cell as Text format, then insert new values into the cell.
Note: You can't just format the cell as Text, delete the contents, then insert a new value. The ' will still appear in that situation. The step to change it to General format, if it isn't already in that format, is important! (Maybe setting it to some other non-Text format will work just as well as setting it to General, but I know that General will work.)
Luckily, a .Clear will delete the contents of the cell and set the format back to General, so programatically we can do two things at once.
I therefore believe the following code should do what you want:
Cells(i, 1).Clear
Cells(i, 1).NumberFormat = "#"
Cells(i, 1).Value = combox1.Value
The first line is there simply to get rid of the issue that currently exists. If you were working with a completely new workbook, it wouldn't be needed.

convert the cell's number format to Text before setting numeric values.
cells(i,1).NumberFormat = "#"
cells(i,1)=combox1.value

Related

vba customize cells format

I am needing to customize cells with simple thousands format, like 1000, without any separator or decimal.
However, I wish to remove text fonts other than a number when they are input.
For example, I want to input 120118, however in my paper from which I am copying that figures, it is formatted as a date, thereby 12/01/18. I am needing Excel to simply keep it as 120118 after typing, removing the slash (/). I have seen similar settings in access queries.
Have you tried simply pasting only the cell value with:
Selection.PasteSpecial Paste:=xlPasteValues
Or just clear the cell format and format it again with your desired format.
Try:
Selecting the range
Home > Number > Number Format (or Ctrl+1 I think) > Custom
Enter ddmmyy
Okay
Can be done programmatically e.g.
Thisworkbook.worksheets("Sheet1").range("A1:A50").numberformat = "ddmmyy"
The above would only be a visual/cosmetic change and the internal value of each cell would still be a date (technically a number) for calculation purposes.
However, if I've misunderstood and you instead want to go from the date 21 Jan 2018 to the number 210118, I think you would need to get the range's value(s), format as DDMMYY string, then clng() - or maybe (DD*10000) + (MM*100) + (YY) might work, then format as "000000" to preserve leading zeros.

Formula in worksheet and VBA works differently

Cell A1 contains the number 25, which is right-aligned, implying it's a number, not text.
D1 contains the formula:
="" & A1
The 25 in D1 is left-aligned, implying it's text. That is confirmed by the following formula, which returns 1:
=IF(D1="25",1,0)
The following VBA code puts 25 in F1 but the 25 is right-aligned and the IF test returns 0:
Range("F1") = "" & Range("A1")
Any explanation for the discrepancy?
E1 contains the following formula which gives a left-aligned 25 and the IF test returns 1:
TEXT(A1,"0")
However, the following code gives a right-aligned 25 and the IF test returns 0:
Range("F1") = Application.WorksheetFunction.Text(Range("A1"), "0")
Not that I have to use the TEXT function. I just wonder why it works differently than when in a worksheet.
Any rule that tells when or what worksheet functions won't work in VBA code, or more precisely, will give different results than when in worksheet?
When a data is written by vba into a cell, an internal type conversion function is called if required, that is if the data type is different from the cell's numberformat property.
You dont want that conversion function to be called.
To avoid this conversion function to be called, choose the proper Numberformat property for the cell before writing the data.
Range("b4").NumberFormat = "#"
Range("b4") = Application.WorksheetFunction.Text(Range("A1"), "0")
You simply get the wrong idea of what is a number in Excel.
in general ALL input is a string. Also writing "25" in a cell.
However: If possible, Excel will convert all inputs to a numerical value if possible. Also for dates and times.
To prevent this, you simply insert a ' in front of your "text" in the cell.
The confusing part for you is the different behavior for formulas.
A formula will always output a "result" AND the "data type".
So =1+1 will be numeric as the last action was math.
=Left(1+1,1) will be text as the last action was text-based.
For =A1 it will simply copy the type. If there is a formula, then this will be the same. But if there is a "direct input" it will always try to convert to numerical and only be text if it can't be converted or if it starts with a leading ' (A1 itself does this already).
As a result: If there is a plain 25 in the cell, it will always be "numerical" no matter "how" you input the 25.
For newer Excel there is only one exception: if the cell formatting is text prior to entering a number, it will be treated as text (no converting). This does not apply if you change the formatting later.
Simple test:
enter 25 in A1 (formatting general)
enter =ISNUMBER(A1) in A2 (will be TRUE)
set formatting for A1 to "text" (A2 will still be TRUE)
enter 25 in A1 (now A2 will become FALSE)
This may fail (Excel confuses itself sometimes here). Try it with a new sheet. ;)
Hopefully you understood the fault in your logic ;)
The cell alignment says nothing about the cell's contents. Forget about anything being "implied" by it. When you start on a virgin worksheet the format for all cells is "General" which means that Excel will decide the format of what you enter. If you enter a number the format will be "Number". If you enter what looks like a date to Excel the format will be "Date", and for most other things the format will be "Text".
So, if you enter " 25" in a cell formatted as "General" Excel will recognise this to be a number despite the leading spaces, read it is numeric, and format the cell to the right. This will happen regardless of whether you made the entry by hand or used VBA. You can then proceed to format the alignment as you wish.
However, if you enter the number 25 in a cell formatted as Text Excel will recognise the number as text and display it formatted to the left (unless you expressly formatted the horizontal alignment to the right).
The best way to deal with any problems you might encounter in this regard, set the NumberFormat and HorizontalAlignment properties for the cells that you want to write to. You can do that both manually or using VBA.
Worksheet function when used in the worksheet behaves / works the same way as when used in VBA. Consider below code:
Note: Range("B1") contains a numeric value 25
Dim r As Range, v As Variant
Dim wf As WorksheetFunction: Set wf = Application.WorksheetFunction
With Sheet1
Set r = .Range("B1")
v = r.Value2
v = wf.Text(r.Value2, "0")
End With
Now using the local window, let us check the data type of variant v.
SC1: All variables un-initialized
You can see, at the start that all variables have no value and the variant type v is empty.
SC2: Variables initialized and v assigned a value
After executing lines up to v = r.value2, all variable types were confirmed (e.g. Range/Range etc.) and variant v is now Variant/Double.
SC3: Re-assign a value on v but using worksheet function Text
Executing the last line which uses the worksheet function Text, variant v type becomes Variant/String. I think this confirms that the function Text works as expected converting the numeric 25 into a string type.
As for the behavior of passing VBA generated value to worksheet, it is covered by Docmarti's post above.

Why don't numeric cells have a .Characters() property?

With any text value, I can individually format each character and then copy that formatting to another cell by iterating over the Range.Characters() Collection.
However, if the cell is a number (even if the numberFormatting displays it as a string e.g. dates) then it does not expose a .Characters() property and, indeed, cannot be selectively formatted digit-by-digit.
Why does Excel display strings using Character objects but not numbers, even when the number is being displayed as a string?
If you want to go around this, you may do the following:
In cell A1 put '123456 with the " ' " sign in front.
Then write
range("A1").Characters(1,3).Font.Bold = true
It would take only the first three numbers, not taking into account the " ' " sign. Thus, the number is kind of displayed as a string, but you can still use it calculations e.g. A1 + 4 would give 123460.

Adding text to a cell in Excel using VBA

I've been working with SQL and Excel Macros, but I don't know how to add text to a cell.
I wish to add the text "01/01/13 00:00" to cell A1. I can't just write it in the cell because the macro clears the contents of the sheet first and adds the information afterwards.
How do I do that in VBA?
Range("$A$1").Value = "'01/01/13 00:00" will do it.
Note the single quote; this will defeat automatic conversion to a number type. But is that what you really want? An alternative would be to format the cell to take a date-time value. Then drop the single quote from the string.
You could do
[A1].Value = "'O1/01/13 00:00"
if you really mean to add it as text (note the apostrophe as the first character).
The [A1].Value is VBA shorthand for Range("A1").Value.
If you want to enter a date, you could instead do (edited order with thanks to #SiddharthRout):
[A1].NumberFormat = "mm/dd/yyyy hh:mm;#"
[A1].Value = DateValue("01/01/2013 00:00")
You need to use Range and Value functions.
Range would be the cell where you want the text you want
Value would be the text that you want in that Cell
Range("A1").Value="whatever text"
You can also use the cell property.
Cells(1, 1).Value = "Hey, what's up?"
Make sure to use a . before Cells(1,1).Value as in .Cells(1,1).Value, if you are using it within With function. If you are selecting some sheet.

How to add new line to a cell so that new data is appended to previous data on a new line

I have a loop in which a cell is getting overwritten.
The cell lets say contain text xxx at one particular counter. In the next counter it gets over written by yyy. I can only see yyy in the end as the cell gets overwritten. What I want to do is preserve the original xxx and have the yyy on a next line in the same cell. Just like one enters data manually by pushing Alt + Enter.
Can anyone help me identify the correct syntax for this code logic?
Use the following:
Range("A1").Value = CStr(Range("A1").Value) + Chr(10) + "yyy"
CStr makes sure, that a possible numeric value of (in this example) A1 is cast to string, otherwise the + operator might fail.
Chr(10) takes care of appending a line break. "yyy" of course is your new text. If this text happens to be a numeric value aswell, use CStr(yyy).