Move down to the next row of filtered data vba excel - 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

Related

Click hyperlink in selected range

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

Re-run the same macro until last row of data

I'm a beginner. Just learning by Googleing, but cannot find a solution for this. Please help.
I want to run the below macro.
I have multiple cells named "CV_=CVCAL" in the same column.
What I want is for the macro to find the first cell with the value "CV_=CVCAL" and offset to the adjacent cell. If the adjacent cell has a particular value, if the value is below lets say "1.5" i want to fill it will a cell style 'bad'.
I want the macro to go through all the cells that have the name CV_=CVCAL and do the same thing until there is no more cells named CV_=CVCAL.
Sub If_CV()
Range("A1").Select
Set FoundItem = Range("C1:C1000").Find("CV_=CVCAL")
FoundItem.Offset(columnOffset:=1).Select
If ActiveCell.Value >= 1.5 Then
ActiveCell.Style = "Bad"
End If
End Sub
Sounds like you want to loop through your values.
Determine the end of your range
Loop through your range and check your criteria
Sub If_CV()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long, i As Long
lr = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
For i = 2 To lr
If ws.Range("C" & i) = "CV_=CVCAL" Then
If ws.Range("D" & i) >= 1.5 Then
ws.Range("D" & i) = "Bad"
End If
End If
Next i
End Sub
A basic loop would be simpler:
Sub If_CV()
Dim c As Range, ws As Worksheet
For Each ws in ActiveWorkbook.Worksheets
For Each c in ws.Range("C1:C1000").Cells
If c.Value = "CV_=CVCAL" Then
With c.offset(0, 1)
If .Value >= 1.5 Then .Style = "Bad"
End With
End If
Next ws
Next c
End Sub

VBA, Clearing Contents of a Selected Row from Column A to J

I need help creating a macro that will clear contents of a selected row but only from columns A to J, I can clear the entire row but that is not very useful as I have multiple tables on that row.
Please Help
Thank You!
Application.Intersect(Selection.EntireRow, Selection.Worksheet.Columns("A:J")).ClearContents
One row:
Option Explicit
Sub test()
Dim RowNo As Long
With ThisWorkbook.Worksheets("Sheet1") '<- Change sheet name if needed
RowNo = Selection.Row '<- Get row number
.Range("A" & RowNo & ":J" & RowNo).ClearContents '<- Clear range A to J of the selected line
End With
End Sub
Multiple rows:
Option Explicit
Sub test()
Dim rng As Range
With ThisWorkbook.Worksheets("Sheet1") '<- Change sheet name if needed
For Each rng In Selection.Rows
.Range("A" & rng.Row & ":J" & rng.Row).ClearContents
Next
End With
End Sub
You can run over every cell in your selectin, and clear its contents:
Sub test()
Dim R As Range
For Each R In Selection:
If R.Column <= 10 Then
R.ClearContents
End If
Next
End Sub
The Column being smaller than 10 means being somewhere from column A to J.

How can I repeat code through entire data?

I have written a few lines of code that work like I want them too but I don't know how to repeat it through all rows of my data.
This probably seems like a rather simple thing but since I started VBA just a few days ago I struggle with this line of code
If I continue with ActiveCell.Offset(-1,-4) after my code it's a bug and I don't know how to repeat the code through all rows.
Sub SelectRowsWithNoBlanks()
Range("A2").Select
If ActiveCell.Offset(0, 0).Value <> "" And ActiveCell.Offset(0, 1) <> "" And ActiveCell(0, 1) <> "" And ActiveCell(0, 1) <> "" Then
Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 4)).Select
End If
End Sub
#SiddharthRout As I don't have Access to the data yet I can't tell. But I thought extending the code for more columns later on wouldn't be a problem. So in the code I have written now I was checking for the columns A-D but I thought I could easily add the "checking" for more columns if needed – Anna von Blohn 43 secs ago
In that case, here is a sample code.
Logic
As #Pᴇʜ mentioned avoid the use of .Select. Work with the objects.
Find the last row and loop through the rows. To find the last you you may want to see This
One way (which I am using) is to count the number of cells which are filled using Application.WorksheetFunction.CountA. So if it is columns A to D then there should be 4 cells to be filled to consider the "row" as filled. Similarly for Cols A to E, there should be 5 cells to be filled to consider the "row" as filled as so on.
Code
I have commented the code. So if you have a problem understanding it, let me know.
Option Explicit
Sub SelectRowsWithNoBlanks()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Dim myRange As Range, rng As Range
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Find the last row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Loop through the rows
For i = 2 To lRow
'~~> Change this as applicable
Set rng = .Range("A" & i & ":D" & i)
'~~> Check if the range is completely filled
If Application.WorksheetFunction.CountA(rng) = rng.Columns.Count Then
'~~> Store the range in a range object
If myRange Is Nothing Then
Set myRange = rng
Else
Set myRange = Union(myRange, rng)
End If
End If
Next i
End With
'If Not myRange Is Nothing Then Debug.Print myRange.Address
'~~> Check if any filled rows were found
If Not myRange Is Nothing Then
With myRange
'
'~~> Do what you want with the range
'
End With
Else
MsgBox "No filled rows were found"
End If
End Sub

