Hyperlink not working correctly [duplicate] - vba

This question already has an answer here:
Macro to Hyperlink a cell to itself
(1 answer)
Closed 4 years ago.
I have two hyperlinks in my excel document.
On Sheet1, in cell A1 I have written the formula, =HYPERLINK("#'Sheet1'!A1","click").
On Sheet1, in cell A2 I have done what I think is the same but using a interactive GUI. Right click in cell A2 => Hyperlink => Place in this document => (Type the cell reference) A2.
I also have written a very short sub so that when I click on these hyperlinks I get a message box.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
MsgBox ActiveCell
End Sub
I have placed this macro in Sheet1.
When I click on cell A2 I get a MsgBox with the cell value in. As expected
However, when I click on cell A1 nothing happens.
Why are these two links behaving differently? How can I make cell A1 behave in the same way as cell A2 whilst still using a formula in the cell?
So Why?
I have a program that generates a CSV file. For simplicity the structure looks a little like this.
f1,f2,compare
f3,f2,compare
This means it is easy to overwrite the format.
I then open this file and save it as a .xlsm. What I wanted to have is when the compare is clicked it would run a macro. This could be in the form of a button or a hyperlink or anything else as long as it is obviously clickable.
Hence why I was going for the approach of =HYPERLINK("#'Sheet1'!A1","click") as it was easy to increment the number after the column and still show it was something that was clearly clickable.
Therefore it would be a quick change to get the CSV file output in a format of
f1,f2,"=HYPERLINK(""#'Sheet1'!C1"",""compare"")"
f3,f2,"=HYPERLINK(""#'Sheet1'!C2"",""compare"")"
The goal of this is to have something that is as automated as possible as this workbook could have over a thousand row so it is not feasible for me to manually sort out a compare button on each row
Update 2
When the compare button is clicked, a macro is run. This macro is called CompareFiles. It takes the values from the cells on the same row but in columns a and b, passes them into a shell command, and opens a different program that is used to compare the files.

What you can do is select all the cells that need linking to themselves and use a answer by Gary's Student to do it. The original post can be found here
Sub HyperAdder()
Dim r As Range, s As String
For Each r In Selection
If Len(r.Text) = 0 Then
s = "X"
Else
s = r.Text
End If
ActiveSheet.Hyperlinks.Add Anchor:=r, Address:="", SubAddress:=r.Parent.Name & "!" & r.Address(0, 0), TextToDisplay:=s
Next r
End Sub
If you are using a .vbs file like myself, in my case to convert the .csv and do a few other things, then you can use something similar to the following.
For Each cell In YourSheet.UsedRange.Columns("C:F").Cells
If Len(cell.Text) > 0 Then
ConflictsSheet.Hyperlinks.Add cell, "", "'" & cell.Parent.Name & "'" & "!" & cell.Address(0, 0), cell.Text
End If
Next

Related

VBA to create formula causes 1004 error when parentheses entered and causes an update values box when space is entered

