Consulting

Results 1 to 3 of 3

Thread: VBA To search for a WorksheetSheet and a Column in that sheet.

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location

    VBA To search for a WorksheetSheet and a Column in that sheet.

    Hi,
    I have an Excel workbook with about 10 different sheets and some sheet names have ACCT at the start or end of their name (For ex one sheet name is XYZ_ACCT and other sheet name is ACCT_ABC). In each of these ACCT sheets there is a column with a name ACCT_No. I would like to know if there is a way to search for the sheets whose name have ACCT in it (For ex one sheet name is XYZ_ACCT and other sheet name is ACCT_ABC) and then go to the Column ACCT_No in that sheet and count the number of cells which have values in that column.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Sub blah()
    Dim colmHeader As Range
    For Each sht In ActiveWorkbook.Sheets
      If InStr(1, sht.Name, "ACCT", vbTextCompare) > 0 Then
        Set colmHeader = sht.Rows(1).Find(What:="ACCT_No", LookIn:=xlFormulas, LookAt:=xlWhole, SearchFormat:=False)  'only searches row 1.
        'Set colmHeader = sht.Cells.Find(What:="ACCT_No", LookIn:=xlFormulas, LookAt:=xlWhole, SearchFormat:=False)'searches whole sheet.
        If Not colmHeader Is Nothing Then
          ColumnLetter = Split(Cells(1, colmHeader.Column).Address, "$")(1)
          MsgBox "Sheet " & sht.Name & " has a column with 'ACCT_No' in it and that column (" & ColumnLetter & ") has " & Application.WorksheetFunction.Count(colmHeader.EntireColumn) & " numbers in it."  'count cells with numbers in.
          'MsgBox "Sheet " & sht.Name & " has a column with 'ACCT_No' in it and that column (" & ColumnLetter & ") has " & Application.WorksheetFunction.CountA(colmHeader.EntireColumn) & " values in it (incl. the header itself)." 'counts cells which aren't empty.
        Else
          MsgBox "FYI:" & vbLf & "Sheet " & sht.Name & " has no column with 'ACCT_No' in it"
        End If
        Set colmHeader = Nothing
      End If
    Next sht
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    3
    Location

    It Works !!! Thanks a ton.

    Thanks a lot for the solution. It works !!! Although i need to change the code to make it run from a new workbook which will have a location for the workbook on which I need to run this code, I think I can try doing that. Thanks a to again. Really appreciate the help !!



    Quote Originally Posted by p45cal View Post
    Sub blah()
    Dim colmHeader As Range
    For Each sht In ActiveWorkbook.Sheets
      If InStr(1, sht.Name, "ACCT", vbTextCompare) > 0 Then
        Set colmHeader = sht.Rows(1).Find(What:="ACCT_No", LookIn:=xlFormulas, LookAt:=xlWhole, SearchFormat:=False)  'only searches row 1.
        'Set colmHeader = sht.Cells.Find(What:="ACCT_No", LookIn:=xlFormulas, LookAt:=xlWhole, SearchFormat:=False)'searches whole sheet.
        If Not colmHeader Is Nothing Then
          ColumnLetter = Split(Cells(1, colmHeader.Column).Address, "$")(1)
          MsgBox "Sheet " & sht.Name & " has a column with 'ACCT_No' in it and that column (" & ColumnLetter & ") has " & Application.WorksheetFunction.Count(colmHeader.EntireColumn) & " numbers in it."  'count cells with numbers in.
          'MsgBox "Sheet " & sht.Name & " has a column with 'ACCT_No' in it and that column (" & ColumnLetter & ") has " & Application.WorksheetFunction.CountA(colmHeader.EntireColumn) & " values in it (incl. the header itself)." 'counts cells which aren't empty.
        Else
          MsgBox "FYI:" & vbLf & "Sheet " & sht.Name & " has no column with 'ACCT_No' in it"
        End If
        Set colmHeader = Nothing
      End If
    Next sht
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •