+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    03-18-2010
    Location
    denmark
    MS-Off Ver
    Excel 2007
    Posts
    11

    More names for my VBA code

    Hi

    My code is almost working now ^^
    Now my problem is that i need more names in my code, and i'm unsure where i can change.
    Code:
     Private Sub Worksheet1_Change(ByVal Target As Range)
    Dim total As Integer
    total = Application.Sum(Sheets(1).Range("A14:I14"))
    
    If total = 227 Then
    Application.GoTo Sheets(1).Range("A17"), True
    End If
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim total As Integer
    total = Application.Sum(Sheets(1).Range("A15:I15"))
    
    If total = 179 Then
    Application.GoTo Sheets(1).Range("A17"), True
    End If
    End Sub
    it is this, excel says I shall change the name of.
    Code:
     Private Sub Worksheet_Change(ByVal Target As Range)
    As you can see in my first code i use
    Code:
    Worksheet_Change 
    and 
    Worksheet1_Change
    But when i use 2, it wont work. Is there another way to change to name so i can keep adding more codes like this?

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: More names for my VBA code

    Hi,

    No ... Event Macros cannot be replicated ...
    You have to make the effort to code all your requests into a single event module ...
    You need to adjust the ranges and use If or Select Case for many instances ...

    HTH

  3. #3
    Registered User
    Join Date
    03-18-2010
    Location
    denmark
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: More names for my VBA code

    Aww.. Would have been so easy if it was possible -.- oh well. Going to write some codes

    Thanks

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,225

    Re: More names for my VBA code

    Construct a single ws_change macro and organize the evaluation of the total variable into a select case statement...like so:
    Code:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Total As Long
    
    Total = Application.Sum(Sheets(1).Range("A15:I15"))
    
        Select Case Total
            Case 227:   Application.GoTo Sheets(1).Range("A17"), True
            Case 179:   Application.GoTo Sheets(1).Range("A17"), True
        End Select
    
    End Sub
    Last edited by shg; 03-19-2010 at 12:35 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,443

    Re: More names for my VBA code

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    Try using the correct terms, Names are not Events
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0