Excel hyperlinks.add anchor syntax/argument - excel

Below is a bit of code I'm using to track changes to an excel doc. I get a runtime error '5' "Invalid procedure call or argument" on the bolded bit. I think the issue is the syntax or arguments for the Hyperlinks.Add anchor, since 'anchor' doesn't capitalize when I go to the next line. Do I have the arguments and the syntax correct?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sSheetName As String
sSheetName = "1107"
If ActiveSheet.Name <> "LogDetails" Then
Application.EnableEvents = False
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1,0).Value = ActiveSheet.Name & " - " & Target.Address(0, 0)
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = OldValue
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Value
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Environ("username")
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = Now
**Sheets("LogDetails").Hyperlinks.Add anchor:=Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 5), Address:="", SubAddress:="'" & sSheetName & "'!" & OldAddress, TextToDisplay:=OldAddress**
Sheets("LogDetails").Columns("A:D").AutoFit
Application.EnableEvents = True
End If
End Sub

Consider removing the apostrophes when setting the SubAddress
Example recorded with Excel:
ActiveSheet.Hyperlinks.Add anchor:=Selection, Address:="", SubAddress:= _
"Sheet1!A1", TextToDisplay:="Sheet1!A1df"
Code
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
With Excel.Application
.EnableEvents = False
End With
Dim sSheetName As String
sSheetName = "1107"
If ActiveSheet.Name <> "LogDetails" Then
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name & " - " & Target.Address(0, 0)
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = OldValue
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Value
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Environ("username")
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = Now
Dim hlink_cell As Range
Set hlink_cell = Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 5)
Sheets("LogDetails").Hyperlinks.Add anchor:=hlink_cell, Address:="", SubAddress:=sSheetName & "!" & OldAddress, TextToDisplay:=OldAddress
Sheets("LogDetails").Columns("A:D").AutoFit
End If
With Excel.Application
.EnableEvents = True
End With
End Sub

Related

Why does changing FontStyle, when there is a copy paste from keyboard, cause clipboard to be cleared?

The following code would highlight the current row and restore the previous row.
Private Sub mcrToggleHighlightedRow(iPassedPrevRow As Integer, iPassedCurRow As Integer)
'2022.05.16 - changing the font.fontstyle was forcing the clipboard to be cleared/deleted.
' Couldn't do any copy and pasting
'remove highlighting on previous row
If iPassedPrevRow <> 0 And iPassedPrevRow >= 16 Then
'Non fillable rows - Reset
With Range("A" & iPassedPrevRow & ":C" & iPassedPrevRow)
.Interior.Color = xlNone
' .Font.FontStyle = "Regular"
End With
With Range("D" & iPassedPrevRow & ":D" & iPassedPrevRow)
.Interior.Color = xlNone
' .Font.FontStyle = "Italic"
' .Font.Size = 8
End With
With Range("S" & iPassedPrevRow & ":S" & iPassedPrevRow)
.Interior.Color = xlNone
' .Font.FontStyle = "Regular"
End With
'Yellow Fillable Rows - reset
With Range("E" & iPassedPrevRow & ":E" & iPassedPrevRow)
.Interior.Color = xlNone
End With
With Range("J" & iPassedPrevRow & ":L" & iPassedPrevRow)
.Interior.Color = xlNone
End With
End If
'if selected cell is not in the detail lines, exit
If iPassedCurRow < 16 Then Exit Sub
If iPassedCurRow <> 0 Then
'Non fillable rows
With Range("A" & iPassedCurRow & ":C" & iPassedCurRow)
.Interior.Color = 16777164
' .Font.FontStyle = "Bold"
End With
With Range("D" & iPassedCurRow & ":D" & iPassedCurRow)
.Interior.Color = 16777164
' .Font.FontStyle = "Bold Italic"
' .Font.Size = 12
End With
With Range("S" & iPassedCurRow & ":S" & iPassedCurRow)
.Interior.Color = 16777164
' .Font.FontStyle = "Bold"
End With
'Yellow Fillable Rows
With Range("E" & iPassedCurRow & ":E" & iPassedCurRow)
.Interior.Color = 13434879
' .Font.FontStyle = "Regular"
End With
With Range("J" & iPassedCurRow & ":L" & iPassedCurRow)
.Interior.Color = 13434879
End With
End If
Before commenting out the .Fond.FontStyle lines, I would CTRL-C to copy and put something in the clipboard, it would be cleared from the clipboard after moving to a new cell, thus not able to CTRL-V and paste it.
Once I commented out the lines, copy and paste works again.
What is causing the clipboard to be cleared?

