﻿{"id":142,"date":"2012-01-25T12:26:21","date_gmt":"2012-01-25T03:26:21","guid":{"rendered":"http:\/\/fujiitoshiki.com\/improvesociety\/?p=142"},"modified":"2014-07-31T13:53:48","modified_gmt":"2014-07-31T04:53:48","slug":"to-change-the-number-of-the-first-element-of-two-dimensional-dynamic-array-excel-vba","status":"publish","type":"post","link":"https:\/\/www.fujiitoshiki.com\/improvesociety\/?p=142","title":{"rendered":"How to change the number of the first element of two-dimensional dynamic array in EXCEL VBA?"},"content":{"rendered":"<div class=\"theContentWrap-ccc\"><p>EXCEL VBA restricts two-dimensional dynamic array to change the number of element in the last dimension only. Although the constraint, what could you do when you would like to change the first dimension of two-dimensional dynamic array? Please see the code below;<\/p>\n<pre class=\"lang:vb decode:true \" >\r\nOption Explicit\r\nSub DynamicArray_Sample()\r\nDim mySht   As Worksheet\r\nDim myRng   As Range\r\nDim tmpAr   As Variant\r\nDim myAr()  As String\r\nDim myID()  As String\r\nDim myStr() As String\r\nDim i       As Long\r\nSet mySht = Worksheets.Add\r\nWith mySht\r\n    .Range(\"A1\").Value = \"10001\"\r\n    .Range(\"A2\").Value = \"10002\"\r\n    .Range(\"A3\").Value = \"10003\"\r\n    .Range(\"A4\").Value = \"10004\"\r\n    .Range(\"A5\").Value = \"10005\"\r\n    .Range(\"B1\").Value = \"aaaaa\"\r\n    .Range(\"B2\").Value = \"bbbbb\"\r\n    .Range(\"B3\").Value = \"ccccc\"\r\n    .Range(\"B4\").Value = \"ddddd\"\r\n    .Range(\"B5\").Value = \"eeeee\"\r\nEnd With\r\nSet myRng = mySht.UsedRange\r\ntmpAr = myRng\r\nFor i = LBound(tmpAr) To UBound(tmpAr)\r\n    ReDim Preserve myID(i)\r\n    ReDim Preserve myStr(i)\r\n    myID(i) = myAr(i, 0)\r\n    myStr(i) = myAr(i, 1)\r\nNext i\r\nReDim Preserve myID(UBound(myID) + 1)\r\nReDim Preserve myStr(UBound(myStr) + 1)\r\nmyID(UBound(myID)) = \"10006\"\r\nmyStr(UBound(myStr)) = \"fffff\"\r\nReDim myAr(UBound(myID), 1)\r\nFor i = LBound(myAr) To UBound(myAr)\r\n    myAr(i, 0) = myID(i)\r\n    myAr(i, 1) = myStr(i)\r\nNext i\r\nSet mySht = Worksheets.Add\r\nWith mySht\r\n    .Range(\"A1:B6\") = myAr\r\nEnd With\r\nSet mySht = Nothing\r\nEnd Sub\r\n<\/pre>\n<p><iframe style=\"width:120px;height:240px;\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&#038;OneJS=1&#038;Operation=GetAdHtml&#038;MarketPlace=US&#038;source=ss&#038;ref=ss_til&#038;ad_type=product_link&#038;tracking_id=improsocie-20&#038;marketplace=amazon&#038;region=US&#038;placement=1118490398&#038;asins=1118490398&#038;linkId=V4BNQHTFMIXIONTS&#038;show_border=true&#038;link_opens_in_new_window=true\"><\/iframe><\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>EXCEL VBA restricts two-dimensional dynamic array to change the number of element in the last dimension only.  &hellip; <a href=\"https:\/\/www.fujiitoshiki.com\/improvesociety\/?p=142\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;How to change the number of the first element of two-dimensional dynamic array in EXCEL VBA?&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":6026,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":8,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":5974,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/5974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=\/wp\/v2\/media\/6026"}],"wp:attachment":[{"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fujiitoshiki.com\/improvesociety\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}