Hyperlink to a cell on another sheet in Excel - vba

I am working on Excel report in which I need to pass a link which can take me to a cell referenced.
In below example, I have two sheets on same workbook. Now if I click on cell "A1" on Sheet:1, then it should take me to cell "E1" on Sheet:2.
I know by paste special I can pass hyperlink, but tricky part is data on Sheet:2 is dynamic so I can't go that route.
After looking on Google, I have found out multiple things, so I clubbed them together but it is throwing error "Reference is not valid".
Here is the formula that I've used on cell "A1" on Sheet:1:
=HYPERLINK("[Sample.xlsx]Sheet:2!(ADDRESS(MATCH(""ABC"",BEFORE,0),MATCH(""ABC"",BEFORE,0),1))","ABC")
Here BEFORE is a name range that covers A1 to E1.
Sheet:1
a b c d e
1. abc
Sheet:2
a b c d e
1. x x x x abc
Appreciate your inputs.

=HYPERLINK("[Sample.xlsx]'Sheet2'!" & ADDRESS(1,MATCH("abc",BEFORE,0)),"ABC")
If your sheet name might have spaces then make sure to add the single quotes around the name.

Related

Excel VBA Compare cell value to list and overwrite value in separate sheet

In a workbook I have, users either manually enter an account code or select one from a list and the account codes are placed in column C (C7:C446) in a sheet called "JE". The account codes look like this ####### - ### - ## - ######. In column D (D7:D446) in sheet "JE", there is a formula that captures the last 6 digits of the account code. In a sheet called "required_refs", there is a list of 6 digit codes in column A. If the value in the D column in sheet "JE" equals any of the values in column A of "required_refs" sheet, I would like the value in the D column cell to overwrite the cell value in cell D1 in a separate sheet called "references" (I know that may have been confusing, sorry)
Example: if the value of D25 matches any of the values listed in column A of sheet "required_refs", upon double clicking a red colored F25 cell, put the value of D25 (of sheet "JE"), and put it in cell D1 on sheet "references".
I've taken a crack at it as best I know how. I've placed this code in sheet JE:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim project As Range: Set project = Range("D7:D446")
Dim param As Range: Set param = Worksheets("references").Range("D1").Value
For Each cell In project
If project.Value = Worksheets("required_refs").Range("A:A").Value Then
Call gotoRef_ 'macro that simply selects/navigates to the required_ref sheet
project.Value = param
End If
End Sub
Thanks so much in advance for any suggestions on how to complete this. I can elaborate on this further if needed.
This will do what you want:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("F7:F446")) Is Nothing Then Exit Sub
Dim varReference As Variant
varReference = Columns("D").Cells(Target.Row).Value2
If Not IsError(Application.Match(varReference, Worksheets("required_refs").Columns("A"), 0)) Then
Worksheets("references").Range("D1").Value = varReference
End If
End Sub
Important Points:
Whenever working with event handlers, always limit the scope of the target range in the first line. Otherwise, it might not work correctly or it could slow done your spreadsheet.
Make sure your JE sheet column D values and required_refs sheet column A values are all either text or numbers. Otherwise the values won't be compared correctly.
Note the usage of Application.Match() instead of WorksheetFunction.Match() to access the worksheet function. This, coupled with the use of a Variant type variable, allows us to trap the error that occurs if the match fails.
You can always do this on the sheet. Consider the MATCH function. See here for how to use MATCH.
Or another great tool if you're searching for something in a table associated with a value in another column (not your case I don't think)--VLOOKUP formula. Place this formula in the D cell of the sheet you want to place the numbers in. VLOOKUP is in the following format:
=vlookup(lookup value,table_array,column index number, [range lookup])
The lookup value is the 6 digit code you're looking for (on the JE sheet)
The table_array is simply selecting the values you want to search for (required_refs sheet)
The column index number would be one, since the table only has 1 column. It's basically the column number of the value you're looking for.
And range lookup is for if you think there might be more than one place where it matches.
For your case I think it would look like this:
=vlookup('JE'!D1,'required_refs'!A1:A,1,FALSE)
Then just lock the values you want to keep and click and drag down.
Explanation for VLOOKUP here

Hyperlink a sheet to index page containing $ sign [duplicate]

I am working on Excel report in which I need to pass a link which can take me to a cell referenced.
In below example, I have two sheets on same workbook. Now if I click on cell "A1" on Sheet:1, then it should take me to cell "E1" on Sheet:2.
I know by paste special I can pass hyperlink, but tricky part is data on Sheet:2 is dynamic so I can't go that route.
After looking on Google, I have found out multiple things, so I clubbed them together but it is throwing error "Reference is not valid".
Here is the formula that I've used on cell "A1" on Sheet:1:
=HYPERLINK("[Sample.xlsx]Sheet:2!(ADDRESS(MATCH(""ABC"",BEFORE,0),MATCH(""ABC"",BEFORE,0),1))","ABC")
Here BEFORE is a name range that covers A1 to E1.
Sheet:1
a b c d e
1. abc
Sheet:2
a b c d e
1. x x x x abc
Appreciate your inputs.
=HYPERLINK("[Sample.xlsx]'Sheet2'!" & ADDRESS(1,MATCH("abc",BEFORE,0)),"ABC")
If your sheet name might have spaces then make sure to add the single quotes around the name.

