+ Reply to Thread
Results 1 to 4 of 4

Why does code called from a macro button to write data to an excel table work correctly, b

  1. #1
    Registered User
    Join Date
    11-27-2017
    Location
    Redondo Beach, California
    MS-Off Ver
    2016
    Posts
    3

    Question Why does code called from a macro button to write data to an excel table work correctly, b

    Please Login or Register  to view this content.
    Worksheet “Sheet1” contains an excel table that has 6 columns, 40 rows with a header row beginning in worksheet row #7. The first 6 rows of the worksheet are blank.

    When called from a macro button on the worksheet, the above code writes txtSTR to the cells in column 2 and 3 of row 9 in the table (corresponding to row 16 of the worksheet). This is what I would expect and want to happen.

    However, when the same identical code is called from within a Userform, the data is written to the cells in column 2 and 3 of row 16 in the table (corresponding to row 23 of the worksheet) i.e. the right columns but wrong row. I’ve been trying to figure this out for some time now, without any joy. Maybe I’m not seeing something very obvious, but I would appreciate any insight. I understand that I could code around this by adjusting the rownum variable, but I’m really baffled as to why the code operated differently.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,166

    Re: Why does code called from a macro button to write data to an excel table work correctl

    Based just on this code I am baffled too. Can you please attach your entire file for troubleshooting? It will be much easier to understand your problem if you provide your file. This allows us to see and experiment with your data, layout, formulas, code, and possibly attach a file with a completed solution.

    Under the text box where you type your reply click the Go Advanced button. On the next screen scroll down and click on Manage Attachments, which will show a pop-up window to Select and Upload a file. Then close the window.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Why does code called from a macro button to write data to an excel table work correctl

    Are you using another variable called "rownum" in another procedure/scope that the userform could have initialized?

    Otherwise without being able to see it myself I would say use "debug.print" liberally throughout your code. Check that the workbook is the correct one, the sheets correct, correct table, address of the table range, value of rownum variable, etc. by spitting it all out via debug.print lines added to your code.

    In the context provided I dont see a blaring issue with the code, but if you have a userform then you presumably have other code too, which we cant see.

  4. #4
    Registered User
    Join Date
    11-27-2017
    Location
    Redondo Beach, California
    MS-Off Ver
    2016
    Posts
    3

    Re: Why does code called from a macro button to write data to an excel table work correctl

    Thanks for the prompt replies.

    My apologies. The sample code I posted does indeed behave the same regardless of whether it is called from a macro or from a userform. I will be a little more careful and less hasty before posting future questions. I am still very much a VBA newbie! Thanks again for your responses.

+ 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: 01-26-2015, 07:56 PM
  2. How to write a code so that a macro called "Mega" runs only once if cell value A1 > 0
    By omega0010 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-31-2014, 10:47 AM
  3. [SOLVED] Command button with macro does not work correctly
    By c_spooner1999 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-01-2013, 11:22 AM
  4. userform button code needs attention to work correctly
    By rampres! in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-15-2012, 03:30 PM
  5. macro fails to function correctly when called from button form control
    By sarails in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-22-2011, 01:39 AM
  6. Private sub not working correctly when called from another macro
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2008, 08:16 PM

Tags for this Thread

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