Copy zero string Visual Basic Excel - string

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

Related

excel vba change format of date - numbers

Excel changes the following values automatically to a number, I guess because he considers them as a date:
2/2/1 becomes 36527
4/2/1 becomes 36926
I have a column with a combination of different formats now:
2/1/
3/1/
8/7/
36527
1/0/0
36926
Which VBA code can I use to convert the numbers back to their original format? The other values should stay the same.
I know the cDate function, but I guess it's not useful here?
I have already this in my VBA code for pasting values
ActiveWorkbook.Sheets("Import").Columns("A:AH").NumberFormat = "#"
ActiveWorkbook.Sheets("Import").Range("A1").Select
ActiveSheet.Paste
You can't change them back. Once Excel converts them, the original value is gone. Before you input the value, you can prepend an apostrophe to force it to text.
ActiveCell.Value = "'" & sMyValue
or as #Scott Craner commented, you can format the cell as text
ActiveCell.NumberFormat = "#"
ActiveCell.Value = sMyValue

VBA - write "=>" as string in a cell

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.

How to have excel recognize zero as a string?

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")

Find/Replace not finding cell value after formatting

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

VBA Append comma to end of integer, don't automatically change format

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

Resources