Trouble with Array not writing to spreadsheet

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
128
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I put together a little macro attempting to total up some values based on what was located in one array and then write the new values (totals) to another part of the spreadsheet. It may not be the prettiest of code, but I believe everything works except I am unable to successfully write the final array to the spreadsheet. In fact, nothing is written and I'm at a loss as to why. If someone could take a look at the following code and identify what I'm missing or doing wrong, I would very much appreciate it.

VBA Code:
Sub Total2()

    Dim array_MaxPlay As Variant
    Dim LastRow As Integer
    Dim Max1 As Integer
    Dim Max2 As Integer
    Dim Max3 As Integer
    Dim MTotal1 As Integer
    Dim MTotal2 As Integer
    Dim MTotal3 As Integer
    Dim MTotal4 As Integer
   
    Sheets("Courts1").Select   ' To populate next 3 variables; only need to read each just once
    Max1 = Range("S3").Value
    Max2 = Range("S4").Value
    Max3 = Range("U4").Value
   
    MTotal1 = 0
    MTotal2 = 0
    MTotal3 = 0
    MTotal4 = 0
   

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' C7:04851 is the MAX size of the array.  The following is used to identify last row actually being used
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    LastRow = Sheets("Courts1").Range("C7:O4851").Find(What:="*", After:=Sheets("Courts1").Range("C7:O4851").Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Based on the information above, able to populate array_MaxPlay
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    array_MaxPlay = Range("C7:O" & LastRow).Value
  
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Counter2 is for Row location.  In this example, LastRow = 76, minus 6 should be a constant and that brings us to 70; the actual range of the array
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    For Counter2 = 1 To (LastRow - 6)
        For Counter1 = 8 To 13          'Columns; 8 - 13 are the locations of the values being examined and tallied
            If array_MaxPlay(Counter2, Counter1) = Max1 Then
                MTotal1 = MTotal1 + 1
            ElseIf array_MaxPlay(Counter2, Counter1) = Max2 Then
                MTotal2 = MTotal2 + 1
            ElseIf array_MaxPlay(Counter2, Counter1) = Max3 Then
                MTotal3 = MTotal3 + 1
            Else: MTotal4 = MTotal4 + 1
            End If
            Next Counter1
       
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Once all columns in a row are examined, store results in the appropriate location in an array called array_MaxPlayT
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ReDim array_MaxPlayT(1 To LastRow, 1 To 4) As Variant

            array_MaxPlayT(Counter2, 1) = MTotal1
            array_MaxPlayT(Counter2, 2) = MTotal2
            array_MaxPlayT(Counter2, 3) = MTotal3
            array_MaxPlayT(Counter2, 4) = MTotal4
           
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Reset the following variables back to ZERO in preparation of examining the next row in the array called array_MaxPlay  (not array_MaxPlayT)
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            MTotal1 = 0
            MTotal2 = 0
            MTotal3 = 0
            MTotal4 = 0
                     
    Next Counter2
       
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Once all rows are examined and tallied, write results in array_MaxPlayT to the following range
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Sheets("Courts1").Range("R7:U" & LastRow) = array_MaxPlayT
   
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This line
VBA Code:
ReDim array_MaxPlayT(1 To LastRow, 1 To 4) As Variant
needs to go before the loops start otherwise you are clearing it all the time.
 
Upvote 0
Solution
This line
VBA Code:
ReDim array_MaxPlayT(1 To LastRow, 1 To 4) As Variant
needs to go before the loops start otherwise you are clearing it all the time.
Thank you!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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