Click hyperlink in selected range - excel

I am trying to create a macro that filters a sheet based on a certain value, then select a cell that contains a hyperlink and click on that hyperlink that leads to another sheet. I've already managed to achieve the first part but the hyperlink part isn't working, does anyone know how it could be done? Under these lines I'll copy the code I currently have and highlight the part that I have issues with. Thank you in advance.
Sub FilterBasedOnCellValueAnotherSheet()
Dim category As Range
Dim LR As Long
With Worksheets("Result")
Set category = .Range("B3")
End With
With Worksheets("Summary")
With .Range("B3:M300")
.AutoFilter Field:=1, Criteria1:=category, VisibleDropDown:=True
End With
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("L3:L" & LR).SpecialCells(xlCellTypeVisible).Select
**Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True**
End With
End Sub

Try something like this:
Sub FilterBasedOnCellValueAnotherSheet()
Dim category As Range
Dim LR As Long, rngVis As Range, c As Range
Set category = ThisWorkbook.Worksheets("Result").Range("B3")
With ThisWorkbook.Worksheets("Summary").Range("B3:M300")
.AutoFilter Field:=1, Criteria1:=category, VisibleDropDown:=True
On Error Resume Next 'ignore error if no visible rows
Set rngVis = .EntireRow.Columns("L").SpecialCells(xlCellTypeVisible)
On Error GoTo 0 'stop ignoring errors
End With
If rngVis Is Nothing Then Exit Sub
'follow the first link found
For Each c In rngVis.Cells
If c.Hyperlinks.Count > 0 Then c.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Exit For
Next c
End Sub

Related

Move down to the next row of filtered data vba excel

I have the sheet like this:
Picture given
All I want to is that, I can move down the next row and show its value.
I already have the code:
Sub test()
'Select the first row.
MsgBox Sheet1.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 3).Value
'Then move down to the second row of filtered data.
'Code
End Sub
Can someone suggest how to finish my sub above?
I would appreciate your help.
You could try implement/adapt the following:
Sub VisRows()
Dim rng As Range, lr As Long
With Sheet1 'Change accordingly
lr = .Cells(.Rows.Count, "B").End(xlUp).Row
Set rng = .Range("B2:B" & lr)
'Apply your filter
For Each cl In rng.SpecialCells(xlCellTypeVisible)
Debug.Print cl.Value
Next cl
End With
End Sub

Search a column on Sheet name "Current" for a partial value and past the row once found on Sheet "Results

I'm trying to loop thru an excel sheet named "Current" in a column and look for any values that can partially match a value from a Userform. Once found it would copy that row and past it in the next empty row in Sheet "Results"
I've tried a few examples I've found here and tried to customize them but no luck. Here is the code I have now.
Option Explicit
Dim rng3 As Range
Dim Cell As Range
Sub CheckFutureSchedule()
Dim strSearch As String
strSearch = UserForm3.TextBox1.Text
With Sheets("Current")
Set rng3 = Range("D:D").Find(strSearch, , xlValues, xlPart)
If Not rng3 Is Nothing Then
.Rows(Cell.Row).Copy Destination:=Sheets("Results").Rows(Cell.Row)
End If
End With
Worksheets("Results").Select
Unload UserForm3
End Sub
I'm not getting any errors just a blank sheet.
I went with this, I got results but I know it can be simplified.
Sub SearchSchedule()
Worksheets("Current").Activate
strSearch = UserForm3.TextBox12.Text
lastrow = "D" & Range("D" & Rows.Count).End(xlUp).Row
Dim Rng As Range
Set Rng = Range([D1], lastrow)
With Rng
.AutoFilter , field:=1, Criteria1:=strSearch
.SpecialCells(xlCellTypeVisible).EntireRow.Copy
Application.Wait (Now + TimeValue("0:00:01"))
Worksheets("Results").Activate
Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Columns.AutoFit
End With

Applying Same Excel-VBA Code in All Active Sheets

In need of help in applying the following code below for all sheets. I have tried the code I found online which is ApplyToAllSheets() but I am still new and I don't know how I can make it work. Please help.
Sub ApplyToAllSheets()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
Third wks
Next
End Sub
Sub Third(wks As Worksheet)
Dim Rng As Range
Dim cell As Range
Dim ContainWord As String
With wks
Set Rng = .Range(.Range("B1"), .Range("B" & .Rows.Count).End(xlUp))
End With
'For deleting the remaining informations not necessary
Set Rng = Range("B1:B1000")
ContainWord = "-"
For Each cell In Rng.Cells
If cell.Find(ContainWord) Is Nothing Then cell.Clear
Next cell
Set Rng = Range("C1:C1000")
ContainWord = "2019" 'change to current year
For Each cell In Rng.Cells
If cell.Find(ContainWord) Is Nothing Then cell.Clear
Next cell
Set Rng = Range("A1:A1000")
ContainWord = "-"
For Each cell In Rng.Cells
If cell.Find(ContainWord) Is Nothing Then cell.Clear
Next cell
'For deleting the blanks
On Error Resume Next
ActiveSheet.Range("B:B").SpecialCells(xlBlanks).EntireRow.Delete
On Error GoTo 0
'For shifting the date to the left
Columns("C").Cut
Columns("A").Insert Shift:=xlToLeft
Columns("C").Cut
Columns("B").Insert
'For deleting the negative sign "-"
With Columns("B:B")
.Replace What:="-", Replacement:=""
End With
End Sub
It should successfully apply the code to all the sheets
My result is that the first sheet was always cleared and the other sheets are untouched. please help
You've got unqualified - meaning the Worksheet isn't qualified - Range and Columns calls.
This is good - note the period in front of each instance of Range, as well as before Rows.
With wks
Set Rng = .Range(.Range("B1"), .Range("B" & .Rows.Count).End(xlUp))
End With
This, not so much:
Set Rng = Range("B1:B1000") ' no worksheet specified, so it's the ActiveSheet, not wks.
Or again:
Columns("C").Cut
Move that first End With all the way to the end of the Sub, and add a period in front of each instance of Range and Columns. By doing so, they will reference wks and not imply the ActiveSheet.
While you're at it, change that instance of ActiveSheet to wks. You want to work with wks, not the ActiveSheet.

