+ Reply to Thread
Results 1 to 11 of 11

userform command button to run code

  1. #1
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    userform command button to run code

    please see attached sheet, I need the code to find and total matching records in column T based on the selections made on the 2 comboboxes

    combobox1 = week no
    combobox2 = shift

    all help most appreciated
    scouse13
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: userform command button to run code

    Please have a look at the attached... I've coded your userform's initialisation so it dynamically populates when it's opened... That way, you don't need to have an error handler in case there's no match.

    CBx2 populates and becomes active once a CBx1 value has been selected.

    Hitting "Search" (as you've labelled it) plonks a sumifs() formula into A1 on "waste" to do the calculation, and finally replaces it with a fixed value

    You'll note I've changed the codenames of your "Records" and "waste" worksheets to make it easier to reference them in the code. I also changed your data to be in a table as I imagine these are data that will grow over time. It's not necessary but makes things a bit easier, too.

    I was just a bit confused where these figures

    a1=
    If matches week38 and shift A =229.6
    if matches week38 and shift B =59.9

    come from, so you'll have to tweak the "=sumifs(....) formula to suit.

    HTH
    Attached Files Attached Files
    Never stop learning!
    <--- please consider *-ing !

  3. #3
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: userform command button to run code

    Thanks Harrissonland, sorry its taken a while to get back to you, but been working, yeah that all works fine, the only issues I have is you have changed part of the records sheet to a table, 1) I have other code that runs producing results via the records sheet will that be ok, or will I need to change that code 2) can you show code without changing to a table? 3) if not can the table cover all columns and then I can change to code on other buttons to suit what results I need?

    All figures in the records sheet come from a userform data sheet that users enter data on, 90% of data will be entered at the time but there is an edit but to add and change a records details

    thanks

    scouse13

  4. #4
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: userform command button to run code

    Ooops. My table was only a section of your data, but never mind...

    1. Other code will be fine anyway, you can still reference the cells in a table in the normal manner
    2. See below!
    3. Creating a table is as simple as selecting the range you want to include, then hitting [CTRL+T]; the benefits of using a table are discussed extensively elsewhere, but it can be moved about, columns and lines can be inserted or deleted without the code or other references breaking. Whether it's worth it for your application depends how you anticipate it being used, really.


    You could simply replace the table notation with specific ranges, like this.
    Please Login or Register  to view this content.
    It does mean that you have to search for the last used row in each function, rather than using the whole column to iterate through, which is what you'll see up there^^^.

    HTH
    Tim

  5. #5
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: userform command button to run code

    thanks harrisonland will check the new code out, am sure it will work fine and hopefully make it a bit easier for me to change to what other searches I want to do, will let you know and add to reputation.

    thanks for your help

    scouse13

  6. #6
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: userform command button to run code

    HiHarrisonland, run the code before but I am getting " A value in the formula is of the wrong data type" error in the waste a1 cell, any ideas


    Quote Originally Posted by harrisonland View Post
    Ooops. My table was only a section of your data, but never mind...

    1. Other code will be fine anyway, you can still reference the cells in a table in the normal manner
    2. See below!
    3. Creating a table is as simple as selecting the range you want to include, then hitting [CTRL+T]; the benefits of using a table are discussed extensively elsewhere, but it can be moved about, columns and lines can be inserted or deleted without the code or other references breaking. Whether it's worth it for your application depends how you anticipate it being used, really.


    You could simply replace the table notation with specific ranges, like this.
    Please Login or Register  to view this content.
    It does mean that you have to search for the last used row in each function, rather than using the whole column to iterate through, which is what you'll see up there^^^.

    HTH
    Tim
    scouse13

  7. #7
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: userform command button to run code

    Hi there. I've just pasted this into the worksheet after removing the table - it seems to work as intended, so I'm not quite sure what the issue was.

    I've attached it here for you to look at...

    Tim
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: userform command button to run code

    thanks harrisonland, will have a look, I have been playing around with table today as well and sort of got my head around that, just a final thing if you don't mind, working on the set up now of week no, and shift which produces the waste loss, if I wanted to show all shifts what would I need to add.

    ie: choose week no, and choose all shifts, show the results will show the waste loss for shift A, B, C, D, E

    I should with that do most thing I need to

    scouse13

  9. #9
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: userform command button to run code

    This formula will add up all values in column T for a week number stored in A1:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Instead of generating the figure via a VBA routine, you could just pop that in a cell and have the worksheet update automatically.

    Alternatively, you could go the route similar to your week/shift analysis and just use an inputbox or another userform to get a week number from the user.

    I've attached another version demonstrating different approaches on the "main" worksheet. (I also added a bit of error checking/handling in the userforms.)

    Tim
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    06-27-2014
    Location
    bristol, england
    MS-Off Ver
    excel 2007
    Posts
    264

    Re: userform command button to run code

    thanks Harrisonland for all your help, think I have my head around it now to progress further, but all works fine

    thank you

    scouse13

  11. #11
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: userform command button to run code

    No worries - and thanks for rep.

    Tim

+ 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. UserForm command button VBA code
    By Ratu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2017, 09:50 PM
  2. [SOLVED] Run Command Prompt Icacls code in VBA Command Button on Userform
    By luismalave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2015, 11:40 PM
  3. Not sure how to write the code for the command button when making a userform
    By andzia in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2015, 05:45 PM
  4. [SOLVED] Userform: Code for DELETE on a command button
    By Gal403 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2014, 01:28 AM
  5. [SOLVED] VBA Code to Gray Out Command Button on Userform
    By riffology in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2013, 02:49 PM
  6. [SOLVED] getting data from command button in userform why is my code not working?
    By Curious2 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 06-07-2013, 10:37 AM
  7. [SOLVED] Excel vba userform - code for command button not working
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-26-2013, 11:27 AM

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