I have a UserForm with TextBoxes that are used in creating new sheets from a template.
The gist of how this all works is that the VBA creates a copy of a "template sheet" that is hidden in the workbook. It names this copy based on the values entered by the user in the form's TextBox.
Additionally, I have a Summary sheet that is, as the name implies, a summary of different information entered in the sheet(s) the user has created.
To relieve the need for users to manually copy info into the Summary sheet, I'm using a formula tied to a function which will update the Summary sheet for them.
Now, when the user finishes entering their information into the UserForm, they click a Generate Workbook button which runs the code below which build formulas into the Summary sheet based on the info entered in the UserForm TextBox.
I have two issues when the VBA attempts to add a formula to the cells in the Summary Sheet.
Issue 1:
When a space is entered into the TextBox, it opens a "Update Values" dialog box when the user clicks the Generate Workbook button.
Example - Project 1234
Issue 2:
When parentheses are entered into the TextBox, a 1004 error occurs when the user clicks the Generate Workbook button.
Example - Project1234(Mobile)
If no parentheses and no spaces are entered, it all works fine.
There are 25 possible entries in the UserForm.
Each entry has three TextBoxes: ProjectNameTXT, SheetNameTXT, and ProjectNumTXT - each numbered 1-25 (i.e., ProjectNameTXT1, SheetNameTXT1, ProjectNumTXT1).
The two issues only pertain to the SheetNameTXT TextBoxes.
The csvRange is the function I mentioned earlier.
Here's the relevant code.
Code in UserForm1:
Private Sub GenerateWorkbook_Click()
Dim ws As Worksheet
Dim k As Long
Dim strSName
For k = 1 To 25
strSName = Me.Controls("SheetNameTXT" & k).Text
'Creates a data sheet for each project.
'Uses the MASTER SHEET as a template.
Set ws = ThisWorkbook.Worksheets("MASTER SHEET")
ws.Copy ThisWorkbook.Sheets(Sheets.count)
ActiveSheet.Name = strSName
ActiveSheet.Visible = xlSheetHidden
ThisWorkbook.Worksheets("Summary").Select
'THIS IS THE CODE CAUSING THE ISSUES
Range("B" & k + 3).Value = "=IF(ISERROR(csvRange(" & strSName & "!A2:A2500)),"""",csvRange(" & strSName & "!A2:A2500))"
Next k
Unload UserForm1
End Sub
Code for the Function csvRange:
Function csvRange(myRange As Range)
Dim csvRangeOutput
For Each entry In myRange
If Not IsEmpty(entry.Value) Then
'Create comma separated value
csvRangeOutput = csvRangeOutput & entry.Value & ", "
End If
Next
'Removes the last comma and space.
csvRange = Left(csvRangeOutput, Len(csvRangeOutput) - 2)
End Function
The csvRange function is a modified version of this function that muncherelli created:
https://superuser.com/a/241233
I'm not the world's greatest VBA coder, so apologies if my syntax or methodology sucks. Feel free to improve and provide suggestions if you are so inclined.
I searched StackOverflow and didn't find anything that would solve the problem. Tried some of the solutions suggested for similar problems but no luck getting them to fix these issues.
As always, your help and constructive criticism are much appreciated.
Put single quotes around your sheet name in your formula.
"=IF(ISERROR(csvRange('" & strSName & "'!A2:A2500)),"""",csvRange('" & strSName & "'!A2:A2500))

Hyperlink directs to hidden row

I have a workbook that has 2 sheets. Sheet 1 has hyperlinks to several different cells in Sheet 2. The issue is that there are filters in Sheet 2 that will hide rows, so when you try to follow the hyperlink from Sheet 1, the row is hidden and you can't see the target. I'm trying to figure out how to do the following:
On clicking a hyperlink, determine the target row in Sheet 2
If target row is hidden, unhide the target row in sheet 2, then follow the hyperlink.
It can stay unhidden after the hyperlink is followed, I'm fine with that. I've struggled with this for the past several days, and have come up with nothing successful. I've tried the "Followhyperlink" function, but I think this is too late - it's already followed the hyperlink, so unhiding the row at that point is too late.
Any suggestions? I'm stumped!
FollowHyperlink is indeed the event handler to use. Put this code in the worksheet module for Sheet1:
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim hyperlinkParts() As String
If ((Target.Type = msoHyperlinkRange) And (Target.SubAddress <> "")) Then
If (InStr(Target.SubAddress, "!") > 0) Then
hyperlinkParts = Split(Target.SubAddress, "!")
If ((Left$(hyperlinkParts(0), 1) = "'") And (Right$(hyperlinkParts(0), 1) = "'")) Then
hyperlinkParts(0) = Mid$(hyperlinkParts(0), 2, Len(hyperlinkParts(0)) - 2)
End If
Worksheets(hyperlinkParts(0)).Range(hyperlinkParts(1)).EntireRow.Hidden = False
End If
End If
End Sub
This checks that the hyperlink corresponds to a Range object then splits the target address into the sheet name and the specific cell(s). It then unhides the row(s) which correspond to the target address.
The check for the subaddress being empty is needed for hyperlinks to an external workbook where no particular cell is specified
edit: this approach won't work for hyperlinks to named ranges and I've altered the code to avoid getting an error message with that kind of hyperlink
edit2; code revised to deal with worksheet names containing spaces which were previously causing an error

