I'm trying to write in a cell this string "=>".
Macro gives "error 1004".
Macro works correctly if I write "=>x" where x stands for another character.
What am I doing wrong?
Thanks
You can format it as text before entering the value:
With ActiveSheet.Range("A1")
.NumberFormat = "#"
.Value = "=>"
End With
The issue here is that Excel understands => as the beginning of a formula because it starts with an equal sign, and you get the error because the formula is incomplete.
If you want to force Excel to understand it as text add a single quote as first character:
Range("A1").Value = "'=>"
Excel will not show the quote ' but it will recognize the cell content as text instead of a formula.
Related
I need to put data validation on a range of cells so that you can enter no more and no less than 9 characters in those cells. The problem is that SOMETIMES those 9 characters will be all number ... and that "number string" will start with a zero ... e.g. 012345678. Excel will remove the zero, as it recognizes that string as a number and my validation kicks in saying that I need to enter 9 characters into that field.
Any ideas?
Format the range of cells as Text. This will prevent Excel from trimming leading zeroes.
=TEXT(Cellwithnumber,"000000000")
Normally, if you format a range as text prior to typing in data (by right-clicking → format cells) or with something like
Dim c As Object
For Each c In Selection.Cells
c.NumberFormat = "#"
Next c
or
ActiveSheet.Cells.NumberFormat = "#"
Excel wont cut the leading zeroes.
If other users are using your sheet, you could protect the cell formats, so they don't accidentally change it back to numbers by, say, pasting data in with formats.
You could create a new cell and enter them as text formulas. Say the value you want to get the 0 from is in A1, enter your formula in a cell:
=TEXT(A1,"000000000")
That will show the leading 0.
If you want to use VBA:
rngTarget.NumberFormat = "#" ' rngTarget is a Range, can be e.g. ActiveCell or ActiveSheet.Cells(1, 2) or ActiveSheet.Range("B8")
Here is my problem
Worksheets("Worksheet1").Cells(1, 1).Value = "28.10"
Worksheets("Worksheet2").Cells(1, 1).Value = Worksheets("Worksheet1").Cells(1, 1).Value
And in Worksheet2 i got "28,1".
First question: How i can force VBA to copy zero at the and of string to other cell?
Second question: How i can force VBA to not change dot to comma? This is Polish version office so i guess Excel sees that string as a number so changes dot to comma, becouse comma is a default decimal mark in Poland.
Write this:
Worksheets("Worksheet1").Cells(1, 1).NumberFormat = "#"
Worksheets("Worksheet2").Cells(1, 1).NumberFormat = "#"
this will make exel think that in this cell is text (not number) format
This problem seems very simple, yet I just can not find the solution (I am already loosing my mind about it :) )
OK, so I just want to put a certain value into an excel cell, using vba code, just as simple as this:
Cells(1,1).Value2 = "123,456"
The problem: this is a string (intentionally), but excel always convert it to number, and put that number into the cell, instead of the string that I wanted.
How can I force excel not to convert it, and just put into the cell exactly what I want (the string)??
Thanks,
Cells(1,1).Value2 = "'123,456"
note the single apostrophe before the number - this will signal to excel that whatever follows has to be interpreted as text.
Indeed, just as commented by Tim Williams, the way to make it work is pre-formatting as text. Thus, to do it all via VBA, just do that:
Cells(1, 1).NumberFormat = "#"
Cells(1, 1).Value = "1234,56"
This is probably too late, but I had a similar problem with dates that I wanted entered into cells from a text variable. Inevitably, it converted my variable text value to a date. What I finally had to do was concatentate a ' to the string variable and then put it in the cell like this:
prvt_rng_WrkSht.Cells(prvt_rng_WrkSht.Rows.Count, cnst_int_Col_Start_Date).Formula = "'" & _
param_cls_shift.Start_Date (string property of my class)
I have been searching all over for a solution and I couldn't find one.
Let's say I type into cell A1: =if(A2>1,1,0)
Excel will interpret that and either return 1 or 0.
However, what I would like to do is literally convert that formula into the string: '=if(A2>1,1,0)'
If you press Ctrl+~ into Excel, you will see all the formulas. Basically what I want to do, in conclusion, is take all those formulas and make them strings.
(The purpose, if you care, is to compare these strings with another workbook to make sure I didn't delete anything)
You can make a user defined function for this.
Put this code into a Module (not a worksheet code sheet, class, or form)
Function GetFormula(rng As range, Optional asR1C1 As Boolean = False) As String
If asR1C1 Then
getFormula = rng.FormulaR1C1
Else
getFormula = rng.Formula
End If
End Function
and call it in your worksheet like so
=GetFormula(A1)
or if you want the results as R1C1
=GetFormula(A1,TRUE)
Use the FORMULATEXT function. It's built into Excel.
what I would like to do is literally convert that formula into the
string: '=if(A2>1,1,0)'
Simply use the Formula Property of the cell, here is a code example:
Range("A1").Select
Dim strFormula As String
strFormula = ActiveCell.Formula
MsgBox (strFormula)
You can also replace the equal sign with blank using find and replace. In effect removing the equal sign and leaving only the text of the formula as a string.
Just follow this way.
Select the range (contain formula) you want to convert to text,
also you can select All range on your sheet.
Use Find and Replace command by pressing Control-F
then replace "=" with "^"
press replace All..
All done....
If you want to use it again as formula...
Just use Find and Replace, then replace "^" with "="
just look....All formula in text format converted to formula...
Just right-click on the column header (i.e. A) and select "Format Cells" then under the "Number" tab select "Text". Then at each cell press enter and the formula will be displayed instead of the result.
I'm writing a tool that syncs a simple database with Excel sheets. Each item in a table in the database corresponds to one row in the worksheet. I read the Excel sheet into the tool using C# and the Excel interop com interface, then compared the items' values (i.e. one of the columns in the excel sheet) after the sync just to make sure that they are equal.
Yesterday I found a case where the comparison wasn't true:
"'<MedalTitle>' Medal - <MedalDescription>"
"<MedalTitle>' Medal - <MedalDescription>"
The second is the one I've read in from Excel, and as you can see it's skipped the first apostrophe. Is there a way to tell Excel to treat the cell as just text (no, just setting the cell's formatting doesn't help)?
I even tried to copy the value ( 'hello' ) of a cell in VBA like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Target.Offset(1, 0).Value = Target.Worksheet.Range("b2").Value
Target.Offset(2, 0).Value = Target.Worksheet.Range("b2").Formula
Target.Offset(3, 0).Formula = Target.Worksheet.Range("b2").Formula
Target.Offset(4, 0).Formula = Target.Worksheet.Range("b2").Value
End Sub
The result was that the value of target cell is always hello'
If there is no way, I'll have to do something ugly like
if (dbitem.value[0] == ''' )
{
// stuff
}
else
{
// regular comparison
}
I'm afraid the apostrophe ' is a special character for Excel when it appears as the first character in a cell as you've found. It tells Excel to treat the rest of the string as text, so that you can enter something like '34.2 in the cell, and it'll treat it as the string instead of the number (for formatting and so on).
I suggest doing something similar to what you've suggested, except that where you're putting it into Excel, check the first character, and add an extra ' if there's one there already.
Alternatively, you could prepend an apostrophe to all values - if you want them all as text that is. That way you don't need the extra first character check.
Look at the PrefixCharacter property of the Range object which corresponds to that cell
From the help:
If the TransitionNavigKeys property is
False, this prefix character will be '
for a text label, or blank. If the
TransitionNavigKeys property is True,
this character will be ' for a
left-justified label, " for a
right-justified label, ^ for a
centered label, \ for a repeated
label, or blank.
The TransitionNavigKeys part relates to Lotus 1-2-3 compatibility so it's more than likely going to be False
Answer based on article at:
http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostrophes.html
(warning: slightly annoying pop-up may appear)
edit: actually this probably isn't going to be any use because PrefixCharacter is read-only :(
edit2: I was right the first time. PrefixCharacter only gets populated if the value added to the cell started with ' so just read back PrefixCharacter plus Value and concatenate. As long as TransitionNavigKeys is False, that is
try targetcell.Value instead. .Formula is the formula seen in the formula bar while .Value is the evaluated value of the cell.
So, I am guessing that you would have used .Formula in your original code as well. Changing that should work.
EDIT: Ok, it did not work (embarrassed).
Excel treats the starting single quote specially.. so specially that even obscure cell / range properties do not have access. The only workaround I could find is essentially the same as what you thought initially. Here goes:
If VarType(cell) = 8 And Not cell.HasFormula Then
GetFormulaI = "'" & cell.Formula
Else
GetFormulaI = cell.Formula
End If
You might try pre-pending a single quote to your text fields ( '''' + dbField ) in your query so that for fields with embedded single quotes your query would return:
"''stuff in single quotes'"
which when placed in an Excel cell would convert to:
"'stuff in single quotes'"
for characters that weren't in quotes you would get:
"'stuff that wasn't in quotes"
which when placed in an Excel cell would convert to:
"stuff that wasn't in quotes"
Worth a shot. :-)