what mean range.hyperlink(1).address = range.value - excel

what mean
range.hyperlink(1).address = range.value

First of all it is Hyperlinks (plural) then Range.Hyperlinks… needs to specify a range address like Range("A1").Hyperlinks…. Same for Range.Value.
So either
Range("A1").Hyperlinks(1).Address = Range("A1").Value
or
Dim Rng As Range
Set Rng = Range("A1")
Rng.Hyperlinks(1).Address = Rng.Value
So now what does it do?
It takes the first hyperlink that exists in the specified range eg Range("A1") and changes the URL of that hyperlink to the Value in that cell A1.
Be careful when you copy hyperlink cells
But be careful if you create a hyperlink eg in A1 and copy the whole cell into a range (more than one cell) A2:A10 the new inserted links will refer to the exact same hyperlink object, because the hyperlink object is only duplicated once (per inserted range) but not once (per inserted cell)! So in A2:A10 all cells now reference the same hyperlink object.
You can check that by using Cells.Hyperlinks.Count it will show 2 as result even if you see 10 hyperlinks in cells A1:A10.
That means if you then run
Range("A2").Hyperlinks(1).Address = Range("A2").Value
not only the URL of the hyperlink in A2 changes but the URL of the hyperlinks in A3:A10 are changed to the value of cell A2 too, because it is the exact same hyperlink object (they share).
To get distinct hyperlink objects in each cells A2:A10 you need to copy A1 and insert one by one each cell eg. paste in A2 then A3 then A4 …
This way for every pasted cell a unique hyperlink object is created.

Related

How to Copy and Paste cell with hyperlink based on drop down values

I am looking for a Excel VBA code to select the resulting Form address on Sheet 2 from the set of States on Sheet 1, but I need to copy the cell and paste it to retain the existing hyperlink attached to Form.
The only common data is the States.
I used INDEX MATCH but it only gets the word "Link" without the hyperlink itself.
I couldn't actually Select the cell, then copy and paste it.
Sheet 1
Sheet 2
I tried but don't have any idea how. As long when I choose what's selected on Sheet2 State (e.g Sheet2 Cell D2 - Alabama), the corresponding Form Link (e.g Sheet1 Cell B3 - Link) will be selected and copied through the Macro.
Sub test2()
Dim link As Range
Set link = Range("INDEX(Sheet1!B:B,MATCH(Sheet2State,Sheet1State,0),0)))")
Set s1 = Range("D5")
link.Copy
s1.PasteSpecial
End Sub

How to do a picture lookup using Formula in Excel

I am wondering if there is a solution to load a picture using formula (not VBA) from a list of pictures in Excel
For example,
=IF(TODAY()-B9<8,G6,"puste")
Let's say I have a picture in cell G6, that I want the formula to return if the condition is true.
In brief, the solution can be summarized in 2 steps:
Create a linked picture cell using PasteSpecial method of Excel.
Modify the formula of linked cell to "Named Range" formula for making it dynamic.
(Optional) - If there are many cells, and one find it tiresome to manually change the address of each linked image then use the below VBA macro to assign Named Range formula to all cells.
Sub Set_Formula_Linked_Cell()
Dim rngDest As Range
Dim objPic As Object
For Each rngDest In Range("F5:O18").Cells
rngDest.Select
Set objPic = ActiveSheet.Pictures.Paste(Link:=True)
objPic.Formula = "=Country_Flag"
Set objPic = Nothing
Next
End Sub
In Detail, let's follow through a similar situation:
Let's assume we have a list of Country and their adjacent flags.
Next step is to copy the Cell (any cell which contain the flag, do not copy picture but the Cell/Range) and paste as Linked Picture in the destination cell.
Now, a careful observation in the address bar reveals that current cell which displays a flag is linked to another cell. We need to change this formula. Unfortunately, we cannot change the formula here. Either it could be a direct reference or a named range but not a formula.
We will create a "Named Range" with the name "Country_Flag" and Formula as:
=INDEX(Sheet1!$B$2:$B$6,MATCH(Sheet1!$F$3,Sheet1!$A$2:$A$6,0))
In the last Step, we will assign this named range to the linked cell.

Is it possible to extract cell reference from a hyperlink?

I've been researching this one for a while now with no luck, so thought I would open it up here...
Let's say you have two worksheets in an excel workbook, e.g. sheet1 and sheet2.
Now, in sheet2 cell A1, say you have a hyperlink that refers/points/links to sheet1 cell A1. In other words, the value of the cell reference of the hyperlink at sheet2!A1 is sheet1!A1
Do you know if there is a formula or function that would return the cell reference of that hyperlink.
i.e.
=<formula-or-function>(sheet2!A1)
which returns 'sheet1!A1' as its result.
You can retrieve the .SubAddress from the Hyperlinks.Item Property.
'by the active cell
With ActiveCell
Debug.Print .Hyperlinks.Item(1).SubAddress
End With
'by worksheet and cell address
With Worksheets("Sheet2").Range("D6")
Debug.Print .Hyperlinks.Item(1).SubAddress
End With
This is, of course, VBA. I know of no way to perform this action with a worksheet formula short of a User Defined Function (aka UDF) written in VBA.

copying excel cells one by one and not range at once

I'm developing some excel macros, and now I'm stucked with following,
I want the macro getting the cells from another file and put it on the new one but it is important to consider that copy the full range is not an option, so for example first i Need to copy a1 then a2 , etc ...
the reason is because after each "paste" process, I have to check, the old value and then start a triger of another macro depending on both values, and if ai paste the full range at once it is not working,.
Try getting the Cells value at given row and column from the Worksheet objects you are manipulating.
Example : I want to copye the value (cell content only, not style) from worksheet1 cell A1 to worksheet2 cell B3.
worksheet2.Cells(3, 2).Value = worksheet1.Cells(1, 1).Value
Simple as that.

Excel VBA : How to access (Referenced) Ranges from Formula in a Cell

I have a cell (such as B2 in 'Sheet-1') containing a formula like:
='Sheet-2'!B2+'Sheet-3'!B3-'Sheet-4'!B4
Now I want VBA code that can access the references contained in the formula entered in that cell (i.e. B2 in Sheet-1) and then go to those references and make a change in those referred cells.
Expanding on z's comment somewhat.
Range(<cell>).Precedents returns a VBA Range representing all cells referred to by <cell> e.g. if cell A1 had the formula =B1+D1 then Range("A1").Precedents = Range("B1,D1")
Expanding this to further fulfil the requirements of your question we can then iterate over the cells within this range using a For Each loop as shown in the example below:
Public Sub AmendPrecedents(fromCell As Range)
Dim rngReferredTo As Range
Dim cell As Range
Set rngReferredTo = fromCell.Precedents
For Each cell In rngReferredTo
'Do something with this cell, in this case we're just coloring the background red
cell.Interior.Color = RGB(255, 0, 0)
Next cell
End Sub

Resources