Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default modules removing problem

Hello! I want to remove about 30 modules in secend opened Workbook from
first opend Workbook -the instance excel.
This first workbook it is scheduler, wich generated periodically report from
model. This model contains about 30 modules, witch i have to remove before
report save.

In first workbook I running:

Dim objVbc As VBComponent
Set wb2 = workbooks (xxxxx)

For Each objVbc In Workbooks(wb2).VBProject.VBComponents
If objVbc.Type 0 And objVbc.Type < 4 Then
Workbooks(ActiveWorkbook.Name).VBProject.VBCompone nts.Remove
objVbc
Else
objVbc.CodeModule.DeleteLines StartLine:=1,
Count:=objVbc.CodeModule.CountOfLines
End If
Next objVbc

Unfortunately, this code does not work, but when I break the VBA code (after
this above code part ) all modules disappear and is OK!
Is it problem with refresh? What should I do? Help me , please! Thanx



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default modules removing problem

The first flag I see raised is

Workbooks(ActiveWorkbook.Name).

Are you really sure you want to remove modules from the
ActiveWorkbook? A better solution is to set a reference to the
workbook whose modules you want to remove and always use that
reference. E.g.,

Dim DeleteFromWB As Workbook
Dim VBComp As VBIDE.VBComponent

Set DeleteFromWB = Workbooks("Book3.xls")
For Each VBComp In DeleteFromWB.VBProject.VBComponents
Select Case VBComp.Type
Case vbext_ct_ActiveXDesigner, vbext_ct_ClassModule, _
vbext_ct_MSForm, vbext_ct_StdModule
DeleteFromWB.VBProject.VBComponents.Remove VBComp
Case Else
With VBComp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next VBComp

In this code, DeleteFromWB is the workbook containing the modules you
want to delete. It doesn't matter what workbook is active and what
workbook contains the code. The code will always be deleted from
DeleteFromWB.

See www.cpearson.com/Excel/VBE.aspx for lots more info about working
with the VBA objects.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Thu, 20 Aug 2009 19:32:49 +0200, "Adax" wrote:

Hello! I want to remove about 30 modules in secend opened Workbook from
first opend Workbook -the instance excel.
This first workbook it is scheduler, wich generated periodically report from
model. This model contains about 30 modules, witch i have to remove before
report save.

In first workbook I running:

Dim objVbc As VBComponent
Set wb2 = workbooks (xxxxx)

For Each objVbc In Workbooks(wb2).VBProject.VBComponents
If objVbc.Type 0 And objVbc.Type < 4 Then
Workbooks(ActiveWorkbook.Name).VBProject.VBCompone nts.Remove
objVbc
Else
objVbc.CodeModule.DeleteLines StartLine:=1,
Count:=objVbc.CodeModule.CountOfLines
End If
Next objVbc

Unfortunately, this code does not work, but when I break the VBA code (after
this above code part ) all modules disappear and is OK!
Is it problem with refresh? What should I do? Help me , please! Thanx


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default modules removing problem

Uzytkownik "Chip Pearson" napisal w wiadomosci
...
The first flag I see raised is
Workbooks(ActiveWorkbook.Name).
Are you really sure you want to remove modules from the
ActiveWorkbook? A better solution is to set a reference to the
workbook whose modules you want to remove and always use that
reference. E.g.,
Dim DeleteFromWB As Workbook
Dim VBComp As VBIDE.VBComponent
Set DeleteFromWB = Workbooks("Book3.xls")
For Each VBComp In DeleteFromWB.VBProject.VBComponents
Select Case VBComp.Type
Case vbext_ct_ActiveXDesigner, vbext_ct_ClassModule, _
vbext_ct_MSForm, vbext_ct_StdModule
DeleteFromWB.VBProject.VBComponents.Remove VBComp
Case Else
With VBComp.CodeModule
.DeleteLines 1, .CountOfLines
End With
End Select
Next VBComp


Thank You very much! but unfortunatelyit dosn't work tooo! :) This problem
I try solve since several months and without result.
Your and my code works when I start this code from empty workbook.
When I add it to my scheduler with many modules I noticed importand
observation: when I breake code after this removing code, immediately all
modules disappears.... Problem with collection refresh? I see, missing
collection refresh...



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
Removing modules and forms Daniel Bonallack Excel Programming 6 December 17th 08 11:49 PM
Excel cores when trying to close a workbook after exporting+removing VBA modules Tom Med Excel Programming 0 September 19th 07 07:00 PM
Removing modules via VBA ExcelMonkey Excel Programming 3 March 21st 07 03:50 PM
Removing VB Modules Automatically JNaugh Excel Programming 1 March 15th 06 07:00 PM
Problem with Userform and different code modules Jos Vens[_2_] Excel Programming 1 February 23rd 05 09:00 PM


All times are GMT +1. The time now is 06:45 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"