Which Excel Data Type Allows me to Select a Cell as a Reference?

KhanofTarkir

New Member
Joined
Sep 3, 2014
Messages
24
Hello,

In the following function, is it possible to change the "valRow As Long" and "valCol As Long" argument placeholders in the function to a data type that allows me to select a specific cell (such as C16)? I realize that the current code allows me to select a cell, but I would like to apply dollaring so that I can have relative references when I copy and paste.

Code:
Function ADDACROSSSHEETS(valRow As Long, valCol As Long, sheetStart As Integer, sheetEnd As Integer) As Variant
 
 
    For x = sheetStart To sheetEnd
        ADDACROSSSHEETS = Sheets(x).Cells(valRow, valCol).Value + ADDACROSSSHEETS

    Next x
 

End Function

Example:

I want to write in the function that would show up as ADDACROSSSHEETS(C16, 1, 2) instead of ADDACROSSSHEETS(16, 3, 1, 2). This is because I would like to apply dollaring so that I can copy/paste relative references so that ADDACROSSSHEETS(C16, 1, 2) would turn into ADDACROSSSHEETS(D16, 1, 2) in the right adjacent cell and so on. Is this possible?

Thank you!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You would pass a Range object. You can then retrieve its address to use in the code itself. Note that you will have to make this function volatile since it uses indirect references to cells.
 
Upvote 0
Thanks for the response - would you or someone show me explicitly how to do that in the code? I only just started learning VBA and I haven't worked with Range objects.

Thanks!
 
Upvote 0
Code:
Function ADDACROSSSHEETS(rng As Range) As Variant

    valRow = rng.Row
    valCol = rng.Column


    For x = 1 To Sheets.Count
        ADDACROSSSHEETS = Sheets(x).Cells(valRow, valCol).Value + ADDACROSSSHEETS
    Next x


End Function

That's the original code I wrote that passes rng as a range.....

Code:
Function ADDACROSSSHEETS(rng As Range, sheetStart as integer, sheetEnd as integer) As Variant

    valRow = rng.Row
    valCol = rng.Column


    For x = sheetStart To sheetEnd
        ADDACROSSSHEETS = Sheets(x).Cells(valRow, valCol).Value + ADDACROSSSHEETS
    Next x


End Function
 
Upvote 0
You would add this line (assuming that you're using this in a cell):
Code:
application.volatile true
 
Upvote 0
Ah I see you already anticipated that I would have to use a range object NeonRedSharpie..

Thanks a lot for your help and the original code :)
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,239
Members
448,879
Latest member
VanGirl

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