ReDim Preserve 2-dimensional Variant ArrayCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Why did the Mercure fail?

Multiplicative persistence

Start making guitar arrangements

Creepy dinosaur pc game identification

Creature in Shazam mid-credits scene?

Yosemite Fire Rings - What to Expect?

Is there a name for this algorithm to calculate the concentration of a mixture of two solutions containing the same solute?

Which one is correct as adjective “protruding” or “protruded”?

Pre-mixing cryogenic fuels and using only one fuel tank

Closed-form expression for certain product

How to explain what's wrong with this application of the chain rule?

Non-trope happy ending?

why `nmap 192.168.1.97` returns less services than `nmap 127.0.0.1`?

Travelling outside the UK without a passport

What if a revenant (monster) gains fire resistance?

Store Credit Card Information in Password Manager?

Open a doc from terminal, but not by its name

Is it possible to put a rectangle as background in the author section?

Are the IPv6 address space and IPv4 address space completely disjoint?

Can I sign legal documents with a smiley face?

GraphicsGrid with a Label for each Column and Row

Drawing ramified coverings with tikz

"Spoil" vs "Ruin"

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?



ReDim Preserve 2-dimensional Variant Array


Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?













1















This seems to be the millionth question on this topic, but searching didn't help me.



I'm trying to resize the last dimension of a two-dimensional array, but always get an "index out of bounds" error in the ReDim Preserve line.



enter image description here



Dim arrCurrentDataset As Variant

For i = 0 To UBound(fileNames) - 1

strPath = fileNames(i)

Set wkbSource = Workbooks.Open(Filename:=strPath, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=True, Notify:=True)
Set wksSource = wkbSource.Sheets(1)

Dim lngRows As Long
lngRows = wksSource.UsedRange.SpecialCells(xlCellTypeLastCell).Row

'Store dataset to array and afterwards increase second dimension by 2 -> create space to add Materialart and Beschaffungsart
arrCurrentDataset = wksSource.Range("A4:I" & lngRows).value
ReDim Preserve arrCurrentDataset(UBound(arrCurrentDataset, 1), UBound(arrCurrentDataset, 2) + 2)

'...

next i


Is there a problem with my declaration? Do I implicitly try to change the data type?



Thanks a lot for your help!










share|improve this question






















  • When filling an array from range, lower bound becomes 1. When you ReDim you should then use ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2), otherwise you are trying to redim first dimension from "1 to n", to "0 to n"

    – drgs
    2 days ago















1















This seems to be the millionth question on this topic, but searching didn't help me.



I'm trying to resize the last dimension of a two-dimensional array, but always get an "index out of bounds" error in the ReDim Preserve line.



enter image description here



Dim arrCurrentDataset As Variant

For i = 0 To UBound(fileNames) - 1

strPath = fileNames(i)

Set wkbSource = Workbooks.Open(Filename:=strPath, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=True, Notify:=True)
Set wksSource = wkbSource.Sheets(1)

Dim lngRows As Long
lngRows = wksSource.UsedRange.SpecialCells(xlCellTypeLastCell).Row

'Store dataset to array and afterwards increase second dimension by 2 -> create space to add Materialart and Beschaffungsart
arrCurrentDataset = wksSource.Range("A4:I" & lngRows).value
ReDim Preserve arrCurrentDataset(UBound(arrCurrentDataset, 1), UBound(arrCurrentDataset, 2) + 2)

'...

next i


Is there a problem with my declaration? Do I implicitly try to change the data type?



Thanks a lot for your help!










share|improve this question






















  • When filling an array from range, lower bound becomes 1. When you ReDim you should then use ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2), otherwise you are trying to redim first dimension from "1 to n", to "0 to n"

    – drgs
    2 days ago













1












1








1








This seems to be the millionth question on this topic, but searching didn't help me.



I'm trying to resize the last dimension of a two-dimensional array, but always get an "index out of bounds" error in the ReDim Preserve line.



enter image description here



Dim arrCurrentDataset As Variant

For i = 0 To UBound(fileNames) - 1

strPath = fileNames(i)

Set wkbSource = Workbooks.Open(Filename:=strPath, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=True, Notify:=True)
Set wksSource = wkbSource.Sheets(1)

