+ Reply to Thread
Results 1 to 10 of 10

Writing VBA array to Excel. Result ends up displaced by one column.

  1. #1
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Writing VBA array to Excel. Result ends up displaced by one column.

    Hi,

    I've solved this problem before but for the life of me can't remember what I did.

    The final line of code in a procedure which writes a VBA array back to Excel is:

    Please Login or Register  to view this content.
    the arOutData 2nd dimension i.e. the 'columns' is 6

    When this is written to A1 on Sheet2 all 6 columns are output but starting in B1, all the data I expect is there.
    There is no Option Base line of code so I understand by default this is Base 1 but in any case the output is correct, it's just put in the wrong column
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  2. #2
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    818

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Probably the first element will be empty.

    Please Login or Register  to view this content.
    Kind regards, Harry.

  3. #3
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Why do you have +1 ? I tried with a sample data & your line of code & the results get into A1 correctly with #N/A in the last column due to the +1

    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Quote Originally Posted by nankw83 View Post
    Why do you have +1 ? I tried with a sample data & your line of code & the results get into A1 correctly with #N/A in the last column due to the +1

    Please Login or Register  to view this content.
    Hi,

    The array is a 6 'column' array. I found that without the +1 it only populated 5 columns B:F

  5. #5
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    As far as I know, your code should work as expected without the +1 but I guess the +1 is just acting as a work around solution to your issue of not placing the data from cell A1 ... Perhaps if you could post your whole code someone can spot the issue

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Hi, please examine these code :

    - Create two modules

    - Code on Module1 (please note, there is no the "Option Base 1" line on this module) :
    Please Login or Register  to view this content.
    - Code on Module2 :
    Please Login or Register  to view this content.

    You probably define the array using the same syntax as the one in Sub Test1_Without_OptionBase1(), that is :
    - without the "Option Base 1" line code
    - Dim arOutData(3, 6) --> without specifying the lbound for the dimension, so VBA will automatically take this value as the Option Base default value (in this case = 0, because not explicitly specified)
    - so the above syntax is actually --> Dim arOutData(0 To 3, 0 To 6)

    that's why the output is "shifting" one column to right, while actually you have one extra "hidden column", in this case column(0)
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  7. #7
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    818

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Please Login or Register  to view this content.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Quote Originally Posted by HSV View Post
    Probably the first element will be empty.

    Please Login or Register  to view this content.
    Hi,

    Mmm,
    Thanks for the suggestion. When I try
    Please Login or Register  to view this content.
    that of course give a subscript out of range since the original '1' indicates the first (rows) dimension. The '2' in the second part indicates the 2nd (columns) dimension.

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    Thanks karedog,

    It is indeed the Option Base 1 that was causing the problem.
    In this procedure I don't specifically define the array in the way you show, rather I simply read the range into the array with

    Please Login or Register  to view this content.
    where lRows is the last row of data. Not sure whether that makes any difference.
    Once I'd added the Option Base 1 and removed the +1 from the second dimension that cured the anomaly.
    I was always under the impression that the default is Base 1 when reading a range into an array - clearly I was mistaken.

    Please Login or Register  to view this content.
    Thanks once again - and also to the other contributors.

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Writing VBA array to Excel. Result ends up displaced by one column.

    You are welcome Richard, and many thanks for rep.points given.

    Regards

+ 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. Excel VBA Compare two array and create result array
    By Peter Pak in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-29-2019, 10:34 AM
  2. [SOLVED] Adding line tot able & Writing to excel sheet through Array
    By LaFlibuste in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-04-2018, 07:47 PM
  3. Result the header cell if the when the latest 2 set ends
    By Noman050 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-22-2018, 05:25 AM
  4. Cells displaced in excel spreadsheets
    By jnbspace in forum Excel General
    Replies: 6
    Last Post: 04-15-2017, 11:16 AM
  5. [SOLVED] Check if string ends with one of the strings from an array
    By alienss in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-15-2013, 02:58 PM
  6. writing an array with more than one row in vba excel...
    By siba in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-29-2012, 06:18 AM
  7. (TABLE) Help writing formula to display column header of a result
    By scarch05 in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-15-2011, 04:40 AM

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