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;
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
add a comment |
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
add a comment |
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
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
vba
asked Mar 24 at 18:10
AlexanderAlexander
265
265
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 24 at 18:14
paulsm4paulsm4
81.7k9105134
81.7k9105134
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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