Excel VBA, How to select rows based on data in a column?

Sub SelectAllReleventText()
Do While Range(“A1”).Offset(1, 6) <> Empty
Rows(ActiveCell.Row).Select
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Here is my script, I've been told it doesn't do what it is meant to, which I expected since this was my first attempt. I am coming up with a variable not defined error. I thought I defined the variable, but I guess it wasn't specific enough for Excel VBA.
This is what I am attempting to do.
In Workbook 1, On B6 there is an alphanumeric name, I want that row to be selected.
Go down one row, if there is text there select that row.
Continue till text is no longer prevalent.
Copy selected rows.
Paste into another workbook (Workbook2), into tab 1, starting on row 2, since row 1 has headers.
Thanks in advance. Just a heads up, I am using the Options Explicit in my VBA because I was told it was the "right way to do thing"...
Yes using Option Explicit is a good habit. Using .Select however is not :) it reduces the speed of the code. Also fully justify sheet names else the code will always run for the Activesheet which might not be what you actually wanted.
Is this what you are trying?
Option Explicit
Sub Sample()
Dim lastRow As Long, i As Long
Dim CopyRange As Range
'~~> Change Sheet1 to relevant sheet name
With Sheets("Sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then
If CopyRange Is Nothing Then
Set CopyRange = .Rows(i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
End If
Else
Exit For
End If
Next
If Not CopyRange Is Nothing Then
'~~> Change Sheet2 to relevant sheet name
CopyRange.Copy Sheets("Sheet2").Rows(1)
End If
End With
End Sub
NOTE
If if you have data from Row 2 till Row 10 and row 11 is blank and then you have data again from Row 12 then the above code will only copy data from Row 2 till Row 10
If you want to copy all rows which have data then use this code.
Option Explicit
Sub Sample()
Dim lastRow As Long, i As Long
Dim CopyRange As Range
'~~> Change Sheet1 to relevant sheet name
With Sheets("Sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lastRow
If Len(Trim(.Range("A" & i).Value)) <> 0 Then
If CopyRange Is Nothing Then
Set CopyRange = .Rows(i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
End If
End If
Next
If Not CopyRange Is Nothing Then
'~~> Change Sheet2 to relevant sheet name
CopyRange.Copy Sheets("Sheet2").Rows(1)
End If
End With
End Sub
Hope this is what you wanted?
Sid
The easiest way to do it is to use the End method, which is gives you the cell that you reach by pressing the end key and then a direction when you're on a cell (in this case B6). This won't give you what you expect if B6 or B7 is empty, though.
Dim start_cell As Range
Set start_cell = Range("[Workbook1.xlsx]Sheet1!B6")
Range(start_cell, start_cell.End(xlDown)).Copy Range("[Workbook2.xlsx]Sheet1!A2")
If you can't use End, then you would have to use a loop.
Dim start_cell As Range, end_cell As Range
Set start_cell = Range("[Workbook1.xlsx]Sheet1!B6")
Set end_cell = start_cell
Do Until IsEmpty(end_cell.Offset(1, 0))
Set end_cell = end_cell.Offset(1, 0)
Loop
Range(start_cell, end_cell).Copy Range("[Workbook2.xlsx]Sheet1!A2")

Resources