I have a document that needs to generate a hyperlink to a cell in the same workbook by getting the address from another cell. Here's what I have right now:
=hyperlink(CELL("address",INDEX('Budget Record'!C3:C105,MATCH(Y73,'Budget Record'!C3:C105,0),1)))
This displays the appropriate location:
'[Calendar Budget.xlsx]Budget Record'!$C$3
However, when clicked, it says that Excel cannot open the specified file.
I have tried manually creating a hyperlink to that value, and it still doesn't appear to work:
=hyperlink('[Calendar Budget.xlsx]Budget Record'!$C$3)
However, if I plug that into the goto dialogue box, it has no problems with it.
Am I missing an extra step?
After looking into all of the built-in hyperlink options that Excel offers, I could not find a way to have Excel link you to a dynamic/ changing cell reference. Naturally, I turned to VBA :)... this is a very simple macro with only a few lines of code. Give it try:
Press Alt + 11 to open the Visual Basic Editor (VBE)
If your project explorer is not already open, click this icon:
Double click the worksheet tab that you want the hyperlink to work on:
Paste the following code into the white space:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'check if user is selecting the 'hyperlink' cell
If Target.Address = "$A$1" Then
'run the 'selectCell()' subroutine
Call selectCell
End If
End Sub
Sub selectCell()
Dim goToAddress As String
'get cell address
goToAddress = Range("A2").Value
'send user to this cell
Range(goToAddress).Select
End Sub
Go to your worksheet and test the script by typing G3 in cell B1.
Select cell A1. The macro should have automatically selected cell G3.
Hope this helps!
(Posted on behalf of the OP).
I have identified a solution. Basically it looks like the problem was in the formatting.
Basically I needed three steps. The first one is getting the location of the cell returned as a full address (including the file, sheet, and Cell). I do that with this:
=CELL("address",INDEX('Budget Record'!C3:C105,MATCH(Y73,'Budget Record'!C3:C105,0),1))
Here's a sample of what that results in:
'[Calendar Budget.xlsx]Budget Record'!$C$3
The problem with this is that the apostrophe at the beginning is in the wrong spot. To work as a hyperlink, the string should be this:
[Calendar Budget.xlsx]'Budget Record'!$C$3
So, I have a step where I remove the first 22 characters of the starting string:
=RIGHT(Z73,LEN(Z73)-23)
This results in the following:
Budget Record'!$C$3
Next, I need to add on this to the start of the string:
[Calendar Budget.xlsx]'
I do that with the following:
=HYPERLINK("[Calendar Budget.xlsx]'"&AA73)
The resulting output looks like this:
[Calendar Budget.xlsx]'Budget Record'!$C$3
So, we have a hyperlink to a cell in another sheet that dynamically changes depending on the initial referenced cell.
Related
I have an excel sheet with lots of data.
I would like to implement a "search box" at the top, where a user can type in a term/string, click a button, and excel will highlight any cell that contains the string.
However, I also want these cells to "un-highlight" once the user mouse clicks anywhere in the document.
I cannot seem to find the VBA code for this...mainly the last part.
Thanks
I was trying to solve the problem with Conditional Formatting but couldn't make it work, so now I am looking to VBA for the solution. However, I am not familiar with mouseclick properties.
Conditional Formatting + VBA to clear
The following formula in the "Use formula to determine which cells to format" will highlight any cells that "contain" the search phrase:
=NOT(ISERROR(FIND($C$2,B5,1)))
You can see we use `FIND([the search bar value in $C$2 ], [in dynamic B5 so it applieas separately to each cell in search range],[starting at 1]).
If it finds the value it will not be error, if it doesn't find, it will be error.
If we delete the cell contents, all will be formatted. to fix this we can either amend our formula to include an if statement checking if search bar is empty, or simply add a second conditional formatting:
=ISBLANK($C$2)
If you want to make the formatting clear when to click away, then you need to access the VBA worksheet module:
Next you'll want to paste the following code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim sBar As Range
Set sBar = Range("C2")
If Selection.Address <> sBar.Offset(1, 0).Address Then sBar.Value = ""
End Sub
Finished Product:
I am writing an Excel macro to send emails from data in a spreadsheet. The data are in a table with each column providing different variables to create the email (to:, cc:, subject, attachments, etc).
I've got the macro to do what I want on one line of the table. My question is:
How do I scale the VBA code to work for each line of my table? I would like a hyperlink in each row to run the macro using the data in that row. Below is a small snippet of my code as an example:
Sub SendMail()
Dim xContractNumber As String
xContractNumber = Worksheets("Program Info").Range("L10").Value
End Sub
In the above example, I would like a hyperlink that runs the macro using the data in row 10 of the 'Program Info' sheet... And another button or link that would run the macro using data in row 11, and so on.
This answer attempts to combine many of the good answers and comments everyone has already given. The code below contains the functionality for both buttons and hyperlinks in a condensed way.
This code could go in one separate module:
Sub SendMailByButton()
SendMailForRow ActiveSheet.Buttons(Application.Caller).TopLeftCell.Row
End Sub
Sub SendMailForRow(ByVal r As Long)
If r < 1 Then Exit Sub 'Failsafe in case the row number is invalid
Dim xContractNumber As String, xValueInColumnM As String, xValueInColumnN As String
xContractNumber = ActiveSheet.Cells(r, 12).Value 'Col 12 is col "L"
xValueInColumnM = ActiveSheet.Cells(r, 13).Value
xValueInColumnN = ActiveSheet.Cells(r, 14).Value
'...etc.
'...Rest of code to send the actual email
End Sub
If buttons are used, SendMailByButton must be attached to every button's click event, and the above code would be enough.
If manually-added hyperlinks are used, the above code would need to be complemented with the following code in the sheet module for every sheet that uses the hyperlinks (in your case, you may only need to add this code in one sheet's module) ...
'This event is fired when the hyperlink is clicked
Private Sub Worksheet_FollowHyperlink(ByVal target As Hyperlink)
On Error Resume Next
SendMailForRow target.Range.Row
End Sub
Each manual hyperlink would have to link to the same cell it is sitting on (i.e. to a "Place in This Document" with the cell reference set to its current cell).
The problem remains that you will need to manually create a button or hyperlink for every row in your table, which can be a hassle, especially if there are many rows or if the number of rows can grow in future.
A way to circumvent this problem is to have an extra button at the top of the table that allows the user to automatically create the buttons and/or hyperlinks for each row of data (removing excess buttons or hyperlinks if the table shrinks in size). This may require that you post a separate question.
Another way to circumvent this problem is to forego buttons altogether and, instead, use Excel formulas with the native HYPERLINK function (replacing the "regular" links). In that case, the FollowHyperlink event handler above would no longer be needed, but you would need to add the following function (which can go in the same module where SendMailForRow would reside) ...
Function SendMailByHLink()
SendMailForRow ActiveCell.Row
Set SendMailByHLink = ActiveCell
End Function
You would then have to create an Excel formula such as the following in each row (in the column where you want the hyperlink) ...
=HYPERLINK("#SendMailByHLink()", "Send email")
Entering this formula will auto-generate a hyperlink in the cell, and it will tell Excel to execute function SendMailByHLink when the hyperlink is clicked. The function after the "#" is supposed to return the link's target, which is why SendMailByHLink returns ActiveCell to ensure that the focus remains on that cell (if you prefer, you could return another cell such as ActiveCell.Offset(, -2) so that the user is taken to the cell 2 columns back in the same row after the link is clicked). Before returning ActiveCell to Excel, SendMailByHLink will execute the email-sending code.
The nice thing about using a HYPERLINK formula is that you can easily copy/paste the formula up and down the all the rows in the table. Therefore, if your table increases in size, all the user has to do is to copy/paste the HYPERLINK formula into the new rows. The user can also delete excess HYPERLINK formulas if the table shrinks. It may even be possible to have Excel automatically copy the formula if the data is sitting on an official Excel table by using a calculated column.
Sorry for all the extra explanation. If you focus on the code blocks, you will see that the solution is simpler than it looks.
There are a lot of similar questions on here about VBA and hyperlink in excel, but nothing that I could modify to my own situation.
On one sheet I have a column of names (E:E), and on another I have a cell (D13).
What I want to do is hyperlink each cell in column (E:E) such that, when clicked, the hyperlink not only takes me to cell (D13) but also populates cell (D13) with the name that I clicked on.
So, click "John Smith" (Sheet1!E1) ---> (Sheet2!D13) = "John Smith"
First you need a tiny piece of code in a standard module to install the hyperlink in Sheet1:
Sub MakeLink()
Sheets("Sheet1").Hyperlinks.Add Anchor:=Range("E1"), Address:="", SubAddress:="Sheet2!D13", TextToDisplay:="Stuff"
End Sub
Then you need an event macro in the worksheet code area to do the content transfer:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveCell.Value = Sheets("Sheet1").Range(Target.Parent.Address).Value
End Sub
I think the best solution may be to use VBA. If you're unsure on how to do that, record s macro that selects a cell in e, then goes to D13 and updates its value, then look at the code that was recorded and use those snippets to create your code. At a high level you'd want to trigger a macro on click of any cell in column E that does something like:
Dim e_value = <value of selected cell in column E>
ThisWorkbook.Sheets("name of sheet 2").Range("D13") = e.value
Application.Goto Reference:=Worksheets("Sheet2").Range("D13"), Scroll:=False
Note that the above contains pseudocode, but it should give you a general idea on how you could proceed.
I have a cell whose value is constantly changing through a feed. I want to develop a macro which when activated records the cell value at that instant and pastes it in another cell. Tried finding some formula to do that but no success.
You won't be able to do that with a formula. However, you can write a macro that checks if your cell was changed:
Private Sub Worksheet_Change(ByVal Target As Range)
...
End Sub
If you don't want this type of automation, you could simply add a button to your worksheet which triggers a macro when clicked and does what you are looking for.
To copy the value of a cell into another one, you could use this simple solution:
Range("B1").Value = Range("A1").Value
This will copy the value of cell A1 into cell B1.
I need to copy a column that has linked text and paste a column that shows all the URL’s for the linked text
Function GetURL(rng As Range) As String
On Error Resume Next
GetURL = rng.Hyperlinks(1).Address
End Function
In this case you can place it where you want. If you want, for example, the URL from a hyperlink in A1 to be listed in cell C25, then in cell C25 you would enter the following formula:
=GetURL(A1)
This post discusses extracting the URL from a cell with a link in it using a custom formula.
This immediately does the job, and add a separate url link column next to the column with the hyperlinked text:
https://howtouseexcel.net/how-to-extract-a-url-from-a-hyperlink-on-excel
Extracting a URL from a hyperlink on Excel -- this worked for me!
If you want to run this operation one time
Open up a new workbook.
Get into VBA (Press Alt+F11)
Insert a new module (Insert > Module)
Copy and Paste the Excel user defined function below (customized function):
Sub ExtractHL()
Dim HL As Hyperlink
For Each HL In ActiveSheet.Hyperlinks
HL.Range.Offset(0, 1).Value = HL.Address
Next
End Sub
Press F5 and click “Run”
Get out of VBA (Press Alt+Q)
You will see a new column with a list of url added to the right.