Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Detection of VBA code

Hi,

I'm designing a tool to cycle through the sheets in a
workbook to detect if there is any code behind them. In
workbooks with a large number of sheets this can be quite
tricky to detect using the VBA editor.
Does anybody know how to do this...??

Rgds

Chris
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Detection of VBA code

See Chip Pearson's page on working in with code in the VBE:

http://www.cpearson.com/excel/vbe.htm

--
Regards,
Tom Ogilvy

Chris Gorham wrote in message
...
Hi,

I'm designing a tool to cycle through the sheets in a
workbook to detect if there is any code behind them. In
workbooks with a large number of sheets this can be quite
tricky to detect using the VBA editor.
Does anybody know how to do this...??

Rgds

Chris



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default Detection of VBA code

This is a macro I use for detecting "code" in an unknown workbook. Maybe
it'll be helpful.

''The purpose of this sub is to find if the active workbook has
''' "programming" in it like VB or XL4 or userforms, etc.
'' Say you've downloaded an XLS and opened it
'' with Shift down. Now you can look for modules in the VBE but you
'' have to check the Thisworkbook and Sheet modules to be sure.

Sub CodeInProject()
''XL2K+:
Dim VBComp As VBComponent, AllComp As VBComponents, ThisProj As
VBProject
''XL97 & XL2K:
''Dim VBComp As Object, AllComp As Object, ThisProj As Object
Dim WS As Worksheet, DLG As DialogSheet
Dim StdModCount As Integer, StdModWithCodeCount As Integer
Dim ClassModCount As Integer
Dim FormCount As Integer, SheetMods As Integer
Dim CodeBehindStr As String
Dim DlgSheetCount As Integer, XL4Sheets As Integer
Dim MsgStr As String
On Error GoTo ProjProt
Set ThisProj = ActiveWorkbook.VBProject

Set AllComp = ThisProj.VBComponents
For Each VBComp In AllComp
With VBComp
Select Case .Type
Case vbext_ct_StdModule
StdModCount = StdModCount + 1
If HasProc(VBComp) Then _
StdModWithCodeCount = StdModWithCodeCount + 1
Case vbext_ct_ClassModule
ClassModCount = ClassModCount + 1
Case vbext_ct_MSForm
FormCount = FormCount + 1
Case vbext_ct_Document
If HasProc(VBComp) Then
CodeBehindStr = CodeBehindStr & VBComp.Name &
Chr(10) & Chr(9)
End If
End Select
End With
Next
If CodeBehindStr < "" Then CodeBehindStr = Left(CodeBehindStr,
Len(CodeBehindStr) - 2)
XL4Sheets = Excel4MacroSheets.Count
DlgSheetCount = DialogSheets.Count
If CodeBehindStr < "" Then
MsgStr = "Document modules with code: " & Chr(10) & Chr(9) &
CodeBehindStr & Chr(10)
End If
If StdModCount 0 Then
MsgStr = MsgStr & StdModCount & " standard modules (" &
StdModWithCodeCount & " with code)." & Chr(10)
End If
If ClassModCount 0 Then
MsgStr = MsgStr & ClassModCount & " class module(s)." & Chr(10)
End If
If FormCount 0 Then
MsgStr = MsgStr & FormCount & " user form(s)." & Chr(10)
End If
If XL4Sheets 0 Then
MsgStr = MsgStr & XL4Sheets & " XL4 macro sheet(s)." & Chr(10)
End If
If DlgSheetCount 0 Then
MsgStr = MsgStr & DlgSheetCount & " dialog sheet(s)." & Chr(10)
End If
If MsgStr = "" Then MsgStr = "No code evident."
MsgBox MsgStr
Exit Sub
ProjProt:
MsgBox "Project projected"
End Sub

Function HasProc(Comp As VBComponent) As Boolean
Dim Counter As Integer, Txt As String
With Comp.CodeModule
For Counter = 1 To .CountOfLines
If .ProcOfLine(Counter, vbext_pk_Proc) < "" Then
HasProc = True
Exit Function
End If
Next
End With
End Function


--
Jim Rech
Excel MVP


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
key detection in user forms AlanGriffithKapitiNZ Excel Discussion (Misc queries) 0 June 22nd 09 05:13 PM
Directory and File Detection Varne Excel Discussion (Misc queries) 3 November 18th 08 11:46 AM
Auto range of numbers detection Aaron Excel Worksheet Functions 7 February 26th 07 12:09 AM
cannot disable hyperlinks auto-detection joni_piter Excel Discussion (Misc queries) 2 May 5th 06 06:52 PM
Color detection Dan wilson Excel Worksheet Functions 3 March 31st 05 03:51 PM


All times are GMT +1. The time now is 10:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"