Consulting

Results 1 to 9 of 9

Thread: Comments Off

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location

    Question Comments Off

    Is there a way to run a script that takes all the comments in my excel vba modules off?

    I mean both. The comments alone in a row as well as the comments in the same row as code.

    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Do you want to delete the comments or just remove the ' ?

  3. #3
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Not just remove the ', because this would lead to an error as the comment would become part of the code. I want to delete all the comments.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Ok, try this macro:

    Option Explicit
     
     Sub Macro1()
    Dim n               As Long
     Dim i               As Long
     Dim j               As Long
     Dim k               As Long
     Dim LineText        As String
     Dim ExitString      As String
    ExitString = "Ignore Comments In This Module"
    For i = 1 To ActiveWorkbook.VBProject.VBComponents.Count
             With ActiveWorkbook.VBProject.VBComponents(i).CodeModule
                 For j = 1 To .CountOfLines
                     LineText = Trim(.Lines(j, 1))
                     If LineText = "ExitString = " & _
                         """" & "Ignore Comments In This Module" & """" Then
                         Exit For
                     End If
                     n = InStr(1, LineText, "'")
                     Select Case n
                         Case Is = 0
                             'No Comment
                         Case Is = 1
                             .ReplaceLine j, ""
                         Case Is > 1
                         .ReplaceLine j, Left(LineText, n - 1)
                     End Select
                 Next j
             End With
         Next i
    End Sub

  5. #5
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Gee man, you ARE great.



    I will test it too and let you know.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Sounds good. Let me know if there are any problems.

    Take Care

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Here is a variation if you want to completely delete a line if it is all comments instead of leaving a blank line.

    Option Explicit
      
     Sub Macro1()
    Dim n               As Long
         Dim i               As Long
         Dim j               As Long
         Dim k               As Long
         Dim LineText        As String
         Dim ExitString      As String
    For i = 1 To ActiveWorkbook.VBProject.VBComponents.Count
             With ActiveWorkbook.VBProject.VBComponents(i).CodeModule
                 For j = .CountOfLines To 1 Step -1
                     LineText = Trim(.Lines(j, 1))
                     If LineText = "ExitString = " & _
                     """" & "Ignore Comments In This Module" & """" Then
                     Exit For
                 End If
                 n = InStr(1, LineText, "'")
                 Select Case n
                 Case Is = 0
                 Case Is = 1
                     .DeleteLines j, 1
                 Case Is > 1
                     .ReplaceLine j, Left(LineText, n - 1)
                 End Select
             Next j
         End With
     Next i
    ExitString = "Ignore Comments In This Module"
    End Sub

  8. #8
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Gee DRJ,

    are you reading my mind?? I was trying to do that, .

    Many thanks again.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Yeah I figured that you would probably want to actually delete the lines.

    Take Care

Posting Permissions

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