Trouble using index/match on add-in worksheets

kylemd

New Member
Joined
Aug 23, 2017
Messages
4
Hi there! Apologies in advance for typos and rambling.

So I am currently working on a tax formula as a spare time project. The function takes the year and income figure, and calculates tax based on a few variables.

I have stored these variables in separate worksheets, each named as per what year they pertain to. The idea was that I could pass on the year the user input as the sheet name, and retrieve data from that sheet.

However, I'm having some issues doing that. The debugger stops at the line I have bolded, with no further information as to what error has occurred.

Here's what I have so far (snippet obviously) - I adapted @AlphaFrog's response here as it was a bit cleaner than index match.

Code:
'Income taxFunction IncTax(Year, TaxableIncome)


    Dim lb As Double        'Lower bracket
    Dim ls As Double        'Lump sum
    Dim r As Single         'Rate


'Get relevant values from worksheets
    With ThisWorkbook.Worksheets(Year)
[B]        lb = .Range("A" & Application.Match(TaxableIncome, Range("B:B"), -1))[/B]
    End With
    
    With ThisWorkbook.Worksheets(Year)
        ls = .Range("C" & Application.Match(TaxableIncome, Range("B:B"), -1))
    End With
    
    With ThisWorkbook.Worksheets(Year)
        r = .Range("D" & Application.Match(TaxableIncome, Range("B:B"), -1))
    End With
    
'Perform final calculation
    IncTax = (ls + ((TaxableIncome - lb) * r))


End Function

Where am I going wrong? I have a feeling it's got something to do with the way I'm referencing the sheets, but not certain.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try Application.WorksheetFunction.Match not Application.Match...

Good luck,

CN.

Thanks for your quick reply CodeNinja!

I had tried Application.WorksheetFunction.Match before posting, which yielded the same result. With breakpoints on each match line, the code reaches the first one and halts.

I've made the change nonetheless though. I've also combined all three With statements - not sure how I missed that!

Code:
'Income tax
Function IncTax(Year, TaxableIncome)


    Dim lb As Double        'Lower bracket
    Dim ls As Double        'Lump sum
    Dim r As Single         'Rate


'Get relevant values from worksheets
    With ThisWorkbook.Worksheets(Year)
        lb = .Range("A" & Application.WorksheetFunction.Match(TaxableIncome, Range("B:B"), -1))
        ls = .Range("C" & Application.WorksheetFunction.Match(TaxableIncome, Range("B:B"), -1))
        r = .Range("D" & Application.WorksheetFunction.Match(TaxableIncome, Range("B:B"), -1))
    End With
    
'Perform final calculation
    IncTax = (ls + ((TaxableIncome - lb) * r))


End Function

Just in case below are some screenshots of the relevant worksheets also.


JlHD-lfDhdIcsH6Xt_-4YekThqTQtv2mOXCPxn4WlqL1cwqQSJgXRyYx0kjasrZSFYdE08HTisJrJeRDktFpAYWek5rQFlM9Q3rLgY1_p9m96NbXK0C7bhoqzJauqin7zxH32YKcruwkcd75uYwBCKzuCpj76oE378qLxTeFuo38R8OlalZ0F84Gzh9xJYr5pptoG9NEfcj9PHo22NxVB53bXuIGlwhcSUzXXfyDI_afZHegZJ_mQwbMzqXqYC753qGkHEHhI0R51l6mekonQDDbXoOSZDEliLUHkXM31fzJQsdbMtnMykPaiysJM4t7jhiOuSKoq8nSI7I50hSjzyqYYm2eUhCGYbaNN-EXlUMoTsuXMckdqgptrxq21hbjbtf5deIbltrNr3sleZAncMP99HaEeQToe5J4LvEMt46LcNjQxUptZiYSPJtytvc9X8VYVgRoPGzP5kZX71rUu2btApErRUiixvglNIDrtPZ7E7vUDfOeXVdrdiB6JcHskcD7OLYEV8xfeN0ChrB2MkMTD6Jr7B2gFugfaoNy2YibNjf7JQ19QECxN0SXHXlgGiJYron8eZU24DMJXjVUyIcPNdOuklviTXzhy1-9tnc200QOVwNViveXeQD2QaEgWvrZbjdSQHduIztN2ldY-cUjf6QI-I-POUgtcqWuvaWebA=w238-h158-no-tmp.jpg

