Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > Microsoft Support > Microsoft Office support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Microsoft Office support MS Office support forum

Reply
 
LinkBack Thread Tools
Old 09-12-2007, 10:12 AM   #1 (permalink)
Registered User
 
Join Date: Sep 2007
Posts: 2
OS: XP


Excel VBA - A couple of questions from newbie

I am trying to write a macro that cleans up some data that we are getting in. One of the things I am trying to do is split up some data in the cells. I have been inserting new columns and using them. Instead, I would like to find the last column of data. I found some code, but it returns a number and doesn't work in the Range() function.

Also, is there another way to loop through the cells other than using a select? I have been told that this will slow down the code, but I don't know how else to do it.

Any information would be greatly appreciated.

Thanks

PS - here is a sample of my loop using Select - There are a couple other If statements, but you get the idea.

Code:
Do Until x = 60
Range("F" & x).Select
        If InStr(ActiveCell.Value, "(") <> 0 Then
            ActiveCell.Offset(0, 1).Value = Mid(ActiveCell.Value, InStr(ActiveCell.Value, "(") + 1, Len(Trim(ActiveCell.Value)) - 1)
            ActiveCell.Offset(0, 1).Value = Left(ActiveCell.Offset(0, 1), InStr(ActiveCell.Offset(0, 1), ")") - 1)
            ActiveCell.Value = Left(ActiveCell.Value, InStr(ActiveCell.Value, "(") - 1)
        End If
x=x+1
Loop
bbrotherton is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 09-12-2007, 01:57 PM   #2 (permalink)
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
 
Glaswegian's Avatar
 
Join Date: Sep 2005
Location: Glasgow
Posts: 25,503
OS: Win XP Pro SP3 / Win 7 Pro

My System

Blog Entries: 10
Re: Excel VBA - A couple of questions from newbie

Hi and welcome to TSF.

You are correct about Select - you don't actually need to select cells to work with them. An although there's not much wrong with your looping, I prefer to set your range and then simply loop through the set range using a For Each...Next construct. Also assign variables - it's easier to code and quicker for Excel. Once it knows to what the variable has been assigned it will retain that information until the routine finishes. In your code, Excel has to work out to what ActiveCell refers - and it has to do that each time it finds such a reference. Here's my version of your code
Code:
Sub Testing()
Dim myRng As Range
Dim c As Range

Set myRng = Sheets("Sheet1").Range("F1:F60")

For Each c In myRng
    If InStr(c.Value, "(") <> 0 Then
        c.Offset(0, 1).Value = Mid(c.Value, InStr(c.Value, "(") + 1, Len(Trim(c.Value)) - 1)
        c.Offset(0, 1).Value = Left(c.Offset(0, 1), InStr(c.Offset(0, 1), ")") - 1)
        c.Value = Left(c.Value, InStr(c.Value, "(") - 1)
    End If
Next c
    
End Sub
Remember, in VBA there are many ways of doing the same thing!
__________________
Iain - Defender of the Haggis and all things Scottish.
I don't help by PM - post in the Forums.



PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner
Glaswegian is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-13-2007, 08:23 AM   #3 (permalink)
Registered User
 
Join Date: Sep 2007
Posts: 2
OS: XP


Re: Excel VBA - A couple of questions from newbie

I eventually did replace the counter with a variable. I know that is bad, but at the time, I hadn't figured out how to find the last used row. Now I know and replace the number with a variable. I also have a range now instead of using the select. I have whiddled it down to only one thing that I am having problems with logic wise.....I will figure it out!!

I am sure that there are different and possibly better ways to do my entire code, but I am pretty impressed with myself for figuring out so much on my first Excel programming day. :) I know how to program in Access, just not any other office programs.
bbrotherton is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-13-2007, 03:23 PM   #4 (permalink)
Moderator/ Rangemaster TSF Academy; Analyst, Security Team; Oor Wullie; TSF Surgeon and Resident Comic
 
Glaswegian's Avatar
 
Join Date: Sep 2005
Location: Glasgow
Posts: 25,503
OS: Win XP Pro SP3 / Win 7 Pro

My System

Blog Entries: 10
Re: Excel VBA - A couple of questions from newbie

Well if that was your first effort, then it was pretty good. Although the macro recorder produces much superfluous code, it can be useful for showing Objects and Properties that you can then use in written code.
__________________
Iain - Defender of the Haggis and all things Scottish.
I don't help by PM - post in the Forums.



PC Safety & Security::PC running a bit slow?::Donate::Photographers Corner
Glaswegian is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

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 Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 11:38 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85