Conditional Hyperlink if Value from Sheet1 Matches Value in Sheet2 - excel

I would like to create a conditional hyperlink in Column A (Link) of Sheet 1 to a matched cell in Sheet 2 when a value in Column B (Number) of Sheet 1 matches that of Column A in Sheet 2 (Number).
In the first instance, I don't even seem to be able to create a hyperlink between 2 sheets...
I also tried using the MATCH functions to at least confirm if I could accurately reference matching cell in a different worksheet and it seems to work; but just can't seem to (1) get hyperlink between sheets to work and (2) create hyperlinks conditionally to when there are matching values.
Unsure if I may need to use INDEX as well or Macros; just a bit lost on how to achiever my goal overall and would appreciate any tips from the community <3

In formula way, you can try function =CELL("address",INDEX(MATCH))) to return matched cell's address, then create link to it.
One more note, if you want to create link within your workbook, use '#" before cell's address.
=HYPERLINK("#"&CELL("address",INDEX(Sheet2!$A$2:$A$6,MATCH(B2,Sheet2!$A$2:$A$6,0))),B2)
Sheet1:

or Macro way:
Sub FindnLink()
Dim a As String
Set source_range = ThisWorkbook.Sheets("Sheet1").Range("B2:" & Range("B2").End(xlDown).Address)
Set search_range = ThisWorkbook.Sheets("Sheet2").Range("A2:" & Range("A2").End(xlDown).Address)
For Each cell In source_range
cell.Offset(0, -1).Select
a = cell.Value
Excel.ThisWorkbook.Sheets("Sheet1").Hyperlinks.Add Anchor:=Selection, Address:="", _
SubAddress:="Sheet2!" & search_range.Find(What:=cell.Value, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Address, _
TextToDisplay:=a
Next cell
End Sub

Related

Finding and selecting cell with the same value of another cell in VBA

I'm fairly new to working with macros in excel. I've made a dropdown list in sheet 1, cell A1. Based on the value of this cell, I need my macro to find and select the cell in sheet 2 (which is one unique cell) that corresponds with the value chosen in the dropdown list. I want to use this as a reference cell to copy a range.
Any suggestions on how I can do this?
Thanks in advance!
Range.Find can get that done for you.
Sheet2.Cells.Find(What:=DropdownValue).Select
Be careful of doing it in a single line though. This will raise an error if the Find method doesn't find any matches since you can't do Select on Nothing.
It's safer to do it in separate lines like:
Dim rFind As Range
Set rFind = Sheet2.Cells.Find(What:=DropdownValue)
If Not rFind Is Nothing Then rFind.Select
And for reliability and completeness, you should include the other arguments of Find, to ensure the method is using the matching rules that you expect. The most critical ones are LookIn, LookAt and MatchCase.
Set rFind = Sheet2.Cells.Find( _
What:=DropdownValue, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
MatchCase:=True _
)

Insert row after specific text is found

I am fairly new to excel and I am trying to setup a macro that adds a row after a specific point of the worksheet. Example: Row 2 contains text "Original", so it should insert a new row afterwards, and so on.
I know that it might be easier to insert before something, so I could change the setup (so for example the word "original" would be in row 2 and the new row is added above it) if that is an easier solution.
Is this possible? How?
Thanks for all the possible help.
Slightly simpler than the previous answer:
Sub NewRowInsert()
Dim SearchText As String
Dim GCell As Range
SearchText = "Original"
Set GCell = Cells.Find(SearchText).Offset(1)
GCell.EntireRow.Insert
End Sub
This will work with the current active sheet. If you want to use some other sheet, say Sheet2, you could use:
Set GCell = Worksheets("Sheet2").Cells.Find(SearchText).Offset(1)
and if you wanted to operate on a different workbook e.g. TestBook.xlsx, you could use:
Set GCell = Workbooks("TestBook.xlsx").Worksheets("Sheet2").Cells.Find(SearchText).Offset(1)
Note that I've avoided the use of select. This may not be an issue for you, but if you're searching through thousands of rows and making many replacements it could speed up your code considerably.
Try the code below = you can change the textvalue variable value to what ever you want to search for.
Sub newRow()
Dim textvalue As String
textvalue = "Original"
Cells.Find(What:=textvalue, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.offset(rowOffset:=1, columnOffset:=0).Activate
ActiveCell.EntireRow.Insert
End Sub

Excel Function to get an entire column based on a value within a row

What I want to do is something like this: "Get column where value in row 1 is equal to "X"". Then I can do additional stuff using that column like "Count all rows in this column where value is not "Y"".
You could use something like this below to find the desired value in a given row.
Sub Test()
Dim Row As Integer, FindWhat As String
Row = 1
FindWhat = "1"
Rows(Row & ":" & Row).Select
Selection.Find(What:=FindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
MsgBox ActiveCell.Address
End Sub
If 1 is found in cell E1, the result will be $E$1.
so if you have columns
A B C D
-------
1|2 3
2|3
3|2
4|2
And you want to count a the rows that only has value 3 in A use =countif(a:a, 3)
Hi is this a vba type thing or formula based....? If formula i would just use the match formula...say you want to find the column position where the word "dog" appears in row 1
Match("dog",1:1,0)
Then you could possibly build on this formula with the countif formula for your purpose originally.
If vba the first answer is solid from zedfoxis

Find Code from Another Sheet and Return Info From Same Row

I have searched to find the answers to get to where I am but am now stuck! I am a relative beginner with VBA.
I have a Workbook that lists a few hundred orders that we are producing for our customer.
The order details are on the first sheet called "In Progress" and on the 3rd sheet called "StyleData" are more details about each product such as its composition, design reference, SKU etc...
At present my code searches column A on the Data sheet based on the 6 digit style code in the active cell on the In Progress Sheet, then goes to that cell. I have put a MsgBox in purely to put a pause in the code so I know where it has got to.
What I want it to do after finding the style code on the data sheet is return a value on the same row from column H, preferable in a format that the use can select and copy, then it will return to the original cell at the start of the macro.
Code as follows:
Sub get_composition()
Dim item_no As String
Dim data_sheet As Worksheet
Dim found_item As Range
Set Rng = ActiveCell
item_no = ActiveCell.Value
Set data_sheet = Sheets("StyleData")
If Trim(item_no) <> "" Then
With Sheets("StyleData").Range("A:A")
Set found_item = .Find(What:=item_no, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not found_item Is Nothing Then
Application.Goto found_item, True
Else
MsgBox "Nothing found"
End If
End With
End If
MsgBox "Return to Original Cell"
Application.Goto Rng
End Sub
if I understand what you want :
you arrive at cell "found_item" and want to return a value from the same row.
If it's so, you can use method Offset on "found_item"
found_item.Offset() allow you to navigate from the current range
https://msdn.microsoft.com/en-us/library/office/ff840060.aspx
If you are on column A, found_item.Offset(, 1) will return the range on the same line but column B

Excel Dynamic Links

I know this is a simple fix, but all my searching has not uncovered the answer. I need to create a dynamic hyperlink.
Sheet 1
Column A1 = 3/0/1
Column A2 = 3/0/2
Sheet 2
Column C3 = 3/0/1
Column D3 = 3/0/2
Now, creating a link on Sheet 2 to the appropriate cell in Sheet 1 is easy. Think is, Sheet 1 has the potential to be sorted in a variety of ways. So I need the link dynamic. I need it to find "3/0/1" whether it's in column A1 or A77, or whatever.
I've seen examples using the ADDRESS function within the HYPERLINK function, but can't get it to work. Any ideas?
This might actually be one of those time when you want to use .Selection to govern which cells the macro is enacted upon.
Sub hlink_Sel()
Dim fnd As Range, sel As Range
On Error Resume Next
With Sheets("Sheet1")
For Each sel In Selection
Set fnd = .Columns("A:A").Find(What:=sel.Text, After:=.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False)
If Not fnd Is Nothing Then
Debug.Print fnd.Address(0, 0, xlA1, external:=True)
.Hyperlinks.Add Anchor:=sel, Address:="", _
SubAddress:=fnd.Address(0, 0, xlA1, external:=True), TextToDisplay:=sel.Text
End If
Next sel
End With
Set fnd = Nothing
End Sub
Just select some cells that are supposed to have their values duplicated in Sheet1's column A and run the macro. Given the odd nature of your sample data, I've used .Text as the search term in the find operation. Note that the .Address function has the external parameter set to true in order that the worksheet name is included.

Resources