Computing.Net > Forums > Office Software > Need help writing Excel Macro

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need help writing Excel Macro

Reply to Message Icon

Name: SHarner
Date: July 1, 2007 at 06:36:38 Pacific
OS: Windows XP
CPU/Ram: don't know
Product: Dell and HP
Comment:

Using Excel 2003 - I do not have any experience writing macros. Tried it on my own and now have a pop-up window everytime I open the excel file, even though I did not write the macro yet. 2 questions - 1. How do I get rid of that message if I don't have any macros written? 2. I need to write a macro to copy column A from 31 tabs in a file and then calculate the total number of entries by category. Catagory examples would be like, Home, Work, Office. I need to know the total times for all 31 tabs that the word home was entered in column A, how many times Work was entered in column A on all 31 tabs, etc. I know this is probably simple but the tutorials for writing macros is greek to me. I am a user not a programmer. Please help.


SHarner



Sponsored Link
Ads by Google

Response Number 1
Name: rhawk7938
Date: July 1, 2007 at 12:53:18 Pacific
Reply:

No, it's not simple. You would really need to learn some VBA programming to do what you want. If you want to try, the best way to start is to record some macros and then examine the code to understand how they work. Then learn to modify the generated code.

Simple code to find the word "Home" in a specified range(in this example A1 to A26) and record the total number of hits in cell A32, could look like this:

Sub searchword()
Dim myrange As Range
Set myrange = Range("A1:A26")
hits = 0
Range("A1").Select
For Each c In myrange
If c.Value = "Home" Then
numhits = numhits + 1
End If
Next
Range("A32") = "# of hits= " & numhits
End Sub


0

Response Number 2
Name: Coldpaws
Date: July 2, 2007 at 08:33:05 Pacific
Reply:

HI , You don't say what kind of pop up is happening so I'm afraid I can't offer a solution. However , you may want to try a simpler solution to your problem of counting the number of times a particular category occurs in all your 31 tabs, especially if VBA is not your thing.

You can use the 'find' command.
Control + F will bring up the dialogue box.
Type the word you want to find eg home
There is a tab on the bottom right that has the word 'Options'
Click to open some extra options
On the section marked 'Within' click the dropdown and select Workbook. This will now search all the tabs.
If you click 'Find All' Excel will do the search and tell you how many instances it found the word 'Home'.
Only drawback I can see is that it searches ALL the columns not just 'A' so if your word appears anywhere else it'll be counted.
You'll have to repeat the exercise for each word you want to count up .
Worth a shot?

If you're not totally confused , you don't fully understand the question.


0

Response Number 3
Name: DerbyDad03
Date: July 2, 2007 at 08:57:28 Pacific
Reply:

I am assuming that you are getting a dialog box telling you that the workbook contains macros. If you "tried" to write the macro, then you probably have at least one module saved in the VBA editor.

You have choices:
- Go into the VBA editor and look at the folder called Modules in the workbook you are interested in. Remove any modules you find there (e.g. Module1, etc). Do this at your own risk.

- In Excel, click on Tools/Macros/Security. Read the choices offered and pick the one that fits your needs. Be aware of what the Low setting says, but don't let it scare you. Low will allow macros to run without asking you, which could be dangerous if there is something built into a spreadsheet that you recieve from someone else. On the other hand, whatever virus protection software you are running on your machine should have already scanned the document and taken care of the bad code so it can't do it's damage. Again, use this info at your own risk.

Next: A note on posting macros in this forum: What should be one line may get split across more than one line when viewed in the post window. Copying and pasting the code into the VBA editor or into Word, should put it back the way it's supposed to be.

As far as the macro you are looking for there are many ways to achieve your goal. For example, to count words, you could use the looping code suggested by rhawk7938 above (with one minor correction - change the word hits to numhits) or you could use this one-liner and get the same results:

Sub CountWords()
[a32]="# of hits = " & (Application.WorksheetFunction.CountIf([a1:a26], "Home"))
End Sub

To copy Column A of each sheet to Column B of each sheet, the code might look something like this:

Sub CopyColumnA()
For NumShts = 1 To Sheets.Count
Sheets(NumShts).Columns("A").Copy Destination:=Sheets(NumShts).Range("B1")
Next
End Sub


Bottom lines:
1 - There are multiple ways to get the same end results in VBA.
2 - As rhawk7938 said, recording macros and modifying the code is a great way to learn, although the VBA recorder doesn't write the most efficient code, nor can it create a loop like rhawk7938 did. For example, the VBA recorder will write a line of code every time you Select a cell or range of cells while recording. In reality, you rarely, if ever, have to select cells in VBA to do something to/with them.
3 - Do a Google search on writing Excel VBA code for additional help.
4 - Take a look at this link (you might have to register to view it).

http://peach.ease.lsoft.com/scripts...

It's a brief tutorial written by myself and another to help people debug existing VBA code. By using the VBA recorder and the tips in the tutorial, you might be able to learn a little bit about VBA coding.

If you want some assitance in writung the code you need right now, let me know and I'll try to assist you outside of this forum.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Need help writing Excel Macro

Excel macro for line break, help! www.computing.net/answers/office/excel-macro-for-line-break-help/2377.html

Excel Macro Question www.computing.net/answers/office/excel-macro-question/2045.html

Need Help With Excel Macros www.computing.net/answers/office/need-help-with-excel-macros/8465.html