tXYYQzmbMtRUG2L97
Sd6I6QDx9QSNZITz_WYRIY7Ep-z5tKXSuxXw-xcpiIxsHZP9mCxEdQtCvTV14R4aWQSa19f5jfSPUTJUCDniBOmwPbZ9mXyqTxVzzqrXAg8EbfZY5sfY4-G6jo5EEy7ZHAKdR1sEmOXfhnOv_9kj1SmTsHE--bKel3LzvktqgmuPaau0LfhrQGCn44vdJ8ZQ1g13lZVPo8PkNOn5UxNfTwZzC2RP6n9hN5yKgYIsUgknTvWlHFK9xWXLe60gWqtsm2BvhrekNMrFDtVYRk_wy0lR3l8flpei2MuodqFjE_YaE_dWlVH8MQElUH2hrwa1811SNFssrXcp9xyUAxDUNtEakZEVqXjquvrxpJbLG6DiyBgrSLOUkwbtgbL-1yjM4M_cXMcfIkI0Zi1Jv4tpgT-G1fINyi2YFQ3eA5ElbG48mzpvFMKNnXdKPEC8Y5NVesistfYDQJynfBLcSRFENkkR05YeQfUwUdyC2l9rR_oMb1FwO3uC5KcZtqj0IFcOqcoQz_lF9DF7En_IaXZcaAoNcWF1DkEoKEpWjLKqgc32n0Oghzk7ScHWxGo_5JwJdr5MMjRWjbTXppceQNhZSrRbTYcaORMOuLLp3MLtG54pLvntBTfqoG__CXDkL_h5MtUlVjH9ud_qPpz-o9KBB1XTZ_BdGXwni6pKL51ICP8GCA=w471-h181-no-tmp.jpg
 
Upvote 0
All the ranges in your Match functions will refer to the active sheet, not ThisWorkbook.Worksheets(Year).

Try this.
Code:
'Income tax

Function IncTax(Year, TaxableIncome)


    Dim lb As Variant        'Lower bracket
    Dim ls As Variant        'Lump sum
    Dim r As Variant         'Rate


'Get relevant values from worksheets
    With ThisWorkbook.Worksheets(Year)
        lb = .Range("A" & Application.Match(TaxableIncome, .Range("B:B"), -1))
    End With
    
    With ThisWorkbook.Worksheets(Year)
        ls = .Range("C" & Application.Match(TaxableIncome, .Range("B:B"), -1))
    End With
    
    With ThisWorkbook.Worksheets(Year)
        r = .Range("D" & Application.Match(TaxableIncome, .Range("B:B"), -1))
    End With
    
'Perform final calculation
    IncTax = (ls + ((TaxableIncome - lb) * r))

End Function
Or perhaps something like this.
Code:
Function IncTax(Year, TaxableIncome)
Dim lb As Variant        'Lower bracket
Dim ls As Variant        'Lump sum
Dim r As Variant         'Rate
Dim Res As Variant

    With ThisWorkbook.Worksheets(Year)
    
        Res = Application.Match(TaxableIncome, .Range("B:B"), -1)

        If Not IsError(Res) Then

            'Get relevant values from worksheets
            lb = .Range("A" & Res).Value
            ls = .Range("C" & Res).Value
            r = .Range("D" & Res).Value

            'Perform final calculation
            IncTax = (ls + ((TaxableIncome - lb) * r))
        Else
            IncTax = CVErr(xlErrNA)
        End If
        
    End With
    
End Function
 
Upvote 0
All the ranges in your Match functions will refer to the active sheet, not ThisWorkbook.Worksheets(Year).

Hi @Norie! You are correct - I was missing the "." in front of each Range. Thanks so much! :biggrin:

Everything is working as intended now, there was a few other things I needed to change:


  • Passing the variable Year as the worksheet name didn't work. I had to convert it to string y first.
  • As I was using a greater than match formula the data set needed to be sorted largest to smallest.

Here is the final code I've ended up with. Thanks again to @CodeNinja and @Norie for your help!

Code:
'Income tax
Function IncTax(Year, TaxableIncome)


'Variables for calculation
    Dim lb As Variant                       'Lower bracket
    Dim ls As Variant                       'Lump sum
    Dim r As Variant                        'Rate
    
'Variables to run query
    Dim y As String: y = Year               'Sheet name string


'Get relevant values from worksheets
    With ThisWorkbook.Worksheets(y)
    
        Res = Application.WorksheetFunction.Match(TaxableIncome, .Range("B:B"), -1)
        
        If Not IsError(Res) Then
            lb = .Range("A" & Res).Value
            ls = .Range("C" & Res).Value
            r = .Range("D" & Res).Value
        
        'Perform final calculation
            IncTax = (ls + ((TaxableIncome - lb) * r))
            
        Else
            IncTax = CVErr(xlErrNA)
        End If
        
    End With


End Function
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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