Converting HYPERLINK() - to text with an underlying link - excel

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.

Related

Excel Formula - Check if cell has formula

What formula do you use to check if another cell has formula? For example, I have 2 columns, A has cells which contains either a formula or a value.
(Column A usually contains Formulas but other users try to change their values by directly typing and replacing the formula that was previously there)
In Column B I want to add a formula that will say "HasFormula" if the cell on Column A has formula and say "PlainValue" if it contains a value.
I'm thinking maybe using =ISNUMBER() but that may not be accurate.
I am using Excel 2010.
Excel actually has a builtin ISFORMULA() function.
Say A1 has a formula and you want to check that. In say B1, you can use:
=If(ISFORMULA(A1),"HasFormula","PlainValue")
Edit: Per your comment, you don't have ISFORMULA(). An alternative is to create a quick UDF, and use the custom function in the worksheet.
In a workbook module, put this code:
Function isFormula(ByVal target As Range) As Boolean
isFormula = target.hasFormula
End Function
Then you can call it like this: =isFormula(A1) and it will return TRUE if A1 has a formula.
If you can't use VBA, then you can use this formula:
=IF(ISERROR(FORMULATEXT(A1)),"PlainText","HasFormula")
The MrExcel website (link below) has this method which uses old code from Excel 4 (which is still present for backward compatibility)...
Define a NAME such as "CellToLeftHasFormula" and in the "refers to" box put
=GET.CELL(48,OFFSET(INDIRECT("RC",FALSE),0,-1))
Then in column B use the formula =CellToLeftHasFormula which will return TRUE if it has.
Be aware that this will mean your Excel will now contain a macro and so will need to be saved as such (xlsm). I use this in Excel 2010.
For full explanation (and other .CELL options, besides 48) see MrExcel link: https://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
You can use the Range.HasFormula property.
https://learn.microsoft.com/en-us/office/vba/api/excel.range.hasformula
EDIT:
Text and code from the above link:
"True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise. Read-only Variant. ..."
Worksheets("Sheet1").Activate
Set rr = Application.InputBox( _
prompt:="Select a range on this worksheet", _
Type:=8)
If rr.HasFormula = True Then
MsgBox "Every cell in the selection contains a formula"
End If
You can restrict the user by protecting the column A.
You can directly check if a cell contains a formula by using a shortcut Ctrl + `.
You can use vba and write a user defined function :
1. Press alt + F11
2. Insert module in workbook
3. Paste this code
Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
4. Now, use Isformula in the cell wherever you want.

MS Excel - Paste a formula from a tab-delimited line

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.

Check whether a cell contains a link in Excel

I am trying to check whether values in a column, say A, contain a link, and if true, in column B, I want to type a text, for example: link.
I have many records (10 000), so doing this by hand will take a lot of time. Thx.
Save your file as .xlsm to allow Macros
Alt + F11 to open Visual Basic
Insert -> Module
Paste this function, it returns the count of hyperlinks in a range:
Function IsHyperlink(r As Range) As Integer
IsHyperlink = r.Hyperlinks.Count
End Function
Alt+Q
Use your new function with the if conditions to display your text:
=IF(IsHyperlink(A1),"LINK","NO LINK")
Here:
Sub Links()
Dim lnk As Hyperlink, lnks As Hyperlinks
Set lnks = Range("A:A").Hyperlinks
For i = 1 To lnks.Count
Set lnk = lnks(i)
lnk.Range.Value = "Link"
Next
End Sub
You need to read more on VBA if you want to use the procedure above. Please also always share your research first and at least a code stub. This was simple hence the exception.
There is a formula that could work, but it's based upon the length of the characters in the cell. For example, a hyperlink typically contains more characters due to the pathname. If the number of characters in your cells is always less than 30, for example, then you could write the formula
=If(Len(Cell("Filename",A1))>30,"True","False")
The formula will return "True" if the cell is a hyperlink.
This formula assumes that the Excel file is stored in a subfolder where each folder name is referenced which adds to the length of the hyperlink.
An aproximate solution without VBA:
=AND(ISREF(A1),ISNUMBER(SEARCH("http",A1,1)))
It is based on asking for two conditions:
There is reference in the cell (it applies for both hyperlinks and internal references)
The cell text (the link) contains the string "http", to cover both http and https
I managed to find a formula, not on VBA.
I used =IF(HYPERLINK(A1>0), "WEB", "")
Thank you all for your time. :)

Reference combined cell above

I have a sheet with a lots of columns ordered in a hierarchical way with the cells merged:
I'd like to name those columns (in example: row 5) like this:MainGroupA-SubGroupA-SubSubGroupA.
Simply referencing the columns above in the classic way won't work as the field above isn't available anymore. (In the example: the fields B1 to F1) (i.e. I can't enter A1&A2&A3 / R[-4]C&R[-3]C&R[-2]C as this formula tries to read from the "hidden" cells).
Is there a way to do this without manual work or the need to un-merge the parent-cells? I might be able to do this with some external text editor or even VBA but would prefer an "Excel formula solution" as it would stay updated for new groups and columns.
To Clarify: I'd like all columns in Line 5 to have the text like in A5
If you want:
MainGroupA-SubGroupA-SubSubGroupA
in A5 then this should work:
=A1&"-"&A2&"-"&A3
Edit Then try:
=OFFSET(A1,0,1-MOD(COLUMN(),6))&"-"&OFFSET(A2,0,MOD(COLUMN(),2)-1)&"-"&A3
though this won't give the same text as in A5 across the complete row.
The answer from pnuts is great and helped me solve some test cases. It was however a little difficult to adapt and produced empty strings for the last column, so I also wrote a VBA-Function to do exactly what I need.
Open the VBA Editor (ALT + F11) and enter the following code in a new module:
Public Function checkLeftIfEmpty(start As range) As String
If start.Cells.Count > 1 Then
checkLeftIfEmpty = "Only a single cell allowed as parameter"
Exit Function
End If
Dim currentRange As range
Set currentRange = start
Do While currentRange.Column >= 1
If currentRange.Value <> "" Then
checkLeftIfEmpty = currentRange.Value
Exit Function
Else
Set currentRange = currentRange.Offset(0, -1)
End If
Loop
End Function
You can now use the function checkLeftIfEmpty to find the first cell left-side from your parameter which contains text: (This will be the text of the merged cell itself, if applied to a "hidden by merge" cell)
And also in combination to concatenate a string:

Excel: Formula to extract a string of text delimited by markers from cells

I'm messing with a spreadsheet containing postal addresses that have been inserted in the cells' comments
Each comment contain an address composed of a variable number of lines (damn UK addresses, they can have up to 7 lines!) in the following format:
Line1,
Line2,
Line3,
[...],
State
With my poor skills, I've managed to extract the comment with a VBA script, obtaining the following string on a single cell:
Line1,Line2,Line3,[...],State
At this point each string between commas must be extracted to its own cell.
I've managed to extract the 1st 3 lines with the following formulas:
For Line1:
=LEFT(A8;(SEARCH(",";A8))-1)
For Line2:
=MID(A8; SEARCH(",";A8)+1; SEARCH(","; A8; SEARCH(","; A8)+1)-SEARCH(",";A8)-1)
For Line3:
=MID(A8; SEARCH(",";A8;SEARCH(",";A8;SEARCH(",";A8;SEARCH(",";A8)))+1)+1;SEARCH(","; A8; SEARCH(","; A8;SEARCH(",";A8)+1)+1)-SEARCH(",";A8;SEARCH(",";A8)+1)-1)
From this point I start to get overflow errors from my brain... I probably need some days of sleep.
Can anybody help me to get to "line6", and finally suggest me how to pull out the "State line" which ends without comma?
I thought I could pull out the "State" line with =RIGHT(",";SEARCH(",";A8)-1) but I'm obviously doing something wrong because that pulls out a comma instead of a string.
I guess I could do everything with a VBA script, but I'm not that skilled yet :(
With comma separated data in A1, in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,",",REPT(" ",999)),COLUMNS($A:A)*999-998,999))
and copy across. For example:
Note:
Why not use TextToColumns ?
The row of formulas re-calculates automatically if A1 changes.
The row of formulas will work even if A1, itself, contains a formula.
If you are wanting to do this programmatically instead of using a built-in, check out the split function for chopping up your comma separated string. It will split up your input string into an array. Then you can do whatever you like with the array.
Dim Names() As String
Names() = Split(inputValue, ",")
For i = 0 To UBound(Names)
' do what you want with each piece
Next
Gary's Student's answer is great for using the built-in functions.
If you want a VBA solution:
Sub spitString()
Dim sourceRange As Range
Dim stringArr() As String
Dim i As Integer
Set sourceRange = ActiveSheet.Range("A1")
stringArr = Split(sourceRange.Value, ",")
For i = LBound(stringArr) To UBound(stringArr)
sourceRange.Offset(0, i + 1).Value = stringArr(i)
Next i
End Sub
You could avoid adding comments: Are you aware that users can add line breaks inside a cell by pressing ALT+RETURN?
If having high rows d is a problem and you don't like that formatting, an alternative approach might be to write a simple bit of code that changes the height of the current row when a user clicks in a certain range. It would , make other rows less high. Perhaps.
Just a thought. It has benefits keeping it simple.
Harvey.

Resources