+ Reply to Thread
Results 1 to 20 of 20

How to dimension and initialize a 2D array of strings (variants?)

  1. #1
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Question How to dimension and initialize a 2D array of strings (variants?)

    Hi,

    I've been "using" Excel VBA for a few dozen years off-and-on, but I don't normally need arrays beyond one dimension..

    The below code works, but I had to resort to some "fixes" that left me puzzled.

    I first tried to declare and fill the 2d array on the same line (tried with and without keyword New), no luck, so ended up doing it C-style, one at a time.

    In the nested loop, tried "country = ar(2, indx)". Didn't work?!? Got the "mess" you see below from the variable in the locals window...

    At this point I'm more than willing to feel stupid - can someone enlighten please :-)

    Thanks!
    GC



    Please Login or Register  to view this content.
    Last edited by Grug.Crood; 05-21-2021 at 05:41 PM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    Your VBA procedure revamped :

    PHP Code: 
    Sub fixCountryNames(Ws As Worksheet)
        
    With Ws.Range("B2"Ws.[B1].End(xlDown))
                
    = .Value2
            
    For R& = 1 To UBound(V)
                
    Select Case V(R1)
                       Case 
    "Czechia":  V(R1) = "Czech Republic"
                       
    Case "USA":      V(R1) = "United States"
                       
    Case "UK":       V(R1) = "United Kingdom"
                       
    Case "UAE":      V(R1) = "United Arab Emirates"
                       
    Case "S. Korea"V(R1) = "South Korea"
                
    End Select
            Next
               
    .Value2 V
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 05-21-2021 at 06:57 PM.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Or this …


    Maybe a better way :

    PHP Code: 
    Sub fixCountryNames(Ws As Worksheet)
            
    = [{"Czechia","Czech Republic";"USA","United States";"UK","United Kingdom";"UAE","United Arab Emirates";"S. Korea","South Korea"}]
            
    Application.ScreenUpdating False
        With Ws
    .Range("B2"Ws.[B1].End(xlDown))
            For 
    R& = 1 To UBound(V)
                .
    Replace V(R1), V(R2), xlWhole
            Next
        End With
            Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Or this …

    Or this.
    See attached.

    Column A, from the first Row on down, has the values to be changed.
    Column B, from the first Row on down, has the values to be changed into.

    Column C has the data to be checked and changed

    Column E gets the result

    Change all refrences where required.

    It goes through the whole list in case of multiple instances of the same values.

    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Quote Originally Posted by Grug.Crood View Post
    In the nested loop, tried "country = ar(2, indx)". Didn't work?!? Got the "mess" you see below from the variable in the locals window...
    This is a typical example.
    Since you didn't declare country, it is Variant.
    So it is only assigning the value ar(2, index) to country.

    If you declare country as range, or country.value = ar(2, index), it should fix it.

  6. #6
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: Or this …

    Jindon,

    Per following code window, adding .value (country.value) or prior declaration (dim country as Range) both worked, thank you! I am still, however, puzzled due to:

    ? vartype(country)
    8
    ? vartype(country.value)
    8

    Also, I reckon seeing varType(countries.Cells) report as an array kind of surprised me because the locals window shows "variant/object/range".

    If you can clarify where my thought process went South that'd be great, but I am satisfied I can take it from here (topic closed).

    Thanks again!
    GC

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: Or this …

    Marc,

    Much appreciated, I don't recall ever having that form in assignments (brackets), but I do have a mind like a cast iron trap -- a little rain and rusted solid.

    Of the below snippit you inspired, is there a way to combine the two lines and initialize on the declaration line? I tried several times, but no luck.

    Thanks for the help!!!

    GC


    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: Hi ! Try this !

    Marc,

    It didn't occur to me to use the with statement. I find them harder to read, but I will try to work through that one (only caveat is the list could change and editing or moving the list to a separate function seems like it would be more work?).

    It's eloquent , thanks again!

    GC

    Quote Originally Posted by Marc L View Post

    Your VBA procedure revamped :

    PHP Code: 
    Sub fixCountryNames(Ws As Worksheet)
        
    With Ws.Range("B2"Ws.[B1].End(xlDown))
                
    = .Value2
            
    For R& = 1 To UBound(V)
                
    Select Case V(R1)
                       Case 
    "Czechia":  V(R1) = "Czech Republic"
                       
    Case "USA":      V(R1) = "United States"
                       
    Case "UK":       V(R1) = "United Kingdom"
                       
    Case "UAE":      V(R1) = "United Arab Emirates"
                       
    Case "S. Korea"V(R1) = "South Korea"
                
    End Select
            Next
               
    .Value2 V
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Did the attachment in Post #4 work at all?

  10. #10
    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,903

    Re: How to dimension and initialize a 2D array of strings (variants?)

    If country is a Variant, then country = ar(2, indx) means that you want to replace the current contents of the variable country (a cell) with whatever is in the array. So the country variable simply switches from referring to a cell to referring to a string at that point (which you don't then do anything with). If you declare it as a range, then you can't replace the variable's contents with a Let assignment (you'd need a Set statement), so it assigns the array value to the default range property (value).
    Last edited by rorya; 05-24-2021 at 03:18 PM.
    Rory

  11. #11
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Quote Originally Posted by rorya View Post
    If country is a Variant, then country = ar(2, indx) means that you want to replace the current contents of the variable country (a cell) with whatever is in the array. So the country variable simply switches from referring to a cell to referring to a string at that point (which you don't then do anything with). If you declare it as a range, then you can't replace the variable's contents with a Let assignment (you'd need a Set statement), so it assigns the array value to the default range property (value).
    That (the underlined) is exactly what I was trying to do. The variable changed, but not the spreadsheet cell value.

    I have three ways to make that work, but still don't know how to declare and initialize the array of values (on one line).

    CG

  12. #12
    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,903

    Re: How to dimension and initialize a 2D array of strings (variants?)

    What I mean is that it goes from:

    country -> refers to a cell

    to:

    country -> refers to a string variable


    You can't declare and assign an array in one statement in VBA.

  13. #13
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: How to dimension and initialize a 2D array of strings (variants?)

    @Jolivanes, it probably does, but I can't do the work on the sheet itself, so needed to work the list in the VBA coding.

    I don't suppose you know how to declare and initialize a 2 dimensional array of strings (1 to 2, 1 to 5) on one line of code?

    GC

  14. #14
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Quote Originally Posted by rorya View Post
    What I mean is that it goes from:

    country -> refers to a cell

    to:

    country -> refers to a string variable


    You can't declare and assign an array in one statement in VBA.
    Thanks Rorya

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Re: "but I can't do the work on the sheet itself"
    There are other sheets. Hidden if need be. External workbooks is another possibility.
    I jus can't see me, or anybody, typing in or changing a whole bunch of names in code.
    What are you going to do if you use similar code for an extended amount like 40 pieces of data?

  16. #16
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Quote Originally Posted by jolivanes View Post
    Re: "but I can't do the work on the sheet itself"
    There are other sheets. Hidden if need be. External workbooks is another possibility.
    I jus can't see me, or anybody, typing in or changing a whole bunch of names in code.
    What are you going to do if you use similar code for an extended amount like 40 pieces of data?
    Long story short, I don't expect it will change. If it does then there are options and as you suggest, it could be easier to do it using a worksheet. My biggest surprise was seeing disparity between the local's window vs varType, and needing to add the brackets to initialize the array. Oh well, I'll retire soon and trade excel for a fishing pole and some good cigars...

  17. #17
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: Hi ! Try this !

    @Marc,

    I said I'd look … well, just did. Maybe eloquent was an understatement --- VERY impressive.

    Love it!!!
    GC


    Quote Originally Posted by Marc L View Post

    Your VBA procedure revamped :

    PHP Code: 
    Sub fixCountryNames(Ws As Worksheet)
        
    With Ws.Range("B2"Ws.[B1].End(xlDown))
                
    = .Value2
            
    For R& = 1 To UBound(V)
                
    Select Case V(R1)
                       Case 
    "Czechia":  V(R1) = "Czech Republic"
                       
    Case "USA":      V(R1) = "United States"
                       
    Case "UK":       V(R1) = "United Kingdom"
                       
    Case "UAE":      V(R1) = "United Arab Emirates"
                       
    Case "S. Korea"V(R1) = "South Korea"
                
    End Select
            Next
               
    .Value2 V
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

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

    Re: How to dimension and initialize a 2D array of strings (variants?)

    Grug.Crood,

    What I meant to say was that this is a typical case when you don't properly declare the variable type and not specifying the property.

    Look at the type in local window when step through the code.
    country = Variant/Empty at start
    country = Variant/Object/Range when loop commence
    country = Variant/String when ar(2, indx) is Text value. Varaint/Integer or Double etc. depends on ar(2, indx)
    So, it will never change the value in cell

    As I mentioned earlier, if you declare country as range or specify the property, it stays as Range/Range, Variant.Object/Range, so that you can change the value in cell.

    HTH

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Or this …


    Thanks for the rep' !

    Quote Originally Posted by Grug.Crood View Post
    I don't recall ever having that form in assignments (brackets)
    It's the short syntax of the Evaluate method, a VBA help must read !


    Quote Originally Posted by Grug.Crood View Post
    is there a way to combine the two lines and initialize on the declaration line?
    PHP Code: 
        Dim V:  = [{"Czechia","Czech Republic";"USA","United States";"UK","United Kingdom";"UAE","United Arab Emirates";"S. Korea","South Korea"}] 

  20. #20
    Registered User
    Join Date
    10-02-2017
    Location
    Huntsville, Alabama
    MS-Off Ver
    2010
    Posts
    57

    Re: How to dimension and initialize a 2D array of strings (variants?)

    @jindon

    Ah hah. I did not realize once a type is assigned on a variant it could be changed.

    Thanks!
    GC

    Quote Originally Posted by jindon View Post
    Grug.Crood,
    ...

    Look at the type in local window when step through the code.

    country = Variant/Object/Range when loop commence
    ...
    country = Variant/String when ar(2, indx) is Text value.

    HTH

+ 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] Reorder array columns dimension by another array
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-16-2018, 12:53 AM
  2. [SOLVED] Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Variable
    By joe31623 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-08-2015, 11:35 AM
  3. [SOLVED] Passing UserForm Variants into an Array [Excel 14/2010 VBA only)
    By lloydgodin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2012, 11:47 AM
  4. Does filling part of an array from a range re-dimension the array?
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-04-2011, 10:09 AM
  5. Limits on Variants, strings, etc?
    By ilovedurango in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2007, 02:25 PM
  6. Mutli-dimensional Array to Single-Dimension Array
    By Blue Aardvark in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-15-2005, 05:05 AM
  7. [SOLVED] Array Parameters as Variants Only
    By TheVisionThing in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-25-2005, 06:06 PM

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