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.
Related
It looks like google sheets is making the same mistake as Excel, by "thinking ahead" and converting the value "1.1.1" to 2001.01.01 when doing sheet.appendRow. I have tried to set the number format of the column in charge to "#" (which should be plain text) before inserting rows - but looks ineffective. On the other hand doing the same after inserts is also ineffective, as the content is already "date".
Adding ' before is working, but it is not what I need.
Is there any way to give a default format or to disable such automatic conversion (from google script)?
I have a cell in which a form deposits a variable number of values seperated by a comma such as: " 1, 2, 4, 6 " etc - when there are only three answers, Google "helps" me by converting the value into a date object. But it's supposed to be a list of choices...
It's not pretty, but I've managed a workaround by using .getDisplayValue instead of .getValue - it does change the cell value into a string, so if you need to do further manipulations that are dependent on the value being a number or something, obviously, this fails.
I overwrite the value for the problem cell in my array before passing it to .appendRow
//getting the values
var values = s.getRange(row,1,1,lastCol).getValues()[0];
//brute force crushing of problem value
values[5] = s.getRange(row,6).getDisplayValue();
Sometimes I like to display numbers along with text in the same cell. To do this, I custom format the cell to something like 0.00" test", e.g. a cell A1 with formula =PI(), but formatted with custom format 0.00" test" would return a displayed result of 3.14 test.
Crucially, the value of the cell is unchanged by the formatting - you can still do =A1 * 3 in another cell and get the result - since the value of A1 is still Pi, only it's display has changed.
For a UDF that returns a numerical value (in my case, Long, but it could be any number), is there a way of returning a cell such that it is displayed 1 way, but it's actual value (.Value2 in VBA I believe) is a number, not text.
I've tried Format( in VBA, but it returns a text string. I would just format my cell how I want it manually, but as you can see from the below code, the formatting is dependent on intermediate results (I'm trying to return the value formatted with the time the calculation took).
UDF_RESULT = Format(valueResult.Length, IIf(tElapsed < timeout, "0" & " """ & Round(tElapsed, 2) & "s""", "0"))
This would be easy to do with a Macro, but within a UDF it's harder. I could declare all of the intermediate values at a module level, then a Worksheet_Calculate() macro can access those values and apply the custom formatting, but a UDF-wrapped approach would be much better.
No.
You're confusing a cell's value with its number format. A UDF can compute a value, and the cell is free to format that value as needed.
So if a UDF returns a number, the cell's value is the result of that function - a number.
Just format the cell as needed. A function doesn't format anything.
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
I am looking for a VBA script that will help me find certain keywords in a cell and if found highlight the entire row. Below are my requirements:
I have a database of words eg hell, get out, shut up, don't you dare etc. I need a macro to search the data in column "E" of excel and in case any of the cell in column "E" contains any word listed in the database (irrespective of the case of the word upper or lower)the entire row is highlighted. The word can be in the beginning, middle or end of the cell and the macro should be able to find that word and highlight the column.
Seeking help from all VBA masters for this.
You can do this with conditional formatting, instead of VBA.
Conditional formatting works by applying a 'second formula' to a given cell. If the 'second formula' results in TRUE, then special formatting conditions can be applied.
EXAMPLE CONDITIONAL FORMATTING
For example, if you have a single column of Data, A:A, and you want to check if that column has the exact string "hello world", you could add a conditional format [Home ribbon, Styles section, Conditional Formatting] that turns a cell yellow with this formula:
=$A1="hello world"
This will only result in TRUE if the cell in column A at that row equals exactly "hello world" [note that Column A has an absolute-reference $, and row 1 does not, so row 1 is relative to the position of the cell in the condiitonal format rule].
To check to see if any row in column A includes hellow world, we need to add a SEARCH function, which checks to see if a small search string is inside of a larger string:
=SEARCH("hello world",$A1)>0
Because SEARCH by default returns the first character in a larger string that matches the search term (and if it finds nothing, it returns #N/A), we check to see if our search for "hello world" in column A returns a number.
SEARCHING MULTIPLE COLUMNS
Now, to see if ANY column, say from A-D, includes "hello world", we concatenate each value of each column so that it gives us a single string, which we can search through for "hello world", like so:
=SEARCH("hello world",$A1&$B1&$C1&$D1)>0
This will first create a single string, equal to A1 & B1 & C1 & D1 all in a row. Then it will search that newly created string to see if "hello world" is inside it, and return a number value if it is.
ARRAY FORMULA BASICS
Finally, we need to do the tricky part - searching for multiple terms instead of just "hello world". This is called an Array Formula. An array formula works by performing a single operation on multiple cells, and then returning multiple results in an Array. In an Excel sheet, an array formula must be confirmed with CTRL + SHIFT + ENTER (instead of just ENTER), but in conditional formatting, you actually don't need to do anything special - it will recognize an array formula without a special command.
As an example of conditional formatting, see this example, which checks whether any value from A1:A5 = 10, and if it does, it gives us the value in B1:B5:
=IF(A1:A5=10,B1:B5,"")
Remember in Excel on a worksheet, this would be confirmed by pressing CTRL + SHIFT + ENTER. If you do test this, it will give you the following result, assuming A2 = 10 and A5 = 10:
={"";B2;"";"";B5}
This result would actually be hidden, because Excel can't "collapse" an array function on its own. So assume column B had values, and we actually want to sum them together. We would then wrap the Array formula in a SUM function:
=SUM(IF(A1:A5=10,B1:B5,""))
As you can see if you test this, we have actually created our own SUMIF function, using Array formulas instead of the built-in SUMIF.
SEARCHING FOR MULTIPLE TERMS WITH ARRAY FORMULAS
So now we apply these principles to the conditional formatting, to create an array formula which will check our concatenated 'NEW STRING' for any number of provided terms, as follow [Assumes the search terms are typed into cells E1:E10]:
=SUM(SEARCH($E$1:$E$10,$A1&$B1&$C1&$D1)>0)
This formula can be placed as a conditional formatting rule which reaches all of A:D. Set the rule to highlight / change format in whatever way you like.
I am storing some values in my excel using vba and them comparing with other results. The results can be whatever: name, surname, address, time...
The problem that I have is that when I store a string that has a date format it takes it as time automatically. For example if I enter 8:15 it automatically convert it to 08:15 and then when I am comparing it with other value that is the same (8:15) it returns me false because it is not 08:15.
So basically the question is... how can I introduce a value like 8:15 without being converted to 08:15?
Two methods:
Put an apostraphe at the front of the value to tell Excel it's a string. Example: The value '8:15 will be interpreted as a string of value "8:15".
Use a formula to store the value as a string. Example: ="8:15" is also interpreted as a string of value "8:15".
I'm assuming this is when you're entering the values in Cells. the format of the cells is probably general, thus it formats things like numbers or dates different. Change it to text.
select all (or specific columns) right click - format cells - number tab - text option. it says there "text format cells are treated as text even when a number is in the cell. the cell is displayed exactly as entered"
edit
another option, in VBA use .FormulaR1C1 instead of .value when comparing. .value is the actual value of the cell (like a formula result) where as .formulaR1C1 will give you the actual entered text. so .formulaR1C1 might get you the actual entered 8:15 instead of the corrected value: 08:15.