+ Reply to Thread
Results 1 to 5 of 5

How to link codes for two command buttons

Hybrid View

  1. #1
    Registered User
    Join Date
    04-24-2014
    Location
    Swindon
    MS-Off Ver
    Excel 2010
    Posts
    2

    How to link codes for two command buttons

    I have created a form and has two command buttons, Say A and B.

    Command button A uses random number generator to select a cell from a range in the spread sheet which is then used in vlookup function to return a value from column number 2 of a record in text box1.

    I would like the Command button B to return the value from column number 3 for the same record in text box2.

    Kindly advise how can I do it.

    The code for Command button A looks like:

    Private Sub CommandButton3_Click()

    Dim LRandomNumber As Integer
    Dim Meaning As String
    Dim Word As String

    LRandomNumber = Int((50 - 1 + 1) * Rnd + 1)

    TextBox1.Value = Application.WorksheetFunction.VLookup(LRandomNumber, Sheet1.Range("A1:B51"), 2, True)

    End Sub
    Last edited by sg2808; 04-24-2014 at 10:25 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: How to link codes for two command buttons

    TextBox1.Value = Application.WorksheetFunction.VLookup(LRandomNumber, Sheet1.Range("A1:B51"), 3, True)
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    04-24-2014
    Location
    Swindon
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: How to link codes for two command buttons

    Thanks nathansav. see my edited post again, i want the button B to give me the output in text box B.

    Just changing the above code to TextBox2.value... does not help as the LRandom is in the different sub function(). I have tried this and it didnt work.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: How to link codes for two command buttons

    put the declaration of LRandomNumber at the very top of your vba like

    Private LRandomNumber as integer

    It will be available to all subs in the module then.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to link codes for two command buttons

    Why 2 subs/buttons?
    Private Sub CommandButton3_Click()
    Dim LRandomNumber As Integer
    Dim Meaning As String
    Dim Word As String
    
        LRandomNumber = Int((50 - 1 + 1) * Rnd + 1)
    
        TextBox1.Value = Application.WorksheetFunction.VLookup(LRandomNumber, Sheet1.Range("A1:C51"), 2, True)
    
        TextBox2.Value = Application.WorksheetFunction.VLookup(LRandomNumber, Sheet1.Range("A1:C51"), 2, True)
    
    End Sub
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 02-05-2014, 12:34 AM
  2. Useform Code - 12 Command Buttons dependant on other command buttons
    By darrenkaye in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2013, 10:58 AM
  3. Master Command Button to call on other Command Buttons located in other Workbooks
    By MrNickRegan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2013, 07:57 PM
  4. Code for a master command button to change the backcolor of multiple command buttons?
    By panttherm5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2012, 10:11 PM
  5. [SOLVED] Change listbox link cell with command buttons
    By zplugger in forum Excel General
    Replies: 1
    Last Post: 08-12-2012, 06:43 PM

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