最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hide - Excluding rows in excel based on certain text - Stack Overflow

programmeradmin1浏览0评论

I have been trying to write a macro to exclude rows containing \ in column V. The data I will be working with will have a variable number of rows so I cant specify the last row. I have found various codes but none of them seem to work. The current one I am working on is a mix of two. I have no experience of working with VBA so the simpler the better.

Sub HideRowsContainingContent()
    Dim ws As Worksheet
    Dim cell As Range
    Dim searchText As String
    Dim lastRow As Long
    
    ' Set the worksheet you want to work on
    Set ws = ThisWorkbook.Sheets("R filtered")
    
    ' Define the content to search for
    searchText = "\\"
    
    ' Find the last row with data in the worksheet
    lastRow = ws.Cells(ws.Rows.Count, "V").End(xlUp).Row
    
    ' Loop through each cell in the used range
    For Each cell In ws.Value = "\\" Then Each cell.EntireRow. Hidden = True
    Else
    For Each cell In ws.EntireRow. Hidden = False
    End If

Each of the For Each statements under the Loop through section, generated the compile error Expected: end of statement.

Thanks Bex

I have been trying to write a macro to exclude rows containing \ in column V. The data I will be working with will have a variable number of rows so I cant specify the last row. I have found various codes but none of them seem to work. The current one I am working on is a mix of two. I have no experience of working with VBA so the simpler the better.

Sub HideRowsContainingContent()
    Dim ws As Worksheet
    Dim cell As Range
    Dim searchText As String
    Dim lastRow As Long
    
    ' Set the worksheet you want to work on
    Set ws = ThisWorkbook.Sheets("R filtered")
    
    ' Define the content to search for
    searchText = "\\"
    
    ' Find the last row with data in the worksheet
    lastRow = ws.Cells(ws.Rows.Count, "V").End(xlUp).Row
    
    ' Loop through each cell in the used range
    For Each cell In ws.Value = "\\" Then Each cell.EntireRow. Hidden = True
    Else
    For Each cell In ws.EntireRow. Hidden = False
    End If

Each of the For Each statements under the Loop through section, generated the compile error Expected: end of statement.

Thanks Bex

Share Improve this question edited 2 days ago Black cat 6,1774 gold badges24 silver badges50 bronze badges asked Feb 8 at 9:20 R PleasanceR Pleasance 11 bronze badge New contributor R Pleasance is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

The basic of your code is working, only the exact syntax should be applied

  • Quotes mean string type no need to escape
  • For Each loop needs Next at the end.
  • InStr function looks for a character in a string, and returns the position.
Sub HideRowsContainingContent()
    Dim ws As Worksheet
    Dim cell As Range
    Dim searchText As String
    Dim lastRow As Long
    
    ' Set the worksheet you want to work on
    Set ws = ThisWorkbook.Sheets("R filtered")
    ' Define the content to search for
    searchText = "\"    
    
    ' Find the last row with data in the worksheet
    lastRow = ws.Cells(ws.Rows.count, "V").End(xlUp).Row
    
    ' Loop through each cell in the used range
    'For Each cell In ws.Value = "\\" Then Each cell.EntireRow. Hidden = True
    'Else
    'For Each cell In ws.EntireRow. Hidden = False
    'End If
    For Each cell In ws.Range("V1:V" & lastRow)
        If InStr(cell, searchText) > 0 Then
            cell.EntireRow.Hidden = True
        Else
            cell.EntireRow.Hidden = False
        End If
    Next cell
End Sub

发布评论

评论列表(0)

  1. 暂无评论