+ Reply to Thread
Results 1 to 10 of 10

Button macro questions

  1. #1
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Button macro questions

    Hi!
    Im EXTREMLY newbie at this so let me try and explain what im trying to do:

    Thing nr 1:
    • Where i work there are alot of places to eat.
    • I have created a list of all places
    • I want to do a button that, when i press it, it chooses 1 of the places randomly (the name of the place) and puts that in a cell.

    What i have done:
    I have made a =index function (in that cell) that randomly picks one in index, and a button that refreshes -> which randomly picks a place (works). However, due to other functions in my worksheet i can not use this "refresh" function since it ruins the rest.

    What i want:
    A button that checks the "index" (list of places) - randomly selects one, and puts that name in a cell (without any refresh function). In other words: Nothing in the cell where i want the place to pop up, but with a button that does the "randomselect" thing for me and puts one in the cell.
    PS: The Index of places is in another sheet and the range is from b3:b40

    Thing nr 2:

    I have created another button (copy paste from a website) that selects a persons email, writes the subject, etc. However: I want it also to select a couple of cells from another sheet and put the content of it in the body.

    Ill try to show the script first. Hope i can =/

    Please Login or Register  to view this content.
    Now where it sais ".Body = (rng)" i simply what that "(rng)" to be a certain sheet between a "A2 and H16". and then to use that contant (within those cells) as text.

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

    Please notice that im EXTREMLY new at this, so i wont understand much of text. I just wonder if any kind soul can help with these 2 buttons.

    Kind regards,
    Doite
    Last edited by Baoli; 03-05-2012 at 01:16 PM.

  2. #2
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    anyone? =(

  3. #3
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    Ah! I solved the email question, that is "thing 2". Now all i wonder is the random cell index thing

  4. #4
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Button macro questions

    RandomSelectionFromList.xlsm

    This may help get started ..... Be sure to enable macros and the code is in Module1


    The example will randomly choose from a list a places (defined as a range -- "MyList") and place the result in cell "A1" each time you press the button.

  5. #5
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    Thx mate =) Ill try it out if i ever get some more spare time away from my original duties.

  6. #6
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    Mate!
    I tried
    Worked on yours but when i ... remade it alittle it doesnt

    Sub RandomPlace_Click()
    Dim ListCount As Integer, num
    Dim RandomNumber As Integer
    Dim rng As Range

    Set rng = Sheets("Rådata-Restaurang").Range("B3:B20")
    ListCount = rng.Count
    RandomNumber = Int(ListCount * Rnd + 1)
    ActiveSheet.Range("A1").Select
    ActiveSheet.Range("A1").Value = ActiveCell.Offset(RandomNumber - 1, 0).Value
    End Sub

    What i changed was the rng. from activesheet.range("mylist") to sheets("blablabal").range("a2:b20")

    It doesnt give me an error, but nothing happends. It just selects "A1" in the sheet where i have the button, but doesnt place anything there

  7. #7
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    Now it starts picking stuff in the sheet where the button is

    I redid the code so its:

    Sub RandomPlace_Click()
    Dim ListCount As Integer, num
    Dim RandomNumber As Integer
    Dim rng As Range

    Set rng = Sheets("Rådata - Restaurang").Range("B3:B20")
    ListCount = rng.Count
    RandomNumber = Int(ListCount * Rnd + 1)
    ActiveSheet.Range("B11").Select
    ActiveSheet.Range("B11").Value = ActiveCell.Offset(RandomNumber - 1, 0).Value
    End Sub

    But as u se, i want to go to "sheets("rådata - restaurang") first, then range ("blabal").
    Not the range in this sheet

    Any fast fix?

  8. #8
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    anyone =( Sorry i ask, but.. i cant figure it out.

  9. #9
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Button macro questions

    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
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  10. #10
    Registered User
    Join Date
    02-22-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Button macro questions

    SOLVED - see post "[SOLVED] VBA button - reference to another sheet "

+ 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