+ Reply to Thread
Results 1 to 4 of 4

VBA - problem copying data from storage array to sheet array

Hybrid View

  1. #1
    Registered User
    Join Date
    08-11-2020
    Location
    Uk
    MS-Off Ver
    365
    Posts
    2

    VBA - problem copying data from storage array to sheet array

    I am trying to replicate what a Data Table does in excel in VBA. I have got the code working as I want thus far however when I copy data out of the temporary storage array it is offset by 1 Column and 1 Row.

    I cannot figure out what the issue is? Thanks in advance.
    Sub DataTableLoop()
    
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    
    Dim CodeRng As Range
    Dim PasteRng As Range
    Dim WatchRng As Range
    Dim ResultRng As Range
    Dim ResultRes As Range
    Dim x As Integer
    Dim y As Integer
    Dim i As Integer
    Dim Count As Integer
    Dim col As Integer
    Dim MyArray As Variant
    Dim TempArr As Variant
    Dim CodeVar As Range
    
    Set CodeRng = Worksheets("OptionCodes").[CodeTop]
    Set PasteRng = Worksheets("OptionCodes").[OptionsCode]
    Set WatchRng = Worksheets("OptionCodes").[WatchRange]
    Set ResultRng = Worksheets("OptionCodes").[ResultsRange]
    
    col = WatchRng.Columns.Count
    
    x = Worksheets("OptionCodes").[Iterations].Value
    y = x - 1
    i = 0
    
    Set ResultRes = ResultRng.Resize(x)
    
    ReDim MyArray(x, col)
    
    Do While i <= y
    
    Set CodeVar = CodeRng.Offset(i, 0)
    
    Count = i + 1
    
    Application.StatusBar = "Iteration: " & Count & " of " & x
    
        CodeVar.Copy
        PasteRng.PasteSpecial Paste:=xlPasteValues
    
            Application.Calculate
        
        TempArr = WatchRng
        For j = 1 To col
            MyArray(Count, j) = TempArr(1, j)
        Next j
        
    i = i + 1
    
    Loop
    
    ResultRes = MyArray
    
    Application.ScreenUpdating = True
    
    Application.Calculation = xlCalculationAutomatic
    
    End Sub

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: VBA - problem copying data from storage array to sheet array

    Hi there,

    It's late here, so I'm not claiming to have thought this one through fully, but what goes through my mind is that the statement:

    
    ReDim MyArray(x, col)
    creates a zero-based array,

    while the statement:
    
    TempArr = WatchRng
    creates a one-based array.

    This might account for your "one row off, one column off" situation.


    Hope this helps.

    Regards,

    Greg M



    P. S. To make the two arrays consistent you can use:

    
    ReDim MyArray(1 To x, 1 To col)
    Last edited by Greg M; 08-11-2020 at 08:29 PM. Reason: P. S. added

  3. #3
    Registered User
    Join Date
    08-11-2020
    Location
    Uk
    MS-Off Ver
    365
    Posts
    2

    Re: VBA - problem copying data from storage array to sheet array

    That worked perfectly! Thank you!

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: VBA - problem copying data from storage array to sheet array

    Hi again,

    Many thanks for your feedback.

    You're welcome - glad I was able to help.

    Regards,

    Greg M

+ 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. [SOLVED] Filter Array 1 with Array 2 as criteria, returning filtered data to source sheet
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-31-2019, 01:08 PM
  2. [SOLVED] Copying an array formula down keeps copying data in top row
    By blackrosepetals9169 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-10-2018, 05:15 AM
  3. Replies: 1
    Last Post: 09-01-2014, 05:27 PM
  4. Array Storage Problem: Array Elements Deleted on 'End' Command
    By AidenS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2014, 10:38 AM
  5. Problem copying array values to worksheet cells
    By CoolGal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2008, 02:00 AM
  6. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2005, 01:54 AM
  7. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 12:30 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