Lock certain cells in the same row based on updates by a formula

I'm trying to lock/unlock and colour certain cells within the same row whenever one of the cell is being updated by a formula (e.g. Column AM5, AM6, etc..). This will be repeated for multiple rows (e.g. T5:AA5, T6:AA6, etc..) . Currently I have the following macro, but it only allows the update whenever the user manually inputs, but doesn't work for updates by formula.
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect
If Intersect(Target, Range("AM:AM")) Is Nothing Then Exit Sub
Select Case Target.Value
Case "L"
Range("T" & Target.Row & ":AA" & Target.Row).Locked = True
Range("AG" & Target.Row & ":AH" & Target.Row).Locked = True
Range("T" & Target.Row & ":AH" & Target.Row).Interior.ColorIndex = 1
Case Else
Range("T" & Target.Row & ":AA" & Target.Row).Locked = False
Range("AG" & Target.Row & ":AH" & Target.Row).Locked = False
Range("T" & Target.Row & ":AH" & Target.Row).Interior.ColorIndex = xlNone
End Select
ActiveSheet.Protect UserInterfaceOnly:=True, AllowFiltering:=True
End Sub

Condense multiple combobox coding on Microsoft Excel VBA

I'm trying to condense the following code. I'm thinking a loop function may work, but it also may not because of the difference in VBA item. Any insight?
If CheckBox1.Value = True _
Then
Range("P" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("P" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox2.Value = True _
Then
Range("Q" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("Q" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox3.Value = True _
Then
Range("R" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("R" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox4.Value = True _
Then
Range("S" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("S" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox5.Value = True _
Then
Range("T" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("T" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox6.Value = True _
Then
Range("U" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("U" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox7.Value = True _
Then
Range("V" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("V" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox8.Value = True _
Then
Range("W" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("W" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox9.Value = True _
Then
Range("X" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("X" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox10.Value = True _
Then
Range("Y" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("Y" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox11.Value = True _
Then
Range("Z" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("Z" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox12.Value = True _
Then
Range("AA" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("AA" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox13.Value = True _
Then
Range("AB" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("AB" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
If CheckBox14.Value = True _
Then
Range("AC" & Rows.Count).End(xlUp).Offset(0, 0).Value = "Y"
Else
Range("AC" & Rows.Count).End(xlUp).Offset(0, 0).Value = "N"
End If
Something like this:
Dim i As Long, ws As Worksheet
Set ws = ActiveSheet 'or whatever
For i = 1 To 14
ws.Cells(Rows.Count, "P").Offset(0, i - 1).End(xlUp).Value = _
IIf(Me.Controls("CheckBox" & i).Value = True, "Y", "N")
Next
Shouldn't that .Offset(0, 0) be .Offset(1, 0) though? Or you're just overwriting the value already there.
This is not a full answer - but the bones of a solution could include this structure
Dim xCtrl As Object, dVal As Variant, xRng As Range
For Each xCtrl In Me.Controls
If Left(xCtrl.name, 8) = "CheckBox" Then
dVal = Val(Mid(xCtrl.name, 9))
If dVal >= 1 And dVal < 15 Then
If xCtrl.Value = True then
' ComboBox is True ... Update here
Else
' ComboBox is False ... Update here
End If
End If
End If
Next xCtrl

PageSetup.RightFooter text remains after switching to PageSetup.CenterFooter

I changed the code for the footer of my PDF from .RightFooter = "Page &P of &N" to .CenterFooter = "Page &P of &N".
Now the "Page &P of &N" shows in the center and on the right even though the .RightFooter is no longer there.
I have deleted the Module and recreated it. I have restarted my PC thinking it was hung in memory.
Here's my code.
Sub Set_PrintRnag()
Dim LstRw As Long
Dim Rng As Range
Dim strDesktop As String
strDesktop = CreateObject("WScript.Shell").SpecialFolders("Desktop")
LstRw = Sheet2.Cells(Rows.Count, "R").End(xlUp).Row
Set Rng = Sheet2.Range("R1:S" & LstRw)
With Sheet2.PageSetup
.LeftHeader = "&C &B &20 Cohort List Report:" & Format(Date, "mm/dd/yyyy")
.CenterFooter = "Page &P of &N"
End With
Rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strDesktop & "\CohortList " & " " & Format(Date, "mm-dd-yyyy") & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Found my own answer. Had to use .RightFooter = "".

'Run-time error 1004: Method 'Range' of object '_Global' failed' only with xls files, but not with txt or xlsx files

I have this piece of code which searches for a phrase, sets the range as the cells to the right of the cell containing the phrase all the way to the last data column and formats the cells with conditional formatting. This code works fine with both txt files and xlsx files, but the phrase
Set rngHeaderAs
gets run-time error 1004: Method 'Range' of object '_Global' failed when I run the code on an xls files. The phrase in the Find section definitely exists, and if I save the very same xls file as an xlsx file, the code runs perfectly.
The code:
Sub Color_labreport_horizontal()
Cells.Replace What:="n,d.", Replacement:="n.d.", lookat:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
On Error Resume Next
'As
Set rngHeaderAs = Range("A1:ZZ200").Find("As*Arsen*", lookat:=xlPart) 'This string generates the error
Set rngAs = Range(rngHeaderAs, rngHeaderAs.End(xlToRight))
AsAddress = rngHeaderAs.Address(False, False)
Dim Ul1As As Double
Ul1As = 8
Dim Ul2As As Double
Ul2As = 20
Dim Ul3As As Double
Ul3As = 50
Dim Ul4As As Double
Ul4As = 600
Dim Ul5As As Double
Ul5As = 1000
With ActiveSheet
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & "<" & Ul1As & ")"
.FormatConditions(1).Interior.ColorIndex = 33
.FormatConditions(1).Borders.LineStyle = xlContinuous
.FormatConditions(1).Borders.Weight = xlThin
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul1As & ";" & AsAddress & "<" & Ul2As & ")"
.FormatConditions(2).Interior.ColorIndex = 4
.FormatConditions(2).Borders.LineStyle = xlContinuous
.FormatConditions(2).Borders.Weight = xlThin
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul2As & ";" & AsAddress & "<" & Ul3As & ")"
.FormatConditions(3).Interior.ColorIndex = 6
.FormatConditions(3).Borders.LineStyle = xlContinuous
.FormatConditions(3).Borders.Weight = xlThin
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul3As & ";" & AsAddress & "<" & Ul4As & ")"
.FormatConditions(4).Interior.ColorIndex = 45
.FormatConditions(4).Borders.LineStyle = xlContinuous
.FormatConditions(4).Borders.Weight = xlThin
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul4As & ";" & AsAddress & "<" & Ul5As & ")"
.FormatConditions(5).Borders.LineStyle = xlContinuous
.FormatConditions(5).Borders.Weight = xlThin
.FormatConditions(5).Interior.ColorIndex = 3
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul5As & ")"
.FormatConditions(6).Interior.ColorIndex = 7
.FormatConditions(6).Borders.LineStyle = xlContinuous
.FormatConditions(6).Borders.Weight = xlThin
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=LEFT(" & AsAddress & ";1)=""<"""
.FormatConditions(7).Interior.ColorIndex = 33
.FormatConditions(7).Borders.LineStyle = xlContinuous
.FormatConditions(7).Borders.Weight = xlThin
End With
With rngAs
.FormatConditions.Add xlExpression, Formula1:="=(" & AsAddress & ") = ""n.d."""
.FormatConditions(8).Interior.ColorIndex = 33
.FormatConditions(8).Borders.LineStyle = xlContinuous
.FormatConditions(8).Borders.Weight = xlThin
End With
End With
Any idea why this happens only when I run it on xls files?
Ron Rosenfelt gave me the answer in the comment. ZZ200 was outside the allowable range, so instead of defining the range as A1:ZZ200 (or some other hardcoded range), I used CurrentRegion:
Set rngHeaderAs = Range("A1").CurrentRegion.Find("As*Arsen*", lookat:=xlPart)
to make sure that I cover the range I want without exceeding allowable range. Now the code runs perfectly also in Excel 97-compatible files.

Resources