Using data type “range” similar to “collection” - VBAAre these novelty ways [and possibly the best way?] to refer a dynamic cell in VBA?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopsReferring to Range address in a VBA entered formulaAdd formula to range using VBAInsert a formula in a range of cells via VBA using reference cellsExcel VBA select range using cells and xlDownLooping through multiple named ranges and going to similar named range VBAVBA Jagged Array to RangeCollection of Ranges in VBAVBA : combine range with row insert and mergingUse multiple collections (or other types) for same “with…” argument?

Fixing obscure 8080 emulator bug?

Any way to create a link to a custom setting's "manage" page?

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

Second (easy access) account in case my bank screws up

Soft question: Examples where lack of mathematical rigour cause security breaches?

How is John Wick 3 a 15 certificate?

Colloquialism for “see you later”

How come the nude protesters were not arrested?

How is water heavier than petrol, even though its molecular weight is less than petrol?

Compiling C files on Ubuntu and using the executable on Windows

Giant Steps - Coltrane and Slonimsky

Inward extrusion is not working

Is a lack of character descriptions a problem?

Mathematically, why does mass matrix / load vector lumping work?

Does Disney no longer produce hand-drawn cartoon films?

Group Integers by Originality

How to handle self harm scars on the arm in work environment?

Is it a problem if <h4>, <h5> and <h6> are smaller than regular text?

Someone whose aspirations exceed abilities or means

Meaning of 'lose their grip on the groins of their followers'

A IP can traceroute to it, but can not ping

How did old MS-DOS games utilize various graphic cards?

Thread Pool C++ Implementation

How to manually rewind film?



Using data type “range” similar to “collection” - VBA


Are these novelty ways [and possibly the best way?] to refer a dynamic cell in VBA?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopsReferring to Range address in a VBA entered formulaAdd formula to range using VBAInsert a formula in a range of cells via VBA using reference cellsExcel VBA select range using cells and xlDownLooping through multiple named ranges and going to similar named range VBAVBA Jagged Array to RangeCollection of Ranges in VBAVBA : combine range with row insert and mergingUse multiple collections (or other types) for same “with…” argument?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have an issue I was wondering if anyone could assist me with... I would like to use data type "range" in a similar way to how "collection" works. I would like to use a counter and a loop: "rng(i) = value" where i can be 1, 7, 100 etc. Hence, if I add "A1, A5, C3, D6" to rng, I would like "rng(3)= 3" to set cell C3 equal to 3. Using the "for each x in range" is not an option with regard to how the code are supposed to work. It it possible to make that work?



An alternative solution for me would be if I could add all individual cells in 7 different collections to one variable of data type range.



Any suggestions?



Regards,
Alexander