Dim lngRows As Long
lngRows = wksSource.UsedRange.SpecialCells(xlCellTypeLastCell).Row

'Store dataset to array and afterwards increase second dimension by 2 -> create space to add Materialart and Beschaffungsart
arrCurrentDataset = wksSource.Range("A4:I" & lngRows).value
ReDim Preserve arrCurrentDataset(UBound(arrCurrentDataset, 1), UBound(arrCurrentDataset, 2) + 2)

'...

next i


Is there a problem with my declaration? Do I implicitly try to change the data type?



Thanks a lot for your help!










share|improve this question














This seems to be the millionth question on this topic, but searching didn't help me.



I'm trying to resize the last dimension of a two-dimensional array, but always get an "index out of bounds" error in the ReDim Preserve line.



enter image description here



Dim arrCurrentDataset As Variant

For i = 0 To UBound(fileNames) - 1

strPath = fileNames(i)

Set wkbSource = Workbooks.Open(Filename:=strPath, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=True, Notify:=True)
Set wksSource = wkbSource.Sheets(1)

Dim lngRows As Long
lngRows = wksSource.UsedRange.SpecialCells(xlCellTypeLastCell).Row

'Store dataset to array and afterwards increase second dimension by 2 -> create space to add Materialart and Beschaffungsart
arrCurrentDataset = wksSource.Range("A4:I" & lngRows).value
ReDim Preserve arrCurrentDataset(UBound(arrCurrentDataset, 1), UBound(arrCurrentDataset, 2) + 2)

'...

next i


Is there a problem with my declaration? Do I implicitly try to change the data type?



Thanks a lot for your help!







arrays excel vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









cr44shcr44sh

206




206












  • When filling an array from range, lower bound becomes 1. When you ReDim you should then use ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2), otherwise you are trying to redim first dimension from "1 to n", to "0 to n"

    – drgs
    2 days ago

















  • When filling an array from range, lower bound becomes 1. When you ReDim you should then use ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2), otherwise you are trying to redim first dimension from "1 to n", to "0 to n"

    – drgs
    2 days ago
















When filling an array from range, lower bound becomes 1. When you ReDim you should then use ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2), otherwise you are trying to redim first dimension from "1 to n", to "0 to n"

– drgs
2 days ago





When filling an array from range, lower bound becomes 1. When you ReDim you should then use ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2), otherwise you are trying to redim first dimension from "1 to n", to "0 to n"

– drgs
2 days ago












2 Answers
2






active

oldest

votes


















2














Your array dimensions are 1 based, but the default is 0 (unless you have an Option Base 1 statement) so you have to specify that in the Redim:



ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)





share|improve this answer


















  • 2





    I like to use LBound to Ubound to avoid problems.

    – user11217663
    2 days ago











  • Excellent! Thank you Rory and drgs.

    – cr44sh
    2 days ago


















0














When you declare a variable as Variant, it can be anything. It may be an array, or an 2-dimensional array, but it could also by an object or an string or something else.



So when you want it to be an array, you have to use a Redim-command. But when you hit your Redim the first time, you don't have an array yet, so Ubound will fail (it's only valid for arrays) - and as it is not an array, there is also nothing to preserve.



You can solve this by either initialize the array before your real work starts, or add a check if the variable contains an array



If isArray(arrCurrentDataset) Then
ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)
Else
' You have to make up your mind about the initial size of the array
ReDim arrCurrentDataset(1 to 10, 1 to 10)
End If





