Change hyperlink address automatically if the TextToDisplay changes - excel

I need a solution for the hyperlink in Excel. I want to make my entire hyperlink address auto-update if I change the text of the hyperlink.
For example, TextToDisplay of the hyperlink is:
D:/attachment/1000.jpg
then I change the TextToDisplay of the hyperlink to:
D:/attachment/1001.jpg
The hyperlink address changes the same as the display text if I change the text of that hyperlink.

Formula Solution
The easist way would be to use a helper column. For example write your URLs in column A and use the following formula in column B to create the hyperlinks. Everytime you change the URL in column A it will change the hyperlink accordingly.
=HYPERLINK(A:A)
VBA Solution
Alternatively you can use the Worksheet_Change event in VBA.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
If Cell.Hyperlinks.Count = 1 Then
Cell.Hyperlinks(1).Address = Cell.Value
End If
Next Cell
End Sub
Note that this code applies to all hyperlinks on the worksheet. If you want to limit it to a specific range do the following changes:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Me.Range("A:A"))
'limits the code to hyperlinks in column A.
'hyperlinks in other cells than column a are not changed
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange
If Cell.Hyperlinks.Count = 1 Then
Cell.Hyperlinks(1).Address = Cell.Value
End If
Next Cell
End If
End Sub

Related

Put first cell's value depend on which row is selected, into another cell

i need to write a cell value in other cell by clicking a range of cells as below:
my range is between b1:x30 and i want every time i click on these selection range then the value of b1 to b30 (depending which row selected) will write in cell z1
i tried to write a code as below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:X30")) Is Nothing Then
If Intersect(Target, Range("B1:X30")) Then
Worksheets("Order Sheet").Range("Z1").Value = .Range("B1:B30").Value
End If
End If
End If
End Sub
i know this code is not complete yet and i need to help to complete.

VBA code to select value from drop down menu on sheet and go to column with same value as header on same sheet - Excel 2010

Code below is supposed to make cursor 'jump'to a column in range of A10:T10, when column header is selected from drop down menu cell A6.
Sub JumpColumn(Target As Range)
If Target.Cells.Count = 1 And Target.Address = "$a$6" Then
Set Rng = Range("a10:t10").Find(What:=Range("a6").Value, LookIn:=xlValues)
Rng.Activate
End If
End Sub
As it is to run with another worksheet change event (PickName) it has been referred to in this code.
Private Sub Worksheet_Change(ByVal Target As Range)
PickName Target
JumpColumn Target
End Sub
PickName is working but not JumpColumn, what am I doing wrong? Thanks.

Add a Value Automatically to a Cell Based on Another Cell

I want to add a value to a cell based on another with VBA but I'm not sure how. I already searched on internet about it but can't find anything.
I have a table, and on the Column C, if any cell contains the text "MAM" (because it might have MAM-565), then change the value from Cell A to "Wrong", but if it contains "NAC", then change value to "Correct". It should be in the same row as the text found.
Also, I want to add the date automatically to cell E every time Cell in D is filled.
This the code I have already:
Private Sub Worksheet_Change(ByVal Target As Range)
'Add Issue Type'
Dim Code As Range
Set Code = Range("C2:C100000")
For Each Cell In Code
If InStr(1, Cell, "NAC") Then
Range("A2:A10000").Value = "Correct"
ElseIf InStr(1, Cell, "MAM") Then
Range("A2:A10000").Value = "Wrong"
End If
Next
End Sub
This how my table looks like:
Table
Thanks in advance guys :)
To automatically add the datestamp:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng as Range
Set rng = Intersect(Target, Me.Range("D:D"))
If rng Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
Dim cell as Range
For Each cell in rng
If Not IsEmpty(cell) Then ' don't do anything if cell was cleared
cell.Offset(,1).Value = Date
End If
Next
SafeExit:
Application.EnableEvents = True
End Sub
As far as the Correct/Wrong, this can easily be done with a formula (ISNUMBER(SEARCH(...)). I don't see the need for VBA here.
Even better, create a table using Ctrl+T. Excel will automatically add the formula in column A in new rows.

How to copy cell value to a specific cell in a new worksheet before double click?

I have a worksheet that has 6 columns and 45 rows. In column B, named I.D., each cell has an unique 3 letter identifier. What I want to do is to create a VBA code that will allow me to double click a cell within column B and copy it to a specific cell in a new worksheet. I'm having trouble specifying the range and transferring the information to a new worksheet. My code so far is as follows:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("B3:B45")) Is Nothing Then
cancel = True
Worksheets("Dashboard").Activate
End If
End Sub
No need to activate. Directly copy the relevant cell. Here is an example
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("B3:B45")) Is Nothing Then
Target.Copy Worksheets("Analysis").Range("A1")
cancel = True
End If
End Sub
I am copying to Cell A1 in the Analysis. Feel free to change it.

Worksheet_FollowHyperlink - get the cell value in which the hyperlink is located

I have a value in a cell and when I double click on it it takes me to the Named Range Account_Number (which resides on another worksheet) and updates the value.
My problem is I would like to adapt my code below so it will work with the Worksheet_FollowHyperlink(ByVal Target As Hyperlink) event.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If (ActiveCell.Column = 23 And Not ActiveCell.Value = "") Then
[Account_Number] = ActiveCell.Value
Application.GoTo Reference:=[Account_Number]
End If
End Sub
I would like to put a hyperlink for instance in cell J9 which contains the value 4111 and when I click on the hyperlink it would take me to the Named Range in the other worksheet and update the value of the Named Range to 4111.
I am uncertain how to dynamically assign the value to the Named Range. Can someone please let me know if this is possible and what the code should be?
Thank you
If you have made a hyperlink to a named cell, the way to copy the value from hyperlink source cell to its target would be:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveCell.Value = Target.Parent.Value
End Sub
You might want to apply this only to hyperlinks to particular named cell, like:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.SubAddress = "Account_Number" Then
ActiveCell.Value = Target.Parent.Value
End If
End Sub

Resources