Dynamically Select Cells For Use in a VBA Function

Dynamically Select Cells For Use in a VBA Function

I've written the function below that I would like to use dynamically in my excel sheet where the function would assess the value of the variable selected in the formula and based on that value, perform a simple calculation referencing values in cells in different columns but in the same row.

I've written the function below that I would like to use dynamically in my excel sheet where the function would assess the value of the variable selected in the formula and based on that value, perform a simple calculation referencing values in cells in different columns but in the same row.

I know that the code below works, but how can I rewrite the ranges so that my function can be used dynamically for each row of the same column?

Function AddedValue(TabSize As Integer)

Select Case TabSize

Case 2
    AddedValue = Range("K3") * (Range("N3") * (Range("H3") * 0.001))

Case 4
    AddedValue = Range("K3") * (Range("O3") * (Range("H3") * 0.001))

Case 6
    AddedValue = Range("K3") * (Range("P3") * (Range("H3") * 0.001))

Case 8
    AddedValue = Range("K3") * (Range("Q3") * (Range("H3") * 0.001))

Case 10
    AddedValue = Range("K3") * (Range("R3") * (Range("H3") * 0.001))

End Select End Function


excel excel-vba

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Excel VBA Tutorial for Beginners 17- Delete Cells using VBA in MS Excel

In this MS Excel VBA video, we are going to see that how can we delete cells in excel using the VB code in the VBA editor and the Overview of formulas in Excel. We will see Basic Excel formulas & functions with examples .

Excel VBA Tutorial for Beginners 16 - Clear Cells in Excel using VBA

In thi MS Excel VBA video, we are going to look at how can we use the VB code to clear up cells in MS Excel. The clear cells feature of MS Excel is used for the purpose. Using this feature we can easily clear cell contents, cell formatting, cell comments and hyperlinks as well

Excel VBA Tutorial for Beginners 20 - Column Width Manipulation in Excel VBA

In this MS Excel VBA video, we are going to see how can we use the VB code to manipulate the Column Width of Cells in MS Excel. So we are going to use two methods and explore them in detail, along with this we aare also going to see that what are the possible values for the width of columns

Excel VBA Tutorial for Beginners 10 - Background Colors in Excel VBA

In this Excel VBA video, we are going to see the usage of With Block in Excel VBA. Using with block, we can reuse and rewrite multiple code lines. Also we are going to look at the interior property in brief as well, which allows us to set background colors and background gradient as well

Excel VBA Tutorial for Beginners 11 - Excel VBA PasteSpecial Method

In this Excel VBA video, we are going to look at the usage of the Paste Special function in Excel using the VB code. Paste Special function helps us to use the paste function in different formats such as Pasting Cell Width, Pasting Text only, Pasting Cell Formats.