+ Reply to Thread
Results 1 to 19 of 19

Range to Array WITH Numberformat

  1. #1
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Question Range to Array WITH Numberformat

    I have a worksheet containing two columns:

    PL208 8032510
    PL209 8054146
    PL2010 8065432
    PL2011 8056412

    The thing is that I used Numberformat "PL20"# for a part of the first column.
    Later I plan to use "PL21"#, "PL22"# (referring to each year).
    So each cell in column 1 contains a simple number like '8' or '9'
    which is converted by Numberformat to 'PL208' and 'PL209'.

    I can make a Range out of these two columns which I can put in an array,
    but the array then contains '8', '9' etc. instead of the preferred 'PL208' and 'PL209'.

    What can I do to have VBA take the Numberformat into account when putting these columns into an Array?
    Last edited by Rezzy777; 03-08-2021 at 10:35 AM.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Range to Array WITH Numberformat

    A simple method would be to load the array as normal, then loop through the first column formatting the values.
    Rory

  3. #3
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    My bad, I forgot to add a tiny piece of info:
    the prefix will change halfway the list to "PL21"#, "PL22"# etc.

    It would be so much easier if VBA could just take the *full* value of each cell into account!

    I already tried using Range.Text and Range.Value(11) but I can't get them to work for this 2D Range...
    Last edited by Rezzy777; 03-08-2021 at 10:42 AM.

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Range to Array WITH Numberformat

    You'll have to loop through and read either the Text or Numberformat properties for each cell in column A then. You can't get an array out of Range.Text

  5. #5
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    I was already afraid of that... many thanks anyway, I'll try something else!

  6. #6
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    Quote Originally Posted by rorya View Post
    You'll have to loop through and read either the Text or Numberformat properties for each cell in column A then. You can't get an array out of Range.Text
    Sorry for coming back at you, but I still can't find a proper solution.

    Here are my ideas:
    - Do I really need to scan through each cell for its Numberformat? Or can I somehow save all Numberformats in a single Array with one click?
    - Is there perhaps some way I can copy the .Text property of the entire Range to a new sheet? Should I use PasteSpecial to do this?
    - Can I somehow put the .Text of the Range into a String Array?
    - Why is this rather simple issue so hard to solve? Has nobody ever needed this before? :-)

    I just need a fast solution (hopefully without having to go through each cell) since I want this to happen while opening the file (I don't want to keep the user waiting)

    Many thanks in advance!

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Range to Array WITH Numberformat

    Quote Originally Posted by Rezzy777 View Post
    Sorry for coming back at you, but I still can't find a proper solution.

    Here are my ideas:
    - Do I really need to scan through each cell for its Numberformat?
    Most likely, yes.

    Or can I somehow save all Numberformats in a single Array with one click?
    No
    - Is there perhaps some way I can copy the .Text property of the entire Range to a new sheet?
    No. You could export the sheet as a CSV but unless you have a LOT of data, I don't see that being quicker. Have you actually tested and timed any aproaches?
    Should I use PasteSpecial to do this?
    No, that won't work
    - Can I somehow put the .Text of the Range into a String Array?
    No, as I mentioned earlier.

    - Why is this rather simple issue so hard to solve? Has nobody ever needed this before? :-)
    Most people don't use formatting as data.

  8. #8
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    Hahaha okay, no other solutions then :-) :-)
    Thank you very much, I'll go through each cell then... ;-)

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Range to Array WITH Numberformat

    Is there a rule that determines the format? How does it get applied in the first place?

  10. #10
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    Good question.

    This is a list with product numbers and -codes for my colleagues.
    The products in column 1 are numbered 'PL208', 'PL209', 'PL2010' etc., and the associated codes are in column 2.

    The product numbers are always ascending, so I wanted to make this as easy as possible for my colleagues
    when they need to add a new product number at the bottom: they select the bottom number and drag it down a few rows.

    The year is added as a prefix ('20' or '21'), so product '1' becomes '201', or '2011', '2046', '20129' etc.
    I used this Numberfomat: "PL20"# and "PL21"#. But now VBA only gets the part AFTER the prefix, i.e. '1', '57', '169'.

    I am building a Excel sheet that makes it easier to fill in products in our webshop (through a feed).
    When my colleagues type the product number 'PL20155', I want to scan through this list of codes
    so it automatically puts the associated code in a cell.
    Therefore I need to have the entire cell value (not only '155') since I'll probably have '155' in 2020 and one in 2021 as well.

    Quite a story, I hope it makes sense. If not, please let me know. ;-)

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Range to Array WITH Numberformat

    I don't do like this for myself, but one way.

    Assuming you have data in col.A & Col.B and read the cell format from A2.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    Wow, thanks, I'll look into that!

    You say you don't like it for yourself. What would you prefer instead?

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Range to Array WITH Numberformat

    The most reason to use array is to get faster loop, so if I really need to maintain the format, I would get the data directly from the range and, like rorya said, change the format if necessary or apply the cell format after output.

  14. #14
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    True, that's the reason I was hoping to use an Array for this ;-)

    Could you elaborate this a bit more please:

    so if I really need to maintain the format, I would get the data directly from the range and, like rorya said, change the format if necessary or apply the cell format after output.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Range to Array WITH Numberformat

    I mean change the data while loop.
    e.g
    While loop
    Please Login or Register  to view this content.
    Or after output
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    Wow, I thought I knew quite a lot of VBA :'-D

    Some things in your code are new to me, could you please explain these:

    Please Login or Register  to view this content.
    1. 'With Range' is new to me. Does it mean it applies 'myFormat' and 'myArray' to all cells inside?
    2. Why do you use resize?


    Please Login or Register  to view this content.
    3. What does the '[h1]' mean?
    4. Why do you need to resize again?

    Thanks for helping me out!

  17. #17
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Range to Array WITH Numberformat

    If the data is sorted, you’ll only need to check the number format if the cell value is 1, then reuse that format until the next 1 and so on.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Range to Array WITH Numberformat

    So, you don't understand the nature of array...

    1) With ... End With
    It is to reference the object.
    So, It is equivalent to
    Please Login or Register  to view this content.
    There are a lot of sites if you search "With End With VBA"

    2) What else do you want to use?

    3) [h1] means Range("H1")

    4) When you deal with the array, Resize property is the MUST to know.
    Unlike Copy/Paste, you need to specify the size of range that you want for output the data from array.

    There are other natures that you need to know about the array, so you will need to learn.

  19. #19
    Registered User
    Join Date
    08-10-2020
    Location
    Netherlands
    MS-Off Ver
    2019
    Posts
    81

    Re: Range to Array WITH Numberformat

    1) With ... End With
    It is to reference the object.
    Haha sorry, I knew that. Must have been sleeping ;-)

    2) What else do you want to use?
    Err, what do you mean? I don't understand why you made the Array larger ;-)

    3) [h1] means Range("H1")
    Never knew that, thanks! :-)

    4) When you deal with the array, Resize property is the MUST to know.
    Unlike Copy/Paste, you need to specify the size of range that you want for output the data from array.
    Got it, thanks!

+ 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. data array with date range and return text value in result
    By jigarecity in forum Excel General
    Replies: 2
    Last Post: 01-27-2021, 10:04 AM
  2. Text array update from range
    By Imh0tep in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-19-2019, 03:49 PM
  3. [SOLVED] VBA Evaluate Array then imput into range adjusting forumla in Array down with the Range
    By fireguy7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-11-2019, 03:40 PM
  4. [SOLVED] ARRAY Formula to return text that meets critera based on a range
    By RoundaboutCJP in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-10-2017, 06:30 AM
  5. [SOLVED] VBA to replace cells containing specific text in a large range with an array formula
    By DaveBre in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-22-2016, 01:21 AM
  6. Replies: 1
    Last Post: 05-18-2016, 05:00 AM
  7. Make an array from text and sheet range
    By mikewild2000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-17-2005, 01:45 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