share|improve this answer






















    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%2f55281411%2fredim-preserve-2-dimensional-variant-array%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Your array dimensions are 1 based, but the default is 0 (unless you have an Option Base 1 statement) so you have to specify that in the Redim:



    ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)





    share|improve this answer


















    • 2





      I like to use LBound to Ubound to avoid problems.

      – user11217663
      2 days ago











    • Excellent! Thank you Rory and drgs.

      – cr44sh
      2 days ago















    2














    Your array dimensions are 1 based, but the default is 0 (unless you have an Option Base 1 statement) so you have to specify that in the Redim:



    ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)





    share|improve this answer


















    • 2





      I like to use LBound to Ubound to avoid problems.

      – user11217663
      2 days ago











    • Excellent! Thank you Rory and drgs.

      – cr44sh
      2 days ago













    2












    2








    2







    Your array dimensions are 1 based, but the default is 0 (unless you have an Option Base 1 statement) so you have to specify that in the Redim:



    ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)





    share|improve this answer













    Your array dimensions are 1 based, but the default is 0 (unless you have an Option Base 1 statement) so you have to specify that in the Redim:



    ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    RoryRory

    24.6k51725




    24.6k51725







    • 2





      I like to use LBound to Ubound to avoid problems.

      – user11217663
      2 days ago











    • Excellent! Thank you Rory and drgs.

      – cr44sh
      2 days ago












    • 2





      I like to use LBound to Ubound to avoid problems.

      – user11217663
      2 days ago











    • Excellent! Thank you Rory and drgs.

      – cr44sh
      2 days ago







    2




    2





    I like to use LBound to Ubound to avoid problems.

    – user11217663
    2 days ago





    I like to use LBound to Ubound to avoid problems.

    – user11217663
    2 days ago













    Excellent! Thank you Rory and drgs.

    – cr44sh
    2 days ago





    Excellent! Thank you Rory and drgs.

    – cr44sh
    2 days ago













    0














    When you declare a variable as Variant, it can be anything. It may be an array, or an 2-dimensional array, but it could also by an object or an string or something else.



    So when you want it to be an array, you have to use a Redim-command. But when you hit your Redim the first time, you don't have an array yet, so Ubound will fail (it's only valid for arrays) - and as it is not an array, there is also nothing to preserve.



    You can solve this by either initialize the array before your real work starts, or add a check if the variable contains an array



    If isArray(arrCurrentDataset) Then
    ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)
    Else
    ' You have to make up your mind about the initial size of the array
    ReDim arrCurrentDataset(1 to 10, 1 to 10)
    End If





    share|improve this answer



























      0














      When you declare a variable as Variant, it can be anything. It may be an array, or an 2-dimensional array, but it could also by an object or an string or something else.



      So when you want it to be an array, you have to use a Redim-command. But when you hit your Redim the first time, you don't have an array yet, so Ubound will fail (it's only valid for arrays) - and as it is not an array, there is also nothing to preserve.



      You can solve this by either initialize the array before your real work starts, or add a check if the variable contains an array



      If isArray(arrCurrentDataset) Then
      ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)
      Else
      ' You have to make up your mind about the initial size of the array
      ReDim arrCurrentDataset(1 to 10, 1 to 10)
      End If





      share|improve this answer

























        0












        0








        0







        When you declare a variable as Variant, it can be anything. It may be an array, or an 2-dimensional array, but it could also by an object or an string or something else.



        So when you want it to be an array, you have to use a Redim-command. But when you hit your Redim the first time, you don't have an array yet, so Ubound will fail (it's only valid for arrays) - and as it is not an array, there is also nothing to preserve.



        You can solve this by either initialize the array before your real work starts, or add a check if the variable contains an array



        If isArray(arrCurrentDataset) Then
        ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)
        Else
        ' You have to make up your mind about the initial size of the array
        ReDim arrCurrentDataset(1 to 10, 1 to 10)
        End If





        share|improve this answer













        When you declare a variable as Variant, it can be anything. It may be an array, or an 2-dimensional array, but it could also by an object or an string or something else.



        So when you want it to be an array, you have to use a Redim-command. But when you hit your Redim the first time, you don't have an array yet, so Ubound will fail (it's only valid for arrays) - and as it is not an array, there is also nothing to preserve.



        You can solve this by either initialize the array before your real work starts, or add a check if the variable contains an array



        If isArray(arrCurrentDataset) Then
        ReDim Preserve arrCurrentDataset(1 to UBound(arrCurrentDataset, 1), 1 to UBound(arrCurrentDataset, 2) + 2)
        Else
        ' You have to make up your mind about the initial size of the array
        ReDim arrCurrentDataset(1 to 10, 1 to 10)
        End If






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        FunThomasFunThomas

        5,2611626




        5,2611626



























            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%2f55281411%2fredim-preserve-2-dimensional-variant-array%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현