New to arrays - Array not returning values

Beemerang

Board Regular
Joined
Sep 14, 2011
Messages
114
Hello all,

I am trying to use arrays for the first time and I am flummoxed! Although the array seems to be populating when I write the values to it, any attempt to read those values fail as nothing is returned. Now I KNOW that this is my stupidity and I have checked and rechecked the code that I cobbled together but I simply cannot see where I am going wrong. Any help would be hugely appreciated.

I would like to write the array to a range in Sheet1 but no values are returned and when I test with debug.print it seems that the array contains only 3 or 4 values instead of the dozens that were written to it. Please forgive my messy code!

Code:
'====DECLARE VARIABLES:'Declare Counter variables
'Dim x As Integer
Dim SheetCounter As Integer     'Used to count sheets
Dim MasterCount As Long         'Used to store number of unique keys
Dim RowCounter As Long          'Used to count rows in ranges
Dim TxnCounter As Integer       'Used to count transactions


'Declare Database variables
Dim UniqueKey As String         'Stores unique customer/household id
Dim MonthAdded As String        'Stores the month in which a household becomes a customer


'Declare Range variables
Dim MasterList As Range         'Holds the master list of unique keys
Dim DataRange As Range          'Holds the active range being worked on
Dim RefCell As Range            'Hold the address of the cell being referenced in a data range


'Declare Calculation variables
Dim CalcNew As Integer          'Stored calculation of new customers for a month
Dim CalcRetained As Integer     'Stores calculation of retained customers for a month
Dim KeyResult As Variant


'Declare Arrays:
Dim MasterArray As Variant      'Stores MasterArray list of unique keys
Dim TxnArray As Variant         'Stores TxnArray month's list of transactions
Dim CalcArray As Variant        'Stores results of calculations


'SET APPLICATION STATE:
'Application.ScreenUpdating = False     'To stop screen flicker
'Application.Calculation = xlCalculationManual      'Stop automatic calculation
'MsgBox Sheets.Count & "sheets in workbook"     'For testing purposes only


'INITIALISE CALCULATION ARRAY:
CalcArray = Array("Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Jan 2019", "Feb 2019", "Mar 2019", "Apr 2019", "May 2019", "Jun 2019", "Jul 2019", "Aug 2019")
ReDim CalcArray(Sheets.Count, 2)




'READ MASTER LIST INTO ARRAY:
Sheets("Master List").Activate
Set DataRange = ActiveSheet.Range("A1:B1", Range("B1").End(xlDown))
DataRange.Select
MasterCount = DataRange.Rows.Count
MasterArray = DataRange.Value2


'COMPARE UNIQUE IDs ON MONTHLY TRANSACTION SHEETS TO MASTER LIST:
SheetCounter = Sheets.Count
For Each Sheet In Sheets
    CalcNew = 0
    CalcRetained = 0
    If Sheets(SheetCounter).Name = "Report" Or Sheets(SheetCounter).Name = "LTV" Or Sheets(SheetCounter).Name = "Master List" Or Sheets(SheetCounter).Name = "Sheet1" Then
        'Do nothing for utility sheets
    Else
    'MsgBox "Now processing sheet " & Sheets(SheetCounter).Name
        Sheets(SheetCounter).Activate
        MonthAdded = ActiveSheet.Name
        Set DataRange = ActiveSheet.Range("A2", Range("A2").End(xlDown))
        RowCounter = DataRange.Rows.Count
        TxnCounter = 0
        On Error Resume Next
        
        For Each RefCell In DataRange
            If RefCell.Offset(0, 1) = "Settled Successfully" Then               'If the txn was settled successfully, generate the client key and check if exists
                UniqueKey = RefCell.Offset(0, 27) & RefCell.Offset(0, 30)       'Generate the unique customer key from billing address and zipcode
                'UniqueKey = "TEST DATA"
                
                KeyResult = IsInArray2DIndex(UniqueKey, MasterArray)
                If KeyResult(0) >= 0 And KeyResult(1) >= 0 Then
                    Debug.Print Chr(34) & MasterArray(KeyResult(0), KeyResult(1)) & Chr(34) & " exists in array at row: " & KeyResult(0) & ", col: " & KeyResult(1)
                    CalcRetained = CalcRetained + 1
                Else
                'IF THE GENERATED KEY DOES NOT EXIST IN THE MASTER LIST:
                    Debug.Print UniqueKey & " does not exist in array"
                    'ReDim Preserve MasterArray(MasterCount + 1, 2)           'Expand the existing Master key array to add in the new keys
                    MasterArray(MasterCount, 1) = UniqueKey                     'Write unique key to master list
                    MasterArray(MasterCount, 2) = MonthAdded                    'Write month added to master list
                    CalcNew = CalcNew + 1                                       'Count as new customer/household
                    Debug.Print MasterArray(MasterCount, 1) & " WAS WRITTEN TO ARRAY FOR " & MasterArray(MasterCount, 2) & " AT POSITION " & MasterCount
                End If
                
            End If
            MasterCount = MasterCount + 1
            TxnCounter = TxnCounter + 1                                 'Increment counter
            'ReDim MasterArray(MasterCount, 2)
           '[ Debug.Print MasterArray(MasterCount, 1) & " | " & MasterArray(MasterCount, 2)
            
        Next
        On Error GoTo 0
    End If
    'STORE THE CALCULATED VALUES FOR REPORTS:
    CalcArray(SheetCounter, 0) = MonthAdded
    CalcArray(SheetCounter, 1) = CalcNew
    CalcArray(SheetCounter, 2) = CalcRetained
