+ Reply to Thread
Results 1 to 4 of 4

Run Macro in selected sheet only

  1. #1
    Registered User
    Join Date
    07-12-2010
    Location
    Niceville, Florida
    MS-Off Ver
    Excel 2003
    Posts
    7

    Run Macro in selected sheet only

    --------------------------------------------------------------------------------

    Hello,

    I have a macro to generate random numbers and it updates when the sheet is recalcuted. Problem is it will run on wich ever sheet is active at the moment instead of its intended sheet. I added this code to it but it jumps to the sheet to run the macro when ever the sheet is recalculated. I need it to run automaticaly when the sheet is recalculated but with out jumping me to that particular worksheet.

    Code:

    With Worksheets("TL")
    .Select

    /Code

    Below is the entire macro

    Code:

    Option Explicit

    Sub TLtestgenerator()
    If you want unique random numbers, i.e. you want to shuffle the numbers 1 to 5'
    Dim i, choice, balls(1 To 5)
    Dim lngArr(1 To 5) As Long
    Dim RwNdx1 As Long
    Dim RwNdx2 As Long
    Dim ColNdx As Long
    Dim ColW As Long
    Dim lrow As Long
    Dim cnt1 As Long
    Dim cnt2 As Long
    Dim temp As Long
    Dim Rng As Range
    Dim ar As Range
    Dim Cell As Range
    With Worksheets("TL")
    .Select
    cnt1 = 1
    cnt2 = 5
    If cnt2 > 5 Then
    End If
    RwNdx1 = 3
    RwNdx2 = 7
    For ColNdx = 3 To 3
    Randomize
    For i = 1 To 5
    balls(i) = i
    Next
    For i = 1 To 5
    choice = 1 + Int((Rnd * (5 - i)))
    temp = balls(choice)
    balls(choice) = balls(6 - i)
    balls(6 - i) = temp
    Next
    i = 0
    With Cells(RwNdx1, ColNdx)
    End With
    Set Rng = Range(Cells(RwNdx1, ColNdx), Cells(RwNdx2, ColNdx))
    For Each ar In Rng
    For Each Cell In ar
    i = i + 1
    Cell.Value = balls(i)
    Next
    Next
    Next ColNdx
    End Sub

    /Code


    Thanks
    Adam

  2. #2
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Run Macro in selected sheet only

    Try turning off screen updating prior to your code:

    Application.ScreenUpdating = False

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Run Macro in selected sheet only

    What triggers this macro ?


    Please Login or Register  to view this content.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Run Macro in selected sheet only

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

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.6.0 RC 1