+ Reply to Thread
Results 1 to 4 of 4

Using command button in userform to update current row

  1. #1
    Registered User
    Join Date
    07-25-2014
    Location
    wagga wagga
    MS-Off Ver
    2010
    Posts
    8

    Using command button in userform to update current row

    Hi All,
    I've been learning as I go, never played with vba before discovering that a userform might make my spreadsheet data entry easier. Now I'm obsessed..!

    Problem is this:

    Usually it's being used to enter new details into a spreadsheet into the first available blank row, but occasionally I need to retrieve details from sheet to userform based on the invoice number keyed into the userform, using the "CmdRetrieveDetails" button. This works a treat.

    I then wish to update details in the sheet and use "CmdUpdateDetails" button to enter them back into the spreadsheet in the row from which they came, but instead they end up in the next blank row still... Can anyone shed any light?

    Code for retrieval button and update details button is below:

    Please Login or Register  to view this content.
    ***There are only 2 values in this list so far, as a test showed that they were still going into the next blank row so I didn't continue!**

    There is a global command at the start, which came from the instruction video for this code

    Dim currentrow As Long

    **

    Help please!!! Thanks! Let me know what you need from me...
    Last edited by puffo; 08-06-2014 at 05:46 PM. Reason: adding code tags

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Using command button in userform to update current row

    Hi puffo,

    Welcome to ExcelForum. Next time you post code please use CODE TAGS. See the forum rules for instructions: http://www.excelforum.com/excel-prog...rum-rules.html

    Your code keeps on going after your find the first occurrence of the 'Invoice Number'. You need to exit the loop after you process the 'Invoice Number'.

    Try this (code change highlighted in red):
    Please Login or Register  to view this content.
    Lewis

  3. #3
    Registered User
    Join Date
    07-25-2014
    Location
    wagga wagga
    MS-Off Ver
    2010
    Posts
    8

    Re: Using command button in userform to update current row

    LJMetzger you are a legend.. so many hours trying this and that, and so simple in the end! Thank you!

    2 questions from here:

    1. in the spirit of learning - the "For" starts before the "if", so in my mind logic dictates that the "if" should end first, and be contained inside the "for", but this does not work.. Why?! (If there is a shortish answer..!)

    2. Is there an easier way to carry on from here and add alll the relevant fields (30ish) under the cmdUpdateDetails command button subroutine without defining each one as a string individually like in my existing code?
    Thanks!

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Using command button in userform to update current row

    in the spirit of learning - the "For" starts before the "if", so in my mind logic dictates that the "if" should end first, and be contained inside the "for"
    You are absolutely correct. Think of the for..next and if..endif constructions as a for..next bracket and an if..endif bracket, and the rules state that the brackets can never cross (i.e. one always has to be inside the other). If you single step through the code (F8 in the debugger), you will see that if the 'exit for' is OUTSIDE if..endif (i.e. after the endif) that we will ALWAYS exit after the first invoice number is checked. If is inside the if..endif, so that when we find a MATCHING invoice number, we EXIT and don't process any more invoice numbers.

    s there an easier way to carry on from here and add alll the relevant fields (30ish) under the cmdUpdateDetails command button subroutine without defining each one as a string individually
    There may be an easier way, but I don't know of one. Your code is very easy to read and follow as is. I write my code in a manner similar to yours.

    One difference is I usually don't like to reference column numbers for items directly in the code. Especially if the column numbers (or row numbers) appear in more than one place in the code. That way if the worksheet design changes, VBA changes to column numbers only have to be in ONE PLACE. I put all those definitions in a special (ordinary) code module I call 'ModConstants'. For example:

    Module 'ModConstants':
    Please Login or Register  to view this content.
    Module 'Module1' or ThisWorkbook Module or a Sheet Module:
    Please Login or Register  to view this content.
    Lewis

+ 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. Coding Search and Update Command Button in an UserForm
    By sottojeromey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2013, 06:39 AM
  2. Command button to get current date and time
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-02-2013, 11:39 PM
  3. Automatically update existing data using a Update command button
    By 9999335 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-23-2012, 09:46 PM
  4. UserForm, update after command button click
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-01-2010, 01:05 PM
  5. Command button to email the current spreadsheet
    By matpj in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-13-2007, 05:05 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