Next Sheet


'====WRITE THE UPDATED MASTER LIST====


Worksheets("Sheet1").Activate
'OPTION A
'Set Summary = Worksheets("Sheet1").Range("A1").Resize(UBound(MasterArray, 1) + 1)
'Summary = MasterArray
For MasterCount = 1 To UBound(MasterArray, 1)
'MasterCount = RowCounter
'MsgBox MasterArray(RowCounter, 1)


'DEBUG TEST
Debug.Print "Writing to array:" & MasterArray(MasterCount, 1) & MasterArray(MasterCount, 2)


 '   Summary.Cells(RowCounter, 1).Value = MasterArray(RowCounter, 1) & "dddd"
 '   Summary.Cells(RowCounter, 2).Value = MasterArray(RowCounter, 2) & "FFFF"
 '   RowCounter = RowCounter + 1


Next




'OPTION B:
'Worksheets("Sheet1").Range("A1").Resize(UBound(MasterArray, 1) + 1, UBound(MasterArray, 2) + 1).Value = MasterArray


'MsgBox TxnCounter & " RECORDS PROCESSED!" 'Pause during testing to check values


'====WRITE CALCULATED AMOUNTS TO CELLS====
'For x = 1 To MasterCount
 '   Debug.Print MasterArray(x, 1)
'Next


Sheets("Report").Range("c7").Value = CalcArray(11, 1)
Sheets("Report").Range("c8").Value = CalcArray(10, 1)
Sheets("Report").Range("c9").Value = CalcArray(9, 1)
Sheets("Report").Range("c10").Value = CalcArray(8, 1)
Sheets("Report").Range("c11").Value = CalcArray(7, 1)
Sheets("Report").Range("c12").Value = CalcArray(6, 1)
Sheets("Report").Range("c13").Value = CalcArray(5, 1)
Sheets("Report").Range("c14").Value = CalcArray(4, 1)
Sheets("Report").Range("c15").Value = CalcArray(3, 1)
Sheets("Report").Range("c16").Value = CalcArray(2, 1)


'====CLEAN UP====
'RESTORE APPLICATION STATE
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
'====CLEAN UP COMPLETED====
Worksheets("Sheet1").Activate
End Sub

Public Function IsInArray(MasterArray As Variant, UniqueKey As String) As Boolean
    Dim i As Integer, found As Boolean
    found = False


    If Not Len(Join(MasterArray)) > 0 Then
        found = False
    Else
        For i = 0 To UBound(MasterArray)
            If MasterArray(i) = UniqueKey Then
               found = True
            End If
        Next i
    End If
    IsInArray = found
End Function
 
Thank so much! I'll get my head around this and make sure I understand the code you provided before I ask any silly questions. I really appreciate the help,
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
My pleasure
The only "silly question" is the one you don't ask ;)
 
Upvote 0
Hey Fluff