how to create relative path in hyperlink excel ? (Word.Document.12)

I have two documents, one which has all the info and it is a word document, and another that is an excel document, that have just some highlights from the word document.
I want to create some links between some selected text in word and excel cells, so far the special past is doing a great job, and create link in this format
=Word.Document.12|'C:\Users\...\xxx.docx'!'!OLE_LINK9'
Now i want to copy both documents in my usb and past them in other computers, this where the problem is, i would have to do the special past all over again since the path is different now, what i though as a solution was to put the path to the word document in cell let say A1 and concatenate the formula above, something like
=Word.Document.12|A1!'!OLE_LINK9'
but it doesnt work, it throws an error message, can you please help me?
PS : I would like to avoid vba if possible
PS : I would like to avoid vba if possible
I have included both ways to do it since the question is tagged with Excel-VBA as well :)
Take your pick.
VBA Way
Is this what you are trying?
Sub Sample()
Dim objOle As OLEObject
'~~> Change this to the respective Sheet name
With ThisWorkbook.Sheets("Sheet1")
'~~> This is your embedded word object
Set objOle = .OLEObjects("Object 1")
'~~> Cell A1 has a path like C:\Temp\
objOle.SourceName = "Word.Document.12|" & .Range("A1").Value & "xxx.docx!'"
End With
End Sub
Non VBA Way
Create a named range and call it say Filepath. Set the formula to
="Word.Document.12|'" & Sheet1!$A$1 & "xxx.docx'!'"
Where Cell A1 will have the file path.
Next Select your word document and in the formula bar, type =Filepath and you are done.

Excel - Writing Macros that will write on the next line

I apologize in advance if my question is too simplistic but I'm rather new to coding.
I'm trying to gather an information from a table and have the code list it out on separate lines.
The problem is that I'm trying to figure out a function that write on the cell or cells below the cell with the function.
For example, if I enter my function gatherinfo(x,y,z) into A1, I want the output to be written on A1, A2, and A3 (assuming that it has three different outputs).
I've been trying to look around for a function/code that represents the cell that the formula is put into but I'm having a tough time looking for it. I know that Activecell function works when I click on the cell and then run the macro. But I want a function that will just work off of the cell that contains that function (without depending on the active cell).
I hope that I'm making some sense. Thanks all in advance for your help!!
Place the following code inside the worksheet code object :
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If Target.Formula Like "=gatherinfo(*,*,*)" Then
MsgBox "The formula " & Target.Formula & " was added to the cell " & Target.Address & "." & vbCrLf & vbCrLf & "Do something with it!"
End If
End If
End Sub
Worksheet code object :
You can then play around with the content of your worksheet and whenever you enter the function "gatherinfo" with 3 parameters, VBA will handle what to do. To know the row and column of the "Target" cell, you can use properties such as .Row and .Column.

VBA for excel Question : How to make VBA search for multiple texts in ranges?

Hello i want to make a form that have 2 or 3 text boxes that i can enter text values in it and vba search for this values if they are matched in 1 cell like if i enter "hello" in the first text box then i entered "world" in the other text box it search the hall collumn for a range that have "hello world" in it then copy paste it to another sheet and there are maybe multiple cells that have the same values till now i was able to do that for one value only , then i tried altering it to match wat i need but i couldnt here is wat i am at :
Private Sub CommandButton1_Click()
Dim rng As range
For Each rng In Worksheets("sheet1").range("a:a")
If rng.Value = TextBox1 Then
rng.EntireRow.Select
Selection.Copy Destination:=Worksheets("Sheet2").range("A" & Worksheets("Sheet2").range("A65536").End(xlUp).Row + 1)
End If
Next rng
End Sub
and actualy this one only works on finding the cell that only have the word i am searching for per example if i am searchin for "Java" and there is a cell that have .net and java it doesnt consider it in the copy process and i want it to find any cell with the search values i gave it
tyvm
I am not sure whether I totally got your question. But if you want to know whether one value is in a cell and another one. You need to use InStr.
The IF Statement above would look something like this (haven't tested it but should work)
If InStr(rng.Value, TextBox1) > 0 and InStr(rng.Value, TextBox2) > 0 then
Hope it helps.