Excel VBA Insert Single quote into column with 16+ Numeric Numbers - excel

In Column M of a sheet called 'FF' , I have a series of values that can be 16+ numerical digits (numbers in the trillions). As a result, excel is losing precision and not properly displaying the values.
I have realized that if I manually put a single quote in the formula bar, it displays the number properly and saves the CSV properly as well. Is there a way to do this for all of column M if Column M already has numeric values populated?
Conceptually, I was hoping to do something like this where the single quote doesn't actually appear
Sub SingleQuote()
With ThisWorkbook.Worksheets("FF")
.Cells(2, 18).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row).Formula = "=IF(Stage!M2="""","""",='M2"
End With
End Sub

Related

vb.net 2010 Excel converting text fields to decimal

I am creating an excel object from a vb.net listview table simply by creating an array of F(x,y), creating a range and putting the values from the array in the range as follows.
shXL.Range(Startcell,AEndCell).Value = F
However some of the fields are numeric and I want them to be formatted to two decimal places and EXCEL to recognize them as decimals
What I end up with in the excel worksheet is many green triangles telling me they are text fields.
How do I convert a range withing the sheet say A5,I20 to be formated as decimals.
I tried: (x,y).numberformat = "00.00" which works to format to 2dp but still treats the cells as text.
Furthermore, is it possible to Excel Sum a range? How is the possible?
Your help is appreciated!
shXL.Range(Startcell,AEndCell).Value = F
'// Loop over same range and convert to decimal
For Each cell In shXl.Range(Startcell,AEndCell)
With cell
.Value = CDec(.Value)
End With
Next
Furthermore, is it possible to Excel Sum a range? How is the possible?
I don't think Excel would have got very far as a spreadsheet product if it couldn't sum a range!
Assuming you want to do this in vb.net you need to use the instance of the application. I'll assume in your code it's XL
mySumValue = XL.WorksheetFunction.Sum(shXL.Range(Startcell,AEndCell))

Formula to extract 10 or 11 digit phone numbers from random complex test string

I a newbee in field of excel formula and need your help in a complex formula, where i need to extract phone numbers from a string of random text. This does not have a fix format for the string
Example set off strings:
Dring to data add9724516002
add 08107936777 to me pler
8000069633 plz add. Me
9000088106 mujhe bhi add karo dosto
I have already tried many formulas but nothing seem to work. Only thing fixed is the length of number, it should be either 10 digits or 11 (including initial 0)
You could use a RegExp via VBA
(which seems to be coming to Excel as a formula option sometime down the track, see uservoice
code
Function GetCode(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\d{10,11}\b"
If .test(strIn) Then
GetCode = .Execute(strIn)(0)
Else
GetCode = "no match"
End If
End With
End Function
If they all look like the strings you have provided above, you could use Text to Columns. Let's say all of those strings were in A1:A4.
Select all four cells
Data - "Text to Columns"
Delimited
Use "space" to separate values
Finish
You will now have a large majority of your phone numbers pulled out, and it will look something like this:
(I've added a row above the data that makes every column its own set of data.. Column 1,2,3,4,5,and 6. I've also added another column in place of column A, Sort. This will be useful at a later stage)
Next, select A1:G5.
Click "Insert - Table"
"My table has headers"
OK
Your range is now a table, meaning you can sort the data via ascending order. I'm assuming you have hundreds of strings that you're sorting through. When you sort via ascending order, all numbers will show up first.
In the pic below I've sorted the first column of actual data, and there are two phone numbers at the top I can pull out:
If you ever want to revert back to your original lineup of data, click the Sort column to "Ascending"
I hope this is a good workaround to avoid VBA. You may not get all of the phone numbers, but probably a good chunk. You can also copy and paste columns C:G to the bottom of column A and sort everything at once if you only need all of the phone numbers.
If the strings that have numbers and letters attached are similar, you can also look into the RIGHT and LEFT formulas to pull out the numbers from the alphanumeric strings

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

Writing matlab matrix to excel

I am trying to export a matrix from Matlab to export with xlswrite. However, my matrix is a cellarray that has strings such as '001', '00323'. When it is exported into Excel, Excel automatically converts them back to numbers and drops the first 2 zeros into '1', and '323'.
Does anyone know how to force excel to accept them as Text as them are being written from Matlab to xlsx?
Thank you!
L.
Excel probably likes to do this because it is exactly what Excel would do if you typed those values in.
One way to fix this is to put '"=001"' in the cell array rather than '001' like the following code. Note that Excel treats the values properly in the resulting file:
myCell= {1, '0001', '="0001"'};
xlswrite('test.xlsx', myCell)
You could write a little function that surrounds all the strings in a cell array with quotes, if needed:
function aCell = fixForExcel(aCell)
for ind = 1:numel(aCell)
myVal = aCell{ind};
if isstr(myVal)
aCell{ind} = sprintf('="%s"', myVal)
end
end
end
I run into this issue with SSNs. I don't know how much control you have with creating the spreadsheet with xlswrite.
You can set the format to Text, and this preserves the leading zeroes.
The problem will remain that when you open the spreadsheet, all leading zeroes will be eliminated. You can create a custom format that specifies that the format for the cell has two leading zeroes. You can use "\0\0#" as your custom expression. The text format will be saved after the first time.
If you have a variable number of 0s, the only way to get around it is to copy the data into excel.

How can I have Excel display trailing zeros when using VBA?

I have an input field where the user inputs a number "X.XXXXX"
I then copy that number using some VBA to another sheet when a button is pressed. The problem occurs when the number ends in 0 or multiple zeros. For example, take the number 5.46770. I have the cell formatted to display 5 decimal places. However, if the trailing number is a 0, Excel still considers the value to be 5.4677 unbeknownst to the user. So when my macro pull the value from the cell it takes 5.4677 vs. 5.46770. What I'm trying to figure out is how to have my VBA code pull the trailing 0(s). Any ideas?
The value of 5.4677 is the same as 5.46770; they are equal.
If you are trying to get a string that is formatted just like the cell, try:
cell.Text
Depending on what you are doing with the NUMBER, you could set the cell value with a single quote in front. It will force it to be shown like text, leading/trailing zeros or not. IE:
CELL2.VALUE = "'" & FORMATNUMBER(CELL1.VALUE,5)

Resources