I've pasted your code, changing only the variable names so that I can make sense of them easily:
Code:
Sub Beemerang()   Dim Ws As Worksheet
   Dim MasterArray As Variant, NewCustArray As Variant, RetainedCustArray As Variant
   Dim i As Long
   Dim Dic As Object
   Dim RefCell As Range
   Dim UniqueKey As String
   
   Set Dic = CreateObject("scripting.dictionary")
   ReDim NewCustArray(1 To Sheets.Count, 1 To 2)
   
   With Sheets("Master List")
      MasterArray = .Range("A1", .Range("B" & Rows.Count).End(xlUp)).Value2
   End With
   For i = 1 To UBound(MasterArray)
      Dic.Item(MasterArray(i, 1)) = MasterArray(i, 2)
   Next i
   
   i = 0
   For Each Ws In Worksheets
      If Ws.Name <> "Report" And Ws.Name <> "LTV" And Ws.Name <> "Master List" Then
         i = i + 1
         NewCustArray(i, 1) = Ws.Name
         RetainedCustArray(i, 1) = Ws.Name
         For Each RefCell In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            If RefCell.Offset(, 1).Value = "Settled Successfully" Then
               UniqueKey = RefCell.Offset(, 27).Value & RefCell.Offset(, 30).Value
               If Not Dic.Exists(UniqueKey) Then
                  Dic.Add UniqueKey, Ws.Name
                  NewCustArray(i, 2) = NewCustArray(i, 2) + 1
               Else
                  RetainedCustArray(i, 2) = RetainedCustArray(i, 2) + 1
               End If
            End If
         Next RefCell
      End If
   Next Ws
   Sheets("Master List").Range("A1").Resize(12, 2).Value = NewCustArray
   Sheets("Master List").Range("D1").Resize(12, 2).Value = RetainedCustArray
End Sub

However, I am getting a Runtime error 13 (Type mismatch error on this line:
Code:
RetainedCustArray(i, 1) = Ws.Name

What did I do wrong? :-)
 
Upvote 0
What did I do wrong? :-)
Absolutely nothing :).
Twas me. I forgot to Redim the second array.
You'll need to add this line
Code:
   Set Dic = CreateObject("scripting.dictionary")
   ReDim NewCustArray(1 To Sheets.Count, 1 To 2)
   [COLOR=#ff0000]ReDim RetainedCustArray(1 To Sheets.Count, 1 To 2)[/COLOR]
 
Last edited:
Upvote 0
I'm trying to get my head around the code instead of just bugging you with a million questions but could you please explain what this line does?

NewCustArray(i, 2) = NewCustArray(i, 2) + 1
 
Upvote 0
It simply adds 1 to the 2nd column of the array.
So the first "column" in the array will ave the sheet names & the 2nd will have a count of how many new customers you have on that sheet
 
Upvote 0
Fluff, please forgive me, but I'm running into some limitations caused by my approach, not the code you so kindly provided. I'm trying to describe the problem in plain English so that I can understand it myself and ask for your advice if I have not already imposed on your time too much.

In short, I am trying to calculate new and retained customers for each month for the period Sep 2018 to Aug 2019, based on a number of transactions. A unique customer code is generated for each customer which is the key I'll use to identify them with.
- Transactions for each month are contained on separate sheets e.g. Sep, Oct, Nov etc.
- If a customer bought for the first time in Sep 2018 and again in Nov 2018, they should be counted as a new customer in Sep, and a retained customer in Nov 2018, but not in Oct 2018.
- They should be counted only once as a retained customer in subsequent months, regardless of how many purchases they made in that particular month.
- If a customer buys for the first time in a particular month and makes another purchase in the same month, they should be counted as both a new AND retained customer.

My thinking is as follows (Please guide me if you have the time or inclination):
1.I need to loop through each monthly sheet of transactions starting from Sep 2018 to Aug 2019 and add each unique customer ID to the MasterArray if it does not yet exist in the Master array, and also count the customer as a new customer for that month.
2. If they already exist in the Master array I need to count them as a retained customer for the current month.

I tried to adapt the code you so kindly provided but I stuffed it up completely. Do you think that my approach is sound or should I try this from another angle?

Thanks again for all your help and if you aren't able to spend more time on this I completely understand.

Cheers
Beem
 
Upvote 0

Forum statistics

Threads
1,223,707
Messages
6,174,000
Members
452,542
Latest member
Bricklin

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