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 Feb 28th, 2002, 05:57 AM   #1
Andrew XJ
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

I need to write Macro to a series of files using a Macro. Is this possible? Please enlighten.
I appreciate any suggestions

XJ
Andrew XJ is offline   Reply With Quote
Old Feb 28th, 2002, 07:41 AM   #2
Guest
 
Posts: n/a
Default

Quote:
On 2002-02-27 23:57, Andrew XJ wrote:
I need to write Macro to a series of files using a Macro. Is this possible? Please enlighten.
I appreciate any suggestions

XJ
Yes this is possible.....you can do a number
of things;
What are you trying to do ?
You can add to routines, copy modules, copy useforms etc, change Application events etc......

Would suggest that you have your routine in
a seperate module then through code export this out, open the files you want this code in and import the module in.
Probably best to keep / transfer the files to change in a seperate Dir (AND BACK them up JIC).


Let me know.


Ivan
  Reply With Quote
Old Feb 28th, 2002, 07:51 AM   #3
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,208
Default

didn't mean to post Anon ??

ivan
Ivan F Moala is offline   Reply With Quote
Old Feb 28th, 2002, 12:38 PM   #4
Andrew XJ
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

In fact i need to Modify a series of files and add some codes in the Auto_open() subroutine of each file. And another task is to Modify the codes according to the file name and path.
Could you provide some simple samples?

[ This Message was edited by: Andrew XJ on 2002-02-28 06:42 ]
Andrew XJ is offline   Reply With Quote
Old Feb 28th, 2002, 02:09 PM   #5
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,639
Default

Here's one I did a while back:

I called the following from my code (application.run("CopyOneModule"). In this case I'm taking module 3 and putting it in the new files. From what I understand, you have to export the module to text and import the module in your new workbook.

Private Sub CopyOneModule()

Dim FName2 As String
Dim FName3 As String
With Workbooks("Tool.xls")'name your workbook
FName2 = .path & "code.txt"
If FName2 <> "" Then
On Error Resume Next
Kill FName2
End If
.VBProject.VBComponents("Module3").Export FName2
End With
FName3 = "Target Workbook Name.xls"
Workbooks(FName3).VBProject.VBComponents.Import FName2
Kill FName2
End Sub


And in module 3, I put the following procedure. All contents of module 3 should be in your target workbook.


Option Explicit

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Sub auto_open()
'Macro hand-crafted by Nate Oliver on 1/29/2002
Dim sUser As String
Dim lpBuff As String * 1024
GetUserName lpBuff, Len(lpBuff)
sUser = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
lpBuff = ""
On Error Resume Next
If ActiveWorkbook.Name = "Tool.xls" Then
Else:
If sUser <> "" Then
MsgBox prompt:="Welcome " & WorksheetFunction.Proper(sUser) & " to the new tool.", _
Title:="Welcome!"
Else: MsgBox prompt:="Welcome to the new tool", _
Title:="Welcome!"
End If
End If

End Sub

Modifying code gets tricky. But, check out the following, Chip does a nice job.

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

Hope this helps. Cheers,

Nate


[ This Message was edited by: NateO on 2002-02-28 08:26 ]
NateO is offline   Reply With Quote
Old Feb 28th, 2002, 03:01 PM   #6
Andrew XJ
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

Andrew XJ is offline   Reply With Quote
Old Feb 28th, 2002, 03:10 PM   #7
Andrew XJ
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

Thank you! Nate, i am sorry that i just step into Macro for one month. I am digesting second part of you codes. From my understanding. Else: is just a label named "Else". Is "Else:" just a typing error?
Your codes and the suggested website is very nice .
May i didn't make my questions clear. I need to change parts of Macro of files. In one subroutine, maybe just some codes need to be modified. I feel it is almost impossible. It is using Macro to edit codes(text,macro)!!
Andrew XJ is offline   Reply With Quote
Old Feb 28th, 2002, 03:16 PM   #8
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,639
Default

Andrew, the Else: is part of a vba "IF" statement.

If &*&*&*& then
'your code
else: 'your code
end if

If you look at Chip's site, you can see the parts regarding adding or deleting procedures. Personally, I think it's messy to delete certain lines, etc...I'd prefer to delete the replace the whole thing with code that works. Just my opinion. Hope this helps.

Cheers, Nate
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO 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 +1. The time now is 08:47 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 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