There is a column of values that are moved from one Excel spreadsheet to another, in the same workbook, by a macro. The values should be five characters, only numbers, including leading zeros.
There are several ways that have successfully replaced the lost leading zeros from the auto formatting that Excel does. With a strange result.
For every cell that the macro has formatted the cells, the Find/Replace tool refuses to recognize any searches that include zeros.
Example:
Before Macro = 9093
After Macro = 09093
The Find/Replace window will find a search value of 9093 but will not find a search value of 09093. A Find/Replace window will find a positive hit after deleting the macro formatted 09093 and hand keying 09093 into the cell.
I have not tried code checking each value for the desired number of characters then concatenating leading zeros until the right number of characters has been reached. My hesitation stems from my assumption that a macro running this code will run very slow when having to go through 1000 or so rows.
Code blocks below are two attempts:
''Masks for a five character sequence.
' Corrects for leading zeros being dropped for Product Code column.
' Currently does not work.
Columns("F:F").Select
Selection.NumberFormat = "00000"
''Alternative method for keeping correct format of Product Code
' (with leading zeros) and searchable with Find window.
' Also not functioning.
Dim a
Dim l As Long
With Range("F2", "F" & lastUsedRow)
.NumberFormat = "#"
a = .Value
For l = 1 To UBound(a, 1)
a(l, 1) = Right("0000" & a(l, 1), 6)
Next l
.Value = a
End With
The actual value of the cell you are trying to find is 9093, even though it is shown as 09093 through formatting. The find/replace tool will look for a string value of 09093 while the actual value is 9093 and thus cannot find it. Presumably when you key in the 09093 it is formatted as text rather than a number to preserve the leading 0s.
If you don't actually use the numbers in the newly created column for analysis, might I suggest the line below. This way you can find the cell with the leading 0's from the Find/Replace dialog as the entire product number including the leading 0's are a string.
Selection.NumberFormat = "#" 'This formats the selected cell as text
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")
I have column in excel with dates of birth of employees (in format DDMMYYYY) and for some reason I was asked to add apostrophe before each and every record (in a way that it is not visible). There is a lot of records.
When I try to do It automatically it always removes leading zero.
For example from 01011900 I get 1011900.
I tried find & replace, concatenate,
Nothing from these answers helped:
Adding Appostrophe in every field in particular column for excel
Each cell has to be formatted as a text, not date.
Any ideas?
Try:
="'"&TEXT(A1,"00000000")
This will force a format of "00000000" which will allow leading zeros. In fact, if you use =TEXT() then you don't need the apostrophe to force the text value.
VBA version:
Sub MacroMan()
Dim rng As Excel.Range
Set rng = Application.InputBox("Select range to amend:", , , , , , , 8)
If Not rng Is Nothing Then
For Each cell In rng.Cells
cell.Value = "'" & Format(cell.Value, "00000000")
Next
End If
End Sub
A way around is simply by changing the last digit of the year (assuming the data is all of the same year, for example 2018 replace with 201, using find and replace menu. Or if the case is multiple years, we could replace the first 2 digits of the year with 3 digits which the last digit is a particular digit, say 0. Thus the year /1988 would become /19088 after replacement.
Second phase is by adding aposthrope to the top cell, and simply paste its format to other cells.
Last phase is replace the wrong year to its original data.
Hope could help.
Right click the column and Format the cell. Choose custom and type ddmmyyyy or mmyydddd. That should do the trick.
I've pasted some numbers on Excel spreadsheet and wanted to do some calculations with it. The problem is that Excel isn't recognizing the numbers. I've already tried several methods to convert them into numbers and none of them works: paste/special multiplying by 1; formating each cell to the number/scientific number format. And there isn't also an error message on the top right corner of each cell like I've read on the internet indicating that there is a number written as text. If I retype each number, Excel recognizes it.
To make sure that the problem was really that the numbers were understood by Excel as text, I tried the functions ISNUMBER(), that returned FALSE and ISTEXT() that returned true.
I want to know how I can fix that problem without having to type into each cell.
Ps. the numbers are in scientific number format, i.e., 1,085859E+001
Since the column is text the cells are formatted as text.
you use Value to convert the text into a number so the formula will work
A2 = 123
A3 = 123 Richard
Formula
=isnumber(A2) result is false
use
=isnumber(value(A2)) result is True
I was having the same problem, until I realized that the decimal separator was set as (,) instead of (.) in the default settings. Once I changed that, everything worked fine.
If your "numbers" are being detected as text, you can use VALUE() to make sure Excel understands that it is actually a number.
A1: `1.23E+10 (this is a string)
B1: =VALUE(A1)
=12300000000
C1: 1.23E+10 (this is a number)
D1: =IF(B1==C1,"It worked", "Uh Oh")
=It Worked (for me anyway)
I'm not sure what the comma in your scientific number will do so might want to have the function replace them if there not required.
See Kenneth Hobs' answer here: http://www.vbaexpress.com/forum/showthread.php?42119-Solved-Convert-exponential-format-to-a-number
Open your Excel File, Press Alt + f11 to open the VBA screen,
Go to Insert > Module, Copy and Paste Kenneth's code:
Sub Expo()
Dim cell As Range, s() As String, lng As Long, n As Integer
For Each cell In Selection
With cell
If Not VarType(.Value2) = vbString Then GoTo NextCell
s() = Split(cell.Value2, "E")
.Value2 = s(0) * 1 * (1 * 10 ^ s(1)) 'ePart(s(1))
.NumberFormat = "General"
.EntireColumn.AutoFit
End With
NextCell:
Next cell
End Sub
You can now run it as a macro to convert selected cells. Or if you want it as a function copy this code instead:
Function Expo(cell As Range)
Dim s() As String
With cell
If VarType(.Value2) = vbString Then
s() = Split(.Value2, "E")
Expo = s(0) * 1 * (1 * 10 ^ s(1)) 'ePart(s(1))
End If
End With
End Function
This way you can use it as a normal function in excel eg =Expo(A1)
As I mentioned in the comments above though, you will have already lost some degree of accuracy when the original number was converted to scientific notation. The best solution is to get the originating program to write the proper numbers into the text file if you can.
Open a new word document and try Pasting the web content in word first, the copy this content from the word document and paste special in excel, as text. This simple solution worked for me
Open a new blank Excel, then go to Data > From Text, this way you can import text and designate which format you want to convert to. On the Text Import Wizard page, select either Delimited or Fixed width (I am not sure how your original text file look like but generally it should be Delimited. On the next page, pick a Delimiter or enter one in Others. On step 3, you should see the data listed below and the data format on the upper left. Pick General for those columns that you believe should not be Text. This should fix your problem.
My case was stubborn, no response to Paste Special or CLEAN(). Finally resolved by copying the offending column of Excel data and pasting into new Notepad++ doc. This revealed a leading "?" in all the bad numbers (apparently some non-printing character). Used Search > Replace to find all "?" and replace with nothing. Edit > Select All, copy to a new Excel column, and voilà!
There may be hidden characters. Trailing/leading spaces may not visible and hence erroneously be neglected. If there is trailing/leading Space characters with numeric values, excel consider it as text.
Copy contents problematic cells to MS-Word [(Select problematic cells and copy them to MS-Word)] and check any hidden characters, Remove hidden characters with "find"/"replace" functionality.
I was having issues with numbers from PPT (e.g. ($3,000))pasted to excel. Tried multiple different ways to get the text to recognize including find replacing parens, commas, $ signs to blank and trying to format so excel could run formulas. The only option that worked was to paste to Word first then paste value to excel which worked without any additional formatting steps. Surprised I could not do it all within excel though. Maybe there's another way
Select all the cells to convert to a number.
|Data| Menu Tab > Data Tools > [Text to columns]
Delimited. [Next]
Deselect all "Delimiters". [Next]
"Column data format" > General
[Finish]
Verify by using =ISNUMBER(C16) in an spare cell, where C16 is a sample cell. Should now return TRUE.
This happened to me lately. I had forgotten that I had set formula recalculation to manual. The weird thing is that it was returing FALSE when initially created (which was correct) but given the test depended on the value of other cells that, when changed, did not trigger the change in the cell with the isnumber() formula.
Pressing F9 "fixed" my problem (and my ignorance).
My current project is to rewrite a program that was written in 98. Their solution then was to do some in Excel, do this next part in Word, and then back to Excel in the 3rd step. The program syncs 2 files, and their solution is just dandy for 20 files that need to sync - however, now there are around 1,000 files to sync - so I'm working on streamlining the process. The output from the files ranges between X.X and X.XXXXX - I formatted them with
Columns("A:A").Select
Selection.NumberFormat = "0.00000"
To make the file easier to read. But the 2nd part is to append a comma, and then a letter regarding the numbers relation: not necessary for the question at hand. When I try to append a comma, Excel attempts to "fix" it and outputs it incorrectly. For example:
0.42400
0.87200
1.31600
1.75200
Becomes:
.424,
.872,
1.316,
1.752,
When it should stay at X.XXXXX. Here is what I have tried:
Changing Excel's options so that decimal identifier is '.' and thousands is ':'.
`Range("A" & ictr) = Format(Range("A" & ictr) & ",")
Selection.NumberFormat = "0.00000,"
Other forums/Google/Bing
I'm really assuming this can be done in Excel, I just cannot find a solution other than re-opening Word, and then back to Excel
Any ideas?
Since you state that the
2nd part is to append a comma, and then a letter regarding the numbers
relation
you're going to need a formula, not just a formatting option. In your VBA code, select the cells next to the original values, and add this line of code:
Selection.FormulaR1C1 = "=TEXT(RC[-1],""#0.00000"") & "","""
This gets you the formatting and the comma (regardless of the length of the number to the left of the decimal point) -- but to add that last letter, you'll obviously have to modify the formula to meet your specific needs.
You could insert a column and use a formula. Something like this should work
Dim TotalRows As Long
TotalRows = ActiveSheet.UsedRange.Rows.Count
Columns("B:B").Insert
With Range(Cells(1, 2), Cells(TotalRows, 2))
.Formula = "=LEFT(""'""&A1&REPT(""0"",5),7)"
.Value = .Value
End With
Columns("A:A").Delete
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. :-)