Highlight a cell if its copied to another sheet

I have a formula that will copy values from one column on Sheet B to another column on another Sheet A. What I'd like to do is highlight the cells that were copied on Sheet B and highlight the cells in Sheet A that are not on Sheet B, essentially the inverse of the first part. On Sheet B only columns G and H would be highlighted but Sheet A could be from column A to H.
=IFERROR(INDEX(Sheet2!G$3:G$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)),G3)
You could set up conditional formatting with the same logic to change the colour or either the cell that you are setting or the one that you are copying from.
Beware: this kind of code can make your spreadsheet very slow if over used.
So in one range (sheet 2) , you would have conditional formatting set up to highlight the cell if
this match failed
MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)
In the other range (sheet 1 a:h)
you would highlight if ISERROR(INDEX(Sheet2!G$3:G$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:‌​$B$7=$C3),),0)))
has picked the value from sheet2
I may have incorrectly butchered your code, but you should get the idea. Where you currently have a range of values, select the range, insert the conditional formatting, but edit the range to only check the first cell, it will automatically increment for you (if you remove the appropriate $ signs)
conditional formatting intro

excel vba use command if

I have two excel files (file 1 with sheet 1 and file 2 with sheet 2).
Both have inside, in column A all the names (for example in A1: "a", in A2: "b" etc etc), in column B the values of relative lenghts (for example in B1: "2", in B2: "3" etc etc) but the orders of the things in the two sheets are different; so, I would want to say: "if the name in a cell in column A of sheet 1 is the same of the name in a cell in column A of sheet 2, so compare the relative values in column B and write TRUE in column C of one of the sheet (if the values are identical) or FALSE"
for you is possible? could you help me, please?
thanks in advance
If Sheet1 has
a 5
b 6
c 7
d 8
and Sheet2 has
c 3
d 8
a 5
then you can type
=IFERROR(IF(B1=INDIRECT("Sheet2!B"&MATCH(A1,Sheet2!A$1:A$4,0)),"Match Found",""),"")
into cell C1 of Sheet1 and drag the fill handle down and you will get
Match Found
Match Found
in column C of Sheet1. No VBA needed. :)
Note: Since your second sheet is actually in a separate file, you'll want to change Sheet2 to [file2.xlsx]Sheet1 in cell C1.
If you want to avoid INDIRECT function then try the following:
Assuming your Sheet1 of file1 is as follows:
And Sheet2 of another file is like:
Enter the following formula in Cell C1 of `Sheet2' and drag/copy it down as required.
=IFERROR(IF(B1=INDEX([file1.xlsx]Sheet1!$B$1:$B$5,MATCH(A1,[file1.xlsx]Sheet1!$A$1:$A$5,0)),TRUE),FALSE)
I would want to copy the word TRUE, only if I compare the same cell of the two sheets and I find that only a part of the name of that cell is the same ( because in reality in cell there is not only a, b, c but a sentence for example as "Sample 1 SX arrow" and others as "Sample 1 DX arrow") and so I want to put in column C the word TRUE only if not complete name in column A are the same but only part of the sentence ( for ecample only the part "SX arrow"!!!
How can I add this in the loop ?

macro code to show much input range in dropdown list

I need a macro code to display more than one input range in dropodown list that I created in sheet2 via the form control.
I want an event with IF logic, where I have many names range from another sheet with the name eg: DaftarA (in sheet1 C1:C30), DaftarB (in sheet1 C40:C60), DaftarC (in sheet1 C70:C90).
How to write macros if the value in sheet2 C1 1, the dropdown list will display the input range of (name range) DaftarA, if the value is 2 then displayed in the dropdown list is (name range) dDftarB and so on ..
is there anything that can help? thank you
You can define a name for the list through the offset formula and use that name as a list for data validation drop down box.
=OFFSET(Sheet1!$B$1,Sheet1!$F$1,0,Sheet1!$E$1,1)
where cell B1 is top of the list, cell F1 shows how much you offset down, cell E1 will control the length of the list. It will work however only if your multiple ranges are on the same sheet and in the same column
It will also work if you define a name with the following formula
=IF(Sheet1!$F$1=1,test1,IF(Sheet1!$F$1=2,test2,IF(Sheet1!$F$1=3,test3)))
However in this case you need to name each particular range you want(like in this example test1, test2, test3) and of course you can't put too many nested formulas as it will get messy.