+ Reply to Thread
Results 1 to 10 of 10

How to fix macro not returning value to correct field?

  1. #1
    Forum Contributor
    Join Date
    09-19-2016
    Location
    Alaska
    MS-Off Ver
    MS 2010, 2013, 2016
    Posts
    272

    How to fix macro not returning value to correct field?

    Hello everyone,

    I have macro, thanks to sintek and xlnitwit (users of this forum). It works well for the most part, but there is an issue with it when it comes to the tab it creates named "Outgoing Wires". As you see in the highlighted part below, the code should first grab the values in column 9 from sheet1 and place them in column "D" from the "Outgoing Wires" tab. Then it should get the values from column 8 from sheet1 and place them in column "E". For some reason the values are being brought over but backwards and it doesn't matter if you change the value from 8 to 9 the code brings the same value over. Could someone review and advise? Also if you notice, some of the values in column "F" for the "Outgoing Wires" is returned as 6.54316e+16, anyone know why the code doesn't return the same format from sheet1?

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by AccountingJ; 12-07-2017 at 11:16 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    Re: How to fix macro not returning value to correct field?

    It's hard to determine what the problem is, I have noticed some of the code is not referring to the correct sheet.
    Some areas you need a period to confirm what sheet you are referring to.

    Please Login or Register  to view this content.
    Columns would also have to be confirmed.
    Please Login or Register  to view this content.
    The code may be referring to the incorrect sheet if you don't confirm it.

  3. #3
    Forum Contributor
    Join Date
    09-19-2016
    Location
    Alaska
    MS-Off Ver
    MS 2010, 2013, 2016
    Posts
    272

    Re: How to fix macro not returning value to correct field?

    I just added the periods, thanks for that. The code is referring to the correct sheet, the values it brings over to the "Outgoing Wires" tab although some are not in the correct order, they are the values we should see.
    Last edited by AccountingJ; 12-07-2017 at 09:46 AM.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    Re: How to fix macro not returning value to correct field?

    That is very interesting, I've added a msgbox to the code and it still comes up with the 8 before the 9. I assume it has something to do with the array, and it wants to do it in order, but other orders do not come up incorrectly.
    Just as a test, if you changed the 8 to a 7, it does run in the correct order.

    Please Login or Register  to view this content.
    Last edited by davesexcel; 12-07-2017 at 10:37 AM.

  5. #5
    Forum Contributor
    Join Date
    09-19-2016
    Location
    Alaska
    MS-Off Ver
    MS 2010, 2013, 2016
    Posts
    272

    Re: How to fix macro not returning value to correct field?

    I think you are on to something, I replaced the 8 with 7 and it works just like you said but I do need the values from 8 to come up. I added your code and get (without the quotation marks) "$BN$3", "$C$3", "$H$3", "$I$3". "$M$3", "$P$3", "$Q$3" which translates to cell 66, 3, 8, 9, 13, 16, 17. For some reason instead of running
    Please Login or Register  to view this content.
    it does
    Please Login or Register  to view this content.
    even though that's not the order set.

    When I run your code with replacing 8 with 7 the code brings the right right values.
    Last edited by AccountingJ; 12-07-2017 at 10:54 AM.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: How to fix macro not returning value to correct field?

    Basically it's how UNION puts together range, or rather Excel handles range.

    If you add Debug.Print rng.Address right after UNION line. You should see address returned as below.
    "$BN$3,$C$3,$H$3:$I$3,$M$3,$P$3:$Q$3"

    Any contiguous range will be handled as one and will always get put together from left to right (depending on language and Excel's setting it can reverse).

    If you want to avoid this, instead of using Union, just put entire used range into array and do all manipulation within and use ARRAY() instead of UNION or just directly access using element index.

    Edit: Alternately, you can put cells in to some collection and loop through it, instead of Union range.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to fix macro not returning value to correct field?

    It is because they are contiguous, so Union turns them into one area, and then the For Each loops from left to right. You can skip the Union and just create an array though
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Forum Contributor
    Join Date
    09-19-2016
    Location
    Alaska
    MS-Off Ver
    MS 2010, 2013, 2016
    Posts
    272

    Re: How to fix macro not returning value to correct field?

    Thank you both, CK76 and xlnitwit I appreciate the explanation instead of just the answer.

    That did it! should I make the similar changes to the other code below, that does correct though.

    Please Login or Register  to view this content.
    No big deal, but you know why the format from sheet1 is not being brought over to the other tabs?

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to fix macro not returning value to correct field?

    The Value and format of a cell are independent of each other. Assigning the value of one cell to another cell does not transfer formatting.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: How to fix macro not returning value to correct field?

    You are welcome and thanks for rep

    I'd recommend keeping structure/method consistent through out your project/module. So yes, makes changes to rest of your code to match.

    As for format not being transferred...
    Array only holds cell.Value as specified in code and only values are put back into range in other sheets.

    You can just use the array holding each cell loop and copy/paste into cell. Or adjust format of cells after array is put into range.

+ 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. [SOLVED] Countifs no returning the correct value
    By lizsantiago07 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-13-2017, 11:08 PM
  2. [SOLVED] formula not returning the correct value
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2016, 02:24 PM
  3. [SOLVED] Calculated Field in Pivot Table, Not Returning Correct If Function
    By egarcia7 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-09-2016, 12:49 PM
  4. Formula sometimes returning correct answer and sometimes returning 0
    By redimp in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-09-2010, 06:28 AM
  5. [SOLVED] MAX Function not returning correct value
    By Phil in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-28-2006, 05:20 PM
  6. Re: UDF not returning correct information
    By keepITcool in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-13-2005, 07:05 PM
  7. [SOLVED] Vlookup not returning correct value
    By Mandy Brookes in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-26-2005, 09:06 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