VBA Excel - 2D array data posting a row & column of 0's

Mozzie_Kh

New Member
Joined
Jul 26, 2022
Messages
6
Office Version
  1. 2013
Platform
  1. Windows
I have a 2D array that stores data from a table after running through the calculations.

I have managed to get the data tabulated and am currently trying to paste the data onto a second excel sheet.

I have managed to apply a brute force method however this isn't practical when the sheet has to get updated with more data, I have been using the UBound code to paste the data, but when I do I keep getting an extra column & row 0's before the data is pasted. I am not sure where the data is coming from.

Initial declaration as follows:
Dim Project_Data() As Integer
Set Output = Sheet2

Then below it's updated for data calculation:
ReDim Project_Data(6, Week_Count)


Code to Paste onto excel sheet:
Dim Destination As Range
Set Destination = Output.Range("B20").Resize(UBound(Project_Data, 1) + 1, UBound(Project_Data, 2))
Destination.Value = Project_Data


Output: (the #'s in red shouldn't be there)
00000000000
056689910101412
00000000000
01110000012
00000012333
00000100000
00000011011
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi & welcome to MrExcel.
It would help if you posted all the code, as the problem probably comes from how the array is being populated. How to Post Your VBA Code
 
Upvote 0
Please see full code below:
VBA Code:
Sub Vacation_Project_Filter()
    Dim table As ListObject
    Dim c As Integer, d As Integer
    Dim Vacation_Count As Integer 'count of vacation per week
    Dim Week_Count As Integer, Emp_Count As Integer
    Dim Project_Data() As Integer
    Dim Weekly_Table(1 To 135) As Integer
    
    Set table = Sheet5.ListObjects("Table4")
    Set Output = Sheet2
    Week_Count = Round((table.ListColumns.Count - 7) / 7, 0)
    
    ReDim Project_Data(6, Week_Count) 
    
    c = 1
    d = 7 '# of days per week to add the vacation days
    
    For z = 1 To table.ListRows.Count
        For a = 1 To Week_Count
            For c = c To d
                If table.DataBodyRange(z, c + 7).Value = "V" Or table.DataBodyRange(z, c + 7).Value = "v" Then
                    Vacation_Count = Vacation_Count + 1
                End If
            Next c
             
            If table.DataBodyRange(z, 2).Value = "PSS-H145" Then
                If Vacation_Count > 2 Then
                    Weekly_Table(a) = Weekly_Table(a) + 1
                    Project_Data(1, a) = Project_Data(1, a) + 1
                End If
            ElseIf table.DataBodyRange(z, 2).Value = "THC" Then
                If Vacation_Count > 2 Then
                    Weekly_Table(a) = Weekly_Table(a) + 1
                    Project_Data(2, a) = Project_Data(2, a) + 1
                End If
            ElseIf table.DataBodyRange(z, 2).Value = "RSAF-MLU" Then
                If Vacation_Count > 2 Then
                    Weekly_Table(a) = Weekly_Table(a) + 1
                    Project_Data(3, a) = Project_Data(3, a) + 1
                End If
            ElseIf table.DataBodyRange(z, 2).Value = "RSAF-H215M" Then
                If Vacation_Count > 2 Then
                    Weekly_Table(a) = Weekly_Table(a) + 1
                    Project_Data(4, a) = Project_Data(4, a) + 1
                End If
            ElseIf table.DataBodyRange(z, 2).Value = "RSNF" Then
                If Vacation_Count > 2 Then
                    Weekly_Table(a) = Weekly_Table(a) + 1
                    Project_Data(5, a) = Project_Data(5, a) + 1
                End If
            Else
                If Vacation_Count > 2 Then
                    Weekly_Table(a) = Weekly_Table(a) + 1
                    Project_Data(6, a) = Project_Data(6, a) + 1
                End If
                
            End If
            
            c = c
            d = d + 7
            Vacation_Count = 0
        Next a
            
        c = 1
        d = 7
    Next z
    
    Dim Destination As Range
    Set Destination = Output.Range("B20").Resize(UBound(Project_Data, 1) + 1, UBound(Project_Data, 2)) 'in UBound, 1 = count of columns & 2 = count of rows
    Destination.Value = Project_Data
End Sub
 
Upvote 0
Thanks for that, by default your Project_Data array is base 0 but you are treating it as Base 1, try
Excel Formula:
ReDim Project_Data(1 to 6, 1 to Week_Count)
although this may need further tweaking.
 
Upvote 0
Solution
No that seemed to have worked, thank you very much. I guess by the change it starts the array at 1 whereas before it was starting with 0 as a placeholder.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top