MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 15th, 2002, 03:52 AM   #1
garyrpollitt
New Member
 
Join Date: May 2002
Location: England UK
Posts: 10
Default

How do I delete a macro in VBA.(or a module)
I run a macro that imports a txt file into Excel then saves it as another name, but I do not want it to save the macro.I need the macro deleted before saving.
Gary

[ This Message was edited by: garyrpollitt on 2002-05-15 02:53 ]
garyrpollitt is offline   Reply With Quote
Old May 15th, 2002, 03:54 AM   #2
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Rather than delete your code, use Workbooks.Add to create a new workbook and import your data into that.
Mudface is offline   Reply With Quote
Old May 15th, 2002, 05:34 AM   #3
garyrpollitt
New Member
 
Join Date: May 2002
Location: England UK
Posts: 10
Default

Cheers Mudface
But that means me creating a new macro in the new workbook to import the data. I only want one macro on a spreadsheet and that is in "Test1".
Basically "Test1" imports txt data, creates a graph then saves it as a eg "122340".Test1 closes keeping the original macro with nothing on its sheets. But 122340 now also has the macro.
Hope you understand!!
Gary
As soon as Test1 is saved as 122340 then 122340 gets the focus and I want to delete the Macro on 122340
garyrpollitt is offline   Reply With Quote
Old May 15th, 2002, 05:54 AM   #4
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-05-15 04:34, garyrpollitt wrote:
Cheers Mudface
But that means me creating a new macro in the new workbook to import the data. I only want one macro on a spreadsheet and that is in "Test1".
Basically "Test1" imports txt data, creates a graph then saves it as a eg "122340".Test1 closes keeping the original macro with nothing on its sheets. But 122340 now also has the macro.
Hope you understand!!
Gary
As soon as Test1 is saved as 122340 then 122340 gets the focus and I want to delete the Macro on 122340

Hi Gary
You could try this code.
BUT be warned...Backup your Files...this
macro will delete ALL Code and Rferences.

Suggest you Save Copies and test extensively.

Place code in Std Module and call it
when you need. You may need to put in Flags
of some sought to ensure that it doesn't get
called EXCEPT when you want it too.


Sub RemoveAllCode()
On Error Resume Next
'Make reference to Extensibilty Library
ThisWorkbook.VBProject.References.AddFromGuid _
"{0002E157-0000-0000-C000-000000000046}", 4, 0
On Error GoTo 0

Dim VBComp As Object, AllComp As Object, ThisProj As Object
Dim ThisRef As Reference, WS As Worksheet, Dlg As DialogSheet

Set ThisProj = ActiveWorkbook.VBProject
Set AllComp = ThisProj.VBComponents
For Each VBComp In AllComp
With VBComp
Select Case .Type
Case 1, 2, 3
AllComp.Remove VBComp
Case vbext_ct_Document
.CodeModule.DeleteLines 1, .CodeModule.CountOfLines
End Select
End With
Next

For Each ThisRef In ThisProj.References
If Not ThisRef.BuiltIn Then ThisProj.References.Remove ThisRef
Next

Application.DisplayAlerts = False
For Each WS In Excel4MacroSheets
WS.Delete
Next
For Each Dlg In DialogSheets
Dlg.Delete
Next

End Sub



__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 15th, 2002, 05:57 AM   #5
Mudface
MrExcel MVP
 
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
Default

Well, you're basically doing the same thing, but instead of using the macro to fill the workbook containing the macro with data, you would create a new workbook, fill that with your data and then save it.

You do this by using references to your new workbook in the macro, rather than references to the current one. As an example, try out the following two procedures, the first fills sheet 1 of the workbook containing the macro with data: -

Public Sub FillCurrentWorkbook()
Dim x As Integer

For x = 1 To 100
ThisWorkbook.Sheets(1).Range("A" & x).Value = x
Next x

End Sub

The second adds a new workbook and fills it with data: -

Public Sub FillNewWorkbook()
Dim x As Integer
Dim mycount As Integer

Workbooks.Add
' Find out the index of the new workbook so we can refer to it
' You could also do this with ActiveWorkbook, but this seems better

mycount = Workbooks.Count

For x = 1 To 100
Workbooks(mycount).Sheets(1).Range("A" & x).Value = x
Next x

End Sub

Do you see what I mean? You just need to rewrite your code to do everything to the newly cretaed workbook.
Mudface is offline   Reply With Quote
Old May 15th, 2002, 12:01 PM   #6
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

Hi Ivan –

Do you know if this will work on stealth, (hope you twig me here)

As useful!

__________________
Free Excel based Web Toolbar available here.

Jack in the UK
J & R Excel Solutions
"making Excel work for you"
Jack in the UK is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 09:32 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes