VBA onchange - Troubleshoot

ouvay

Board Regular
Joined
Jun 9, 2022
Messages
131
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I've got this onchange bit of code I'm working on and its basically a simple index match
unfortunately, it doesn't work as 'subscription is out of range'

Couldn't anyone tell me where I'm going wrong?

TIA


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim WorkRng As Range
Dim Rng As Range

Set WorkRng = Intersect(Application.ActiveSheet.Range("C6:C5000"), Target)
If Not WorkRng Is Nothing Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, -1).Value = calcSlip(Rng.Value)
        Else
            Rng.Offset(0, -1).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If

End Sub

Function calcSlip(ID As Long) As Long

lastRow = SalesList.Cells(Rows.Count, 1).End(xlUp).Row
Dim a() As Variant
ReDim a(1 To lastRow, 1 To 16) As Variant
a = SalesList.ListObjects(1).DataBodyRange

For i = 2 To lastRow
    'Debug.Print a(i, 3)
    If a(i, 3) = ID Then
        calcSlip = a(i, 2)
    End If
Next i

End Function
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
VBA Code:
Function calcSlip(ID As Long) As Long
lastRow = SalesList.Cells(Rows.Count, 1).End(xlUp).Row
'Dim a() As Variant
Dim a
'ReDim a(1 To lastRow, 1 To 16) As Variant
'a = SalesList.ListObjects(1).DataBodyRange
a = SalesList.ListObjects(1).DataBodyRange.value
For i = 2 To lastRow
    'Debug.Print a(i, 3)
    If a(i, 3) = ID Then
        calcSlip = a(i, 2)
    End If
Next i
End Function
 
Upvote 0
VBA Code:
Function calcSlip(ID As Long) As Long
lastRow = SalesList.Cells(Rows.Count, 1).End(xlUp).Row
'Dim a() As Variant
Dim a
'ReDim a(1 To lastRow, 1 To 16) As Variant
'a = SalesList.ListObjects(1).DataBodyRange
a = SalesList.ListObjects(1).DataBodyRange.value
For i = 2 To lastRow
   
    If a(i, 3) = ID Then
        calcSlip = a(i, 2)
    End If
Next i
End Function
if a(i,3) = ID Then --- line still reads as an error
 
Upvote 0
Is the "SalesList" and ActiveSheet.Range("C6:C5000") in the same sheet?
Do they intersect?
 
Upvote 0
Which if statement ?
Try
For Each Rng In WorkRng.Cells
the if statement in the function

VBA Code:
For i = 1 To lastRow
    'Debug.Print a(i, 3)
    If a(i, 3) = ID Then      <---------------------
        calcSlip = a(i, 2)
    End If
Next i
 
Upvote 0
so I tried my code again using ranges instead of arrays and it works .. so I guess the problem lies with using an array? would anyone be able to explain why?
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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