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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,225,767
Messages
6,186,907
Members
453,386
Latest member
testmaster

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