Error in assigning Array value

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
393
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I want to record the values of income and their respective month and day values into 3 arrays, i.e. income(), income_m() and income_d(). But there is an error message of "mismatching". I don't know what it means exactly and would like to seek your help.

-----------------------------

Col B: date (in the format of YYYY/MM/DD)
Col D: amount of income


The outlook of my worksheet looks like this ( only col B and D are displayed below )

BD
1DateIncome
22015/1/14
32015/2/23
42015/3/1710,000
52015/4/2320,000
62015/5/2915,000

<tbody>
</tbody>


My code:

Code:
If Application.WorksheetFunction.CountA(Range("D:D")) > 1 Then
    
        i = Range("D1").End(xlDown).Row
        j = 1
        
        Do While i <> Rows.Count + 1
        
[COLOR=#ff0000]                income(j) = Range("D" & i).Value    ' <--- Error Here, but why?[/COLOR]
                income_m(j) = Month(Range("D" & i).Offset(0, -2).Value)
                income_d(j) = Day(Range("D" & i).Offset(0, -2).Value)
                i = i + 1
                j = j + 1
                
        Loop

End If

Thanks!
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You need Redim or Redim Preserve.
And, I think it will be eternity loop.

Do While i <> Rows.Count + 1

i= number of last row of colimnD.
Rows.Count + 1 =1048576 +1...?
The " i " will never same as "Rows.Count + 1".
 
Upvote 0
First,

Do you mean to find the last row in column D or do you want the count of the number of income items?
Code:
If Application.WorksheetFunction.CountA(Range("D:D")) > 1 Then

With continuous data in column D you will get the LAST row in column D that contains a value
Code:
i = Range("D1").End(xlDown).Row

Rows.Count is giving you the total number of rows in the worksheet (1,048,576). Is that what you want?
Code:
Do While i <> Rows.Count + 1

All that being said, the error you are getting is because you did not size the array.

The code below will fill the arrays you are looking to fill

Code:
Sub Test2()    Dim income As Variant
    Dim income_m As Variant
    Dim income_d As Variant
    Dim i As Integer
    Dim j As Integer
    Dim r As Integer
    
    i = Cells(Rows.Count, "B").End(xlUp).Row
    
    ReDim income(i - 2)
    ReDim income_m(i - 2)
    ReDim income_d(i - 2)
    
    For j = 0 To i - 2
        income(j) = Cells(j + 2, 4).Value
        income_m(j) = Month(Cells(j + 2, 2).Value)
        income_d(j) = Day(Cells(j + 2, 2).Value)
    Next
End Sub
 
Upvote 0
If Application.WorksheetFunction.CountA(Range("D:D")) > 1 Then

i = Range("D1").End(xlDown).Row
j = 0
Dim income() As String
Dim income_m() As String
Dim income_d() As String
Do Until j + 1 > i
ReDim Preserve income(j)
ReDim Preserve income_m(j)
ReDim Preserve income_d(j)
income(j) = Range("D" & j + 1).Value    ' <--- Error Here, but why?
income_m(j) = Month(Range("D" & j + 1).Offset(0, -2).Value)
income_d(j) = Day(Range("D" & j + 1).Offset(0, -2).Value)
j = j + 1

Loop

End If
 
Last edited:
Upvote 0
Thank you all. I manage to figure out where my problem lies.

It works out well perfectly now. Thank you! :)

----------------------------------

My new code now:


If Application.WorksheetFunction.CountA(Range("D:D")) > 1 Then

i = Range("D1").End(xlDown).Row
j = Range("D" & Rows.Count).End(xlUp).Row
n = 1

ReDim Preserve income(j - i + 1)
ReDim Preserve income_m(j - i + 1)
ReDim Preserve income_d(j - i + 1)

Do While i <> j + 1
income(n) = Range("D" & i).Value
income_m(n) = Month(Range("B" & i).Value)
income_d(n) = Day(Range("B" & i).Value)
i = i + 1
n = n + 1
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

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