How can I have my loop search for a value rather than a string of words?

I have some data that has both words and values in cells and I am trying to delete the rows that don’t have values in the cells. My code works now if all of the numbers are negative but if there are positive numbers then my code won’t work. How do I fix this?
Sub tval
Dim s As Long
Dim LastRow As Long
S=2
LastRow= cells.find(“*”,[A1],,, xlByRows,xlPreviousRow).row
Do until s>LastRow
DoEvents
If InStr(1,Cells(s,4), “-“) > 0 Then
S=s+1
Else
Cells(s,4).EntireRow.Delete
LastRow=LastRow -1
End if
Loop
End sub
When deleting rows, you should always start from the end.
Sub tval
Dim s As Long
Dim LastRow As Long
LastRow= Cells(Rows.Count, 1).End(xlUp).Row
For s= LastRow to 2 Step -1
If Not IsNumeric(Cells(s,4)) then
Cells(s,4).EntireRow.Delete
End if
Next s
End sub
This should work for you:
Sub tgr()
Dim ws As Worksheet
Dim rTextConstants As Range
Dim rTextFormulas As Range
Dim rCombined As Range
Set ws = ActiveWorkbook.ActiveSheet
'Exclude row 1 so that only text values found in rows 2+ are found
With ws.Range("A2", ws.Cells(ws.Rows.Count, ws.Columns.Count))
On Error Resume Next 'prevent error if no cells found
Set rTextConstants = .SpecialCells(xlCellTypeConstants, xlTextValues)
Set rTextFormulas = .SpecialCells(xlCellTypeFormulas, xlTextValues)
On Error GoTo 0 'remove on error resume next condition
End With
If Not rTextConstants Is Nothing Then Set rCombined = rTextConstants
If Not rTextFormulas Is Nothing Then
If rCombined Is Nothing Then Set rCombined = rTextFormulas Else Set rCombined = Union(rCombined, rTextFormulas)
End If
If Not rCombined Is Nothing Then
rCombined.EntireRow.Delete
Else
MsgBox "No cells containing text found in sheet '" & ws.Name & "'", , "Error"
End If
End Sub
May I suggest a bit of a different approach:
Before:
Code:
Dim RNG1 As Range, RNG2 As Range
Option Explicit
Sub TestCase()
With ActiveWorkbook.Sheets(1)
Set RNG1 = .Range("A1:A" & .Cells(Rows.Count, 1).End(xlUp).Row)
If RNG1.SpecialCells(xlCellTypeConstants, 1).Count <> RNG1.Cells.Count Then
Set RNG2 = Application.Intersect(RNG1, RNG1.SpecialCells(xlCellTypeConstants, 2))
RNG2.EntireRow.Delete
End If
End With
End Sub
After:
You'll need to change this around to suit your range obviously. It should be a good starting point nonetheless.
You can also use AutoFilter to filter the numbers, and delete the visible cells to accomplish this task. The code accounts for a header row.
With ThisWorkbook.Sheets("Sheet1")
With .Range("A1").CurrentRegion
.AutoFilter
.AutoFilter Field:=4, Criteria1:="<>*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilter
End With
End With

VBA macro delete cells containing #N/A and shift up the cells (not the rows)

I'm creating a ranking and I need to delete all the cells with #N/A (pasted as text, not formula) and to delete those cells and shhift them up.
The worksheet contains 503 raws and I need it from column A to T.
Thanks in advance, I have tried so many VBA codes of this web and I'm not able to find something that works.
Try,
dim rng as range
with worksheets("sheet1")
on error resume next
set rng = .range("A:T").specialcells(xlcelltypeformulas, xlerrors)
if not rng is nothing then
rng.delete shift:=xlup
end if
set rng = .range("A:T").specialcells(xlcelltypeconstants, xlerrors)
if not rng is nothing then
rng.delete shift:=xlup
end if
on error goto 0
end with
This should work. There are faster ways of doing what you ask, but since you don't have that big of a data set, I just modified some code I had available.
Sub KillPoundNa()
Dim rCell As Range, WS As Worksheet, KillRng As Range, UndesireableText As String
UndesireableText = "#N/A"
Set WS = ActiveSheet
Set KillRng = WS.Cells(Rows.Count, 1)
For Each rCell In WS.UsedRange.Cells
If InStr(1, rCell.Text, UndesireableText, vbTextCompare) > 0 Then
Set KillRng = Union(KillRng, rCell)
End If
Next rCell
KillRng.Delete (xlUp)
End Sub

Resources