I'm writing a MatLab programm under windows 7 x64 that takes the clipboard, with mixed data copied from Microsoft Excel, and import it directly to MatLab into a single char array.
When I use str = clipboard('paste') in MatLab, all text data copied from excel are fine, but the numerical data copied loss some decimal places.
For example, if the cell A1 contains the number 2113.12389881239, but it only displays the value 2113.123899 due to cell formating, then 2113.123899 is copied from excel to MatLab instead of the real cell value 2113.12389881239.
I would like to know how to copy the exact cell value stored in Excel not the value displayed.
Thanks in advance.
Given the following value: .1234567, formatted with all from 1-7 decimal places, I get the following results when I copy-paste to another program (notepad):
0.1
0.12
0.123
0.1235
0.12346
0.123457
0.1234567
Assuming you want to copy one number at a time, I used this macro:
Sub FormattedCopy()
Dim oldFormat, copyString As String
oldFormat = Selection.NumberFormatLocal
Selection.NumberFormatLocal = "#"
Selection.Copy
MsgBox ("Please paste your data")
Selection.NumberFormatLocal = oldFormat
End Sub
The message box serves to 'pause' the process, because changing the number format back will clear the clipboard. When I paste any value, it properly displays in notepad as 0.1234567
You can do this with a range of values if you save the range of number formats in an array and feed it back as I did above with for loops.
Related
Suppose I have a column with many (hundreds) of rows, each with a HYPERLINK formula, e.g.
=HYPERLINK("https://npiregistry.cms.hhs.gov/registry/provider-view/99999", "99999")
How can I convert all into a cells that contain the text - but with an underlying link (as in with CTRL-K), e.g., 99999
Thanks!
After further investigation, the problem is not the security setting of the Excel for Mac but the hyperlink formula was composed with 2 cell references; the display text, and the link.
The VBA function to insert / replace a hyperlinked to a cell is as of the following.
ActiveSheet.Hyperlinks.Add Anchor:=current_range, Address:=address_string, TextToDisplay:=display_string
In your situation, you have to replace current_range, address_string, and display_string in the VBA syntax.
In my above example, since we are using a For loop to loop through all the cells in selection, you can leave it as current_range.
For the address_string, and display_string, you will feed the function with the cell location of those two on your spreadhseet. Since your spreadsheet has the address string stored in two separate columns, you want to reference to the column by either using the familiar cell reference format, Range("$C" & current_range.row) format, or use the offset method, which involve in counting the index of the column. Example, current_range.offset(0, -10).value. The -10 in the offset is how many column you want to move left from your current_range.
Note, it is important to add the .value at the back of your cell reference so it's getting the data (String) stored in the cell instead of the potential formula in the cell.
Create this macro.
Sub convert_hyperlink_formula_to_hyperlink_cell()
Dim address_string As String, display_string As String
Dim current_range As Range
For Each current_range In Selection
address_string = Mid(current_range.Formula, 13, InStr(1, current_range.Formula, ",") - 14)
display_string = current_range.value
ActiveSheet.Hyperlinks.Add Anchor:=current_range, Address:=address_string, TextToDisplay:=display_string
Next current_range
End Sub
Select the range of your Hyperlink formulas then run the macro. That should convert the hyperlink formulas to actual hyperlinked cells.
Please try this sample file.
Convert Hyperlink Sample File.xlsm
I have made a copy of your posted formula, and pasted a few lines in the sample file. The macro is already setup in the Excel file for your to test.
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 a Visual Studio program that reads a PDF file and scrapes data from it. The VS program then generates a tab-delimited string that is manually pasted into the spreadsheet.
Everything works fine, but my tab-delimited line erases a formula in one column. Not a big deal as I just copy the formula from the previous line.
Would it be possible to put the formula into my tab-delimited line?
Here is the formula:
=IF(AND(NOT(ISBLANK($M2666)),ISBLANK($O2666)),"y","")
If I put this into the tab-delimited line in the appropriate column, it works fine, if I happen to be inserting the tab-delimited line on row 2666.
I tried using the row() function, but then it's not a valid formula:
=IF(AND(NOT(ISBLANK($Mrow())),ISBLANK($Orow())),"y","")
I tried a function that returns the last row in a given column then made a variable to put into the formula. If I am just pasting in Excel, it works, but when I try to insert it in a tab-delimited line is pastes as text.
Remember, the tab-delimited string is being generated in a program external to the spreadsheet and the program doesn't have access to the spreadsheet to find the last used row.
So, here's the question, how do I paste a formula from the clipboard?
This question is tagged as vba so here is my VBA-based solution.
dim strFormula as string
strFormula = "=IF(AND(NOT(ISBLANK(RC13)),ISBLANK(RC15)),""y"","""")"
range("M2").formular1c1 = strFormula
range("M2666").formular1c1 = strFormula
range("M9999").formular1c1 = strFormula
The Range.FormulaR1C1 property accepts the xlR1C1 style formula that will adjust for any row you place it into.
If you place the xlR1C1 style formula into a tab delimited TXT file, the following code would be necessary before and after using VBA to import the TXT file.
dim origRefStyle as long
origRefStyle = Application.ReferenceStyle
Application.ReferenceStyle = xlR1C1
'import the tab delimited TXT here
Application.ReferenceStyle = origRefStyle
You can use INDIRECT and ADDRESS... and also ROW and COLUMN:
Please search every detail on that function if you need to know.
Say you want get value of M2666 from Q2666 with the formula INDIRECT(ADDRESS(ROW();COLUMN()-4)). Back to the example problem, with assumption the formula is inserted in Q2666, then your formula should be:
=IF(AND(NOT(ISBLANK(INDIRECT(ADDRESS(ROW();COLUMN()-4))));ISBLANK(INDIRECT(ADDRESS(ROW();COLUMN()-2))));"y";"")
Please notice that Excel uses ; not , to separate parameters in a function.
Ok, here is the case:
I have an Excel file in .xlsx format. One of the cells contains the value 132,6 and it's format is number. This means that Excel shows the number as 132,60. Now because it represents a price (132 euro and 60 cents) I'd like to keep it in this format 132,60.
Now I need to convert all cells to text format for purposes that aren't important in this question. Because the value is actually 132,6, after the conversion this is the text value shown, instead of the 132,60. But I'd like to maintain the trailing zero when converting it to text.
Is there any VBA implementation for this?
this format should work
=TEXT(A2;"#'##0.00")
EDIT: In VBA
Below snippet will convert numbers in selection to text.
Sub aMacro()
Dim SelRange As Range
Set SelRange = Selection
For Each c In SelRange.Cells
c.Value = Format(c.Value, "###0.00")
Next
End Sub
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).