Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Detecting Macro code behind a sheet

Hi,

I'm writing a macro that cycles through all the sheets in
another workbook detecting if there is any code behind
them. Useful if you want to quickly find out if someones
slipped in some code in a 20+ sheet model and don't fancy
clicking on each one in the VB Editor to find out...only
way I know!!

Anyway the code below is kicking out errors - starting
with user-type not defined...

This may involve making changes to the references section
in the VB Editor, but I want a solution that will work for
anyone..

Dim VBComp As Object
Dim VBCodeMod As CodeModule

For intcount = 1 To ActiveWorkbook.Sheets.count

sht_name = Sheets(intcount).Name
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents
(sht_name).CodeModule
If VBCodeMod.CountOfLines 0 Then
macro = "True"
Else
macro = ""
End If

Next intcount
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Detecting Macro code behind a sheet

Chris,

Try this:

Sub testit()
Const vbext_ct_Document = 100
Dim vbc As Object, blnFound As Boolean

blnFound = False
For Each vbc In ActiveWorkbook.VBProject.VBComponents
If vbc.Type = vbext_ct_Document Then
If vbc.CodeModule.CountOfLines -
vbc.CodeModule.CountOfDeclarationLines 0 Then blnFound = True
End If
Next

MsgBox IIf(blnFound, "Macros", "No Macros")
End Sub

Rob

"Chris Gorham" wrote in message
...
Hi,

I'm writing a macro that cycles through all the sheets in
another workbook detecting if there is any code behind
them. Useful if you want to quickly find out if someones
slipped in some code in a 20+ sheet model and don't fancy
clicking on each one in the VB Editor to find out...only
way I know!!

Anyway the code below is kicking out errors - starting
with user-type not defined...

This may involve making changes to the references section
in the VB Editor, but I want a solution that will work for
anyone..

Dim VBComp As Object
Dim VBCodeMod As CodeModule

For intcount = 1 To ActiveWorkbook.Sheets.count

sht_name = Sheets(intcount).Name
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents
(sht_name).CodeModule
If VBCodeMod.CountOfLines 0 Then
macro = "True"
Else
macro = ""
End If

Next intcount



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Detecting Macro code behind a sheet

Hi Chris,

Here's a bit better approach to the problem. All of this type of code
requires a reference to the Visual Basic for Applications Extensibility
object library. This object library is forward compatible, but not backward
compatible, so you need to compile and save this in the earliest version of
Excel that you plan to have it run in. If the user is running Excel 2002 or
higher, they must also check the Trust Access to Visual Basic Project
checkbox under the Tools/Macro/Security/Trusted Sources menu.

Sub CheckForDocObjectCode()
Dim objComponent As VBIDE.VBComponent
For Each objComponent In ActiveWorkbook.VBProject.VBComponents
If objComponent.Type = vbext_ct_Document Then
If objComponent.CodeModule.CountOfLines 0 Then
MsgBox objComponent.Name & " has code."
Else
MsgBox objComponent.Name & " does not have code."
End If
End If
Next objComponent
End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"Chris Gorham" wrote in message
...
Hi,

I'm writing a macro that cycles through all the sheets in
another workbook detecting if there is any code behind
them. Useful if you want to quickly find out if someones
slipped in some code in a 20+ sheet model and don't fancy
clicking on each one in the VB Editor to find out...only
way I know!!

Anyway the code below is kicking out errors - starting
with user-type not defined...

This may involve making changes to the references section
in the VB Editor, but I want a solution that will work for
anyone..

Dim VBComp As Object
Dim VBCodeMod As CodeModule

For intcount = 1 To ActiveWorkbook.Sheets.count

sht_name = Sheets(intcount).Name
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents
(sht_name).CodeModule
If VBCodeMod.CountOfLines 0 Then
macro = "True"
Else
macro = ""
End If

Next intcount



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Detecting Macro code behind a sheet

When you're in the VBE, do Tools|references and check:
Microsoft visual basic for Applications Extensibility x.x

Then those types will become available.

On the other hand, you can use a generic object variable:

Option Explicit
Sub testme()

'Dim VBComp as Object ' As VBComponent 'but you didn't use this
Dim VBCodeMod As Object 'As CodeModule
Dim intCount As Long
Dim Sht_name As String
Dim macro As String

For intCount = 1 To ActiveWorkbook.Sheets.Count

Sht_name = Sheets(intCount).CodeName
Set VBCodeMod _
= ActiveWorkbook.VBProject.VBComponents(Sht_name).Co deModule
If VBCodeMod.CountOfLines 0 Then
macro = "True"
Else
macro = ""
End If
Next intCount
End Sub

It's nicer to develop with the reference--you'll get all the intellisense help.
But right before you turn it over to users, get rid of the reference and go back
to generic Objects.

You won't have to worry about having a different version of the library that you
referred to.

ps. note that I changed what sht_name was, too.

And you may want to read more about this type of code from Chip Pearson's web
site:
http://www.cpearson.com/excel/vbe.htm

And watch out. I have "Tools|options|Editor tab, Require variable declaration"
checked.

Even if my module didn't have any real code behind it, I got 2 for the
..countoflines.



Chris Gorham wrote:

Hi,

I'm writing a macro that cycles through all the sheets in
another workbook detecting if there is any code behind
them. Useful if you want to quickly find out if someones
slipped in some code in a 20+ sheet model and don't fancy
clicking on each one in the VB Editor to find out...only
way I know!!

Anyway the code below is kicking out errors - starting
with user-type not defined...

This may involve making changes to the references section
in the VB Editor, but I want a solution that will work for
anyone..

Dim VBComp As Object
Dim VBCodeMod As CodeModule

For intcount = 1 To ActiveWorkbook.Sheets.count

sht_name = Sheets(intcount).Name
Set VBCodeMod = ActiveWorkbook.VBProject.VBComponents
(sht_name).CodeModule
If VBCodeMod.CountOfLines 0 Then
macro = "True"
Else
macro = ""
End If

Next intcount


--

Dave Peterson

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
Macro Copy Excel Chart Sheet to PowePoint - Mod of Jon Peltier code Frank Hayes[_2_] Charts and Charting in Excel 6 January 22nd 08 12:26 AM
Code to protect/unprotect a sheet using a macro with password FredH Excel Discussion (Misc queries) 5 October 23rd 07 04:49 PM
Detecting Duplicate Sums - Worksheet code [email protected] Excel Discussion (Misc queries) 0 September 27th 07 02:20 AM
is there anyway to make it so the users of my excel spread sheet cant view the macro code w/o a password? Daniel Excel Worksheet Functions 2 June 28th 05 05:34 AM
Detecting VBA code Chris Gorham[_3_] Excel Programming 1 November 1st 03 08:34 PM


All times are GMT +1. The time now is 06:32 AM.

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

About Us

"It's about Microsoft Excel"