share|improve this question




























    0















    I have an issue I was wondering if anyone could assist me with... I would like to use data type "range" in a similar way to how "collection" works. I would like to use a counter and a loop: "rng(i) = value" where i can be 1, 7, 100 etc. Hence, if I add "A1, A5, C3, D6" to rng, I would like "rng(3)= 3" to set cell C3 equal to 3. Using the "for each x in range" is not an option with regard to how the code are supposed to work. It it possible to make that work?



    An alternative solution for me would be if I could add all individual cells in 7 different collections to one variable of data type range.



    Any suggestions?



    Regards,
    Alexander










    share|improve this question
























      0












      0








      0








      I have an issue I was wondering if anyone could assist me with... I would like to use data type "range" in a similar way to how "collection" works. I would like to use a counter and a loop: "rng(i) = value" where i can be 1, 7, 100 etc. Hence, if I add "A1, A5, C3, D6" to rng, I would like "rng(3)= 3" to set cell C3 equal to 3. Using the "for each x in range" is not an option with regard to how the code are supposed to work. It it possible to make that work?



      An alternative solution for me would be if I could add all individual cells in 7 different collections to one variable of data type range.



      Any suggestions?



      Regards,
      Alexander










      share|improve this question














      I have an issue I was wondering if anyone could assist me with... I would like to use data type "range" in a similar way to how "collection" works. I would like to use a counter and a loop: "rng(i) = value" where i can be 1, 7, 100 etc. Hence, if I add "A1, A5, C3, D6" to rng, I would like "rng(3)= 3" to set cell C3 equal to 3. Using the "for each x in range" is not an option with regard to how the code are supposed to work. It it possible to make that work?



      An alternative solution for me would be if I could add all individual cells in 7 different collections to one variable of data type range.



      Any suggestions?



      Regards,
      Alexander







      vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 18:10









      AlexanderAlexander

      265




      265






















          3 Answers
          3






          active

          oldest

          votes


















          1














          Unfortunately that would only work if your collection contained a single continuous range.



          With a collection of disconnected cells, each cell is its own Area, and trying to directly index the combined Range will give you unexpected results: the index will be applied to the first area, and because it's a single cell area, it will go out of bounds, so e.g. for the range of A1, A5, C3, D6, rng(3) will refer to cell A3 (third cell down relative to A1).



          To make indexing work the way you want, you need to mention the Areas property explicitly:



          Dim coll As Range

          ' Set initial contents - has to be at least one cell, can be more
          Set coll = some_worksheet.Range("A1,A5,C3")

          ' This is how you add to already stored "collection"
          Set coll = Application.Union(coll, some_worksheet.Range("D6"))

          coll.Areas(3) = 42 ' Sets C3 to 42





          share|improve this answer























          • This is great, I'll give this a go tomorrow morning. Thank you!

            – Alexander
            Mar 24 at 19:06











          • Works perfect, thank you so much

            – Alexander
            Mar 25 at 7:25


















          1














          I'm not sure exactly what you're asking.



          Perhaps this is what you're looking for:




          MSDN: Looping Through a Range of
          Cells



          Another easy way to loop through a range is to use a For Each...Next
          loop
          with the collection of cells specified in the Range property.
          Visual Basic automatically sets an object variable for the next cell
          each time the loop runs. The following procedure loops through the
          range A1:D10, setting to 0 (zero) any number whose absolute value is
          less than 0.01.



          Sub RoundToZero2() 
          For Each c In Worksheets("Sheet1").Range("A1:D10").Cells
          If Abs(c.Value) < 0.01 Then c.Value = 0
          Next
          End Sub






          share|improve this answer






























            1














            This works with any ranges, contiguous or otherwise



            Sub Test()
            Dim rng As Range
            Set rng = Sheet1.Range("A1,B5,E7:E9")

            Dim v As Variant, a As Range, r As Range

            ReDim v(1 To 1)
            For Each a In rng.Areas
            For Each r In a
            v(UBound(v)) = r.Value
            ReDim Preserve v(1 To UBound(v) + 1)
            Next
            Next
            ReDim Preserve v(1 To UBound(v) - 1)

            Debug.Print v(1)
            Debug.Print v(2)
            Debug.Print v(3)

            End Sub





            share|improve this answer

























            • Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

              – Alexander
              Mar 24 at 19:08











            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326916%2fusing-data-type-range-similar-to-collection-vba%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Unfortunately that would only work if your collection contained a single continuous range.



            With a collection of disconnected cells, each cell is its own Area, and trying to directly index the combined Range will give you unexpected results: the index will be applied to the first area, and because it's a single cell area, it will go out of bounds, so e.g. for the range of A1, A5, C3, D6, rng(3) will refer to cell A3 (third cell down relative to A1).



            To make indexing work the way you want, you need to mention the Areas property explicitly:



            Dim coll As Range

            ' Set initial contents - has to be at least one cell, can be more
            Set coll = some_worksheet.Range("A1,A5,C3")

            ' This is how you add to already stored "collection"
            Set coll = Application.Union(coll, some_worksheet.Range("D6"))

            coll.Areas(3) = 42 ' Sets C3 to 42





            share|improve this answer























            • This is great, I'll give this a go tomorrow morning. Thank you!

              – Alexander
              Mar 24 at 19:06











            • Works perfect, thank you so much

              – Alexander
              Mar 25 at 7:25















            1














            Unfortunately that would only work if your collection contained a single continuous range.



            With a collection of disconnected cells, each cell is its own Area, and trying to directly index the combined Range will give you unexpected results: the index will be applied to the first area, and because it's a single cell area, it will go out of bounds, so e.g. for the range of A1, A5, C3, D6, rng(3) will refer to cell A3 (third cell down relative to A1).



            To make indexing work the way you want, you need to mention the Areas property explicitly:



            Dim coll As Range

            ' Set initial contents - has to be at least one cell, can be more
            Set coll = some_worksheet.Range("A1,A5,C3")

            ' This is how you add to already stored "collection"
            Set coll = Application.Union(coll, some_worksheet.Range("D6"))

            coll.Areas(3) = 42 ' Sets C3 to 42





            share|improve this answer























            • This is great, I'll give this a go tomorrow morning. Thank you!

              – Alexander
              Mar 24 at 19:06











            • Works perfect, thank you so much

              – Alexander
              Mar 25 at 7:25













            1












            1








            1







            Unfortunately that would only work if your collection contained a single continuous range.



            With a collection of disconnected cells, each cell is its own Area, and trying to directly index the combined Range will give you unexpected results: the index will be applied to the first area, and because it's a single cell area, it will go out of bounds, so e.g. for the range of A1, A5, C3, D6, rng(3) will refer to cell A3 (third cell down relative to A1).



            To make indexing work the way you want, you need to mention the Areas property explicitly:



            Dim coll As Range

            ' Set initial contents - has to be at least one cell, can be more
            Set coll = some_worksheet.Range("A1,A5,C3")

            ' This is how you add to already stored "collection"
            Set coll = Application.Union(coll, some_worksheet.Range("D6"))

            coll.Areas(3) = 42 ' Sets C3 to 42





            share|improve this answer













            Unfortunately that would only work if your collection contained a single continuous range.



            With a collection of disconnected cells, each cell is its own Area, and trying to directly index the combined Range will give you unexpected results: the index will be applied to the first area, and because it's a single cell area, it will go out of bounds, so e.g. for the range of A1, A5, C3, D6, rng(3) will refer to cell A3 (third cell down relative to A1).



            To make indexing work the way you want, you need to mention the Areas property explicitly:



            Dim coll As Range

            ' Set initial contents - has to be at least one cell, can be more
            Set coll = some_worksheet.Range("A1,A5,C3")

            ' This is how you add to already stored "collection"
            Set coll = Application.Union(coll, some_worksheet.Range("D6"))

            coll.Areas(3) = 42 ' Sets C3 to 42






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 24 at 18:41









            GSergGSerg

            61.9k15112244




            61.9k15112244












            • This is great, I'll give this a go tomorrow morning. Thank you!

              – Alexander
              Mar 24 at 19:06











            • Works perfect, thank you so much

              – Alexander
              Mar 25 at 7:25

















            • This is great, I'll give this a go tomorrow morning. Thank you!

              – Alexander
              Mar 24 at 19:06











            • Works perfect, thank you so much

              – Alexander
              Mar 25 at 7:25
















            This is great, I'll give this a go tomorrow morning. Thank you!

            – Alexander
            Mar 24 at 19:06





            This is great, I'll give this a go tomorrow morning. Thank you!

            – Alexander
            Mar 24 at 19:06













            Works perfect, thank you so much

            – Alexander
            Mar 25 at 7:25





            Works perfect, thank you so much

            – Alexander
            Mar 25 at 7:25













            1














            I'm not sure exactly what you're asking.



            Perhaps this is what you're looking for:




            MSDN: Looping Through a Range of
            Cells



            Another easy way to loop through a range is to use a For Each...Next
            loop
            with the collection of cells specified in the Range property.
            Visual Basic automatically sets an object variable for the next cell
            each time the loop runs. The following procedure loops through the
            range A1:D10, setting to 0 (zero) any number whose absolute value is
            less than 0.01.



            Sub RoundToZero2() 
            For Each c In Worksheets("Sheet1").Range("A1:D10").Cells
            If Abs(c.Value) < 0.01 Then c.Value = 0
            Next
            End Sub






            share|improve this answer



























              1














              I'm not sure exactly what you're asking.



              Perhaps this is what you're looking for:




              MSDN: Looping Through a Range of
              Cells



              Another easy way to loop through a range is to use a For Each...Next
              loop
              with the collection of cells specified in the Range property.
              Visual Basic automatically sets an object variable for the next cell
              each time the loop runs. The following procedure loops through the
              range A1:D10, setting to 0 (zero) any number whose absolute value is
              less than 0.01.



              Sub RoundToZero2() 
              For Each c In Worksheets("Sheet1").Range("A1:D10").Cells
              If Abs(c.Value) < 0.01 Then c.Value = 0
              Next
              End Sub






              share|improve this answer

























                1












                1








                1







                I'm not sure exactly what you're asking.



                Perhaps this is what you're looking for:




                MSDN: Looping Through a Range of
                Cells



                Another easy way to loop through a range is to use a For Each...Next
                loop
                with the collection of cells specified in the Range property.
                Visual Basic automatically sets an object variable for the next cell
                each time the loop runs. The following procedure loops through the
                range A1:D10, setting to 0 (zero) any number whose absolute value is
                less than 0.01.



                Sub RoundToZero2() 
                For Each c In Worksheets("Sheet1").Range("A1:D10").Cells
                If Abs(c.Value) < 0.01 Then c.Value = 0
                Next
                End Sub






                share|improve this answer













                I'm not sure exactly what you're asking.



                Perhaps this is what you're looking for:




                MSDN: Looping Through a Range of
                Cells



                Another easy way to loop through a range is to use a For Each...Next
                loop
                with the collection of cells specified in the Range property.
                Visual Basic automatically sets an object variable for the next cell
                each time the loop runs. The following procedure loops through the
                range A1:D10, setting to 0 (zero) any number whose absolute value is
                less than 0.01.



                Sub RoundToZero2() 
                For Each c In Worksheets("Sheet1").Range("A1:D10").Cells
                If Abs(c.Value) < 0.01 Then c.Value = 0
                Next
                End Sub







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 18:14









                paulsm4paulsm4

                81.7k9105134




                81.7k9105134





















                    1














                    This works with any ranges, contiguous or otherwise



                    Sub Test()
                    Dim rng As Range
                    Set rng = Sheet1.Range("A1,B5,E7:E9")

                    Dim v As Variant, a As Range, r As Range

                    ReDim v(1 To 1)
                    For Each a In rng.Areas
                    For Each r In a
                    v(UBound(v)) = r.Value
                    ReDim Preserve v(1 To UBound(v) + 1)
                    Next
                    Next
                    ReDim Preserve v(1 To UBound(v) - 1)

                    Debug.Print v(1)
                    Debug.Print v(2)
                    Debug.Print v(3)

                    End Sub





                    share|improve this answer

























                    • Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

                      – Alexander
                      Mar 24 at 19:08















                    1














                    This works with any ranges, contiguous or otherwise



                    Sub Test()
                    Dim rng As Range
                    Set rng = Sheet1.Range("A1,B5,E7:E9")

                    Dim v As Variant, a As Range, r As Range

                    ReDim v(1 To 1)
                    For Each a In rng.Areas
                    For Each r In a
                    v(UBound(v)) = r.Value
                    ReDim Preserve v(1 To UBound(v) + 1)
                    Next
                    Next
                    ReDim Preserve v(1 To UBound(v) - 1)

                    Debug.Print v(1)
                    Debug.Print v(2)
                    Debug.Print v(3)

                    End Sub





                    share|improve this answer

























                    • Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

                      – Alexander
                      Mar 24 at 19:08













                    1












                    1








                    1







                    This works with any ranges, contiguous or otherwise



                    Sub Test()
                    Dim rng As Range
                    Set rng = Sheet1.Range("A1,B5,E7:E9")

                    Dim v As Variant, a As Range, r As Range

                    ReDim v(1 To 1)
                    For Each a In rng.Areas
                    For Each r In a
                    v(UBound(v)) = r.Value
                    ReDim Preserve v(1 To UBound(v) + 1)
                    Next
                    Next
                    ReDim Preserve v(1 To UBound(v) - 1)

                    Debug.Print v(1)
                    Debug.Print v(2)
                    Debug.Print v(3)

                    End Sub





                    share|improve this answer















                    This works with any ranges, contiguous or otherwise



                    Sub Test()
                    Dim rng As Range
                    Set rng = Sheet1.Range("A1,B5,E7:E9")

                    Dim v As Variant, a As Range, r As Range

                    ReDim v(1 To 1)
                    For Each a In rng.Areas
                    For Each r In a
                    v(UBound(v)) = r.Value
                    ReDim Preserve v(1 To UBound(v) + 1)
                    Next
                    Next
                    ReDim Preserve v(1 To UBound(v) - 1)

                    Debug.Print v(1)
                    Debug.Print v(2)
                    Debug.Print v(3)

                    End Sub






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 24 at 21:29

























                    answered Mar 24 at 18:47









                    CallumDACallumDA

                    10.9k62242




                    10.9k62242












                    • Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

                      – Alexander
                      Mar 24 at 19:08

















                    • Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

                      – Alexander
                      Mar 24 at 19:08
















                    Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

                    – Alexander
                    Mar 24 at 19:08





                    Fortunately for my case, I work with individual cells and not a continuous range. But I'll try this out! Next time it may be just what I need, thanks!

                    – Alexander
                    Mar 24 at 19:08

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326916%2fusing-data-type-range-similar-to-collection-vba%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript