Consulting

Results 1 to 2 of 2

Thread: sheets exist

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    sheets exist

    hello
    sorry for asking again.
    i want to check if a sheet name exist and then generate a list that will count all lines of code in all mudules.if the sheet is missing then add sheet.
    i got stuck an lost
    [VBA]
    Sub showcomponents()
    Dim ws As Worksheet
    For Each ws In Worksheets
    If Not SheetExists("components") Then
    Worksheets.Add.Name = "components"
    End If
    Next ws
    With Sheets("components")
    Cells.HorizontalAlignment = xlRight
    Cells.ClearContents
    Dim vbp As VBProject
    Dim numcomponents As Integer
    Dim i As Integer
    Set vbp = ActiveWorkbook.VBProject
    numcomponents = vbp.VBComponents.Count
    Application.ScreenUpdating = False
    On Error Resume Next
    End With
    For i = 1 To numcomponents
    Cells(i, 1) = vbp.VBComponents(i).Name
    Select Case vbp.VBComponents(i).Type
    Case 1
    Cells(i, 2) = "module"
    Case 2
    Cells(i, 2) = "class module"
    Case 3
    Cells(i, 2) = "userform"
    Case 100
    Cells(i, 2) = "document module"
    End Select
    Cells(i, 3) = _
    vbp.VBComponents(i).CodeModule.CountOfLines
    Next i
    End Sub

    [/VBA]
    thanks
    moshe

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Well apart from that fact that you don't need tgo loope through all the sheets to check if components exists, a single check suffices, it seems fine to me. That point I made won't actually make any difference, it is just wasteful, as once created it fails the test next time.

Posting Permissions

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