Pass variables from sheet to a Sub/Function

stanleytp

New Member
Joined
Apr 12, 2022
Messages
7
Office Version
  1. 2016
Platform
  1. MacOS
Fairly new to all this but I seem to making my way with the assist from Google. Stuck on this one:

Trying to pass the value in a cell on sheet to a Function but get a compile error on the 'Set rng" line.

Sub Q1_button()

Dim rowstart As Integer
Dim rowend As Integer
Dim colstart As Integer
Dim colend As Integer
Dim activesheetstr As String

Dim rng As Range

activesheetstr = Worksheets("FY22Q2").Cells(2, 19)
rowstart = Worksheets("FY22Q2").Cells(3, 19)
rowend = Worksheets("FY22Q2").Cells(4, 19)
colstart = Worksheets("FY22Q2").Cells(5, 19)
colend = Worksheets("FY22Q2").Cells(6, 19)

'activesheetstr = "FY22Q2"
'rowstart = 2
'rowend = 90
'colstart = 6
'colend = 6

Set rng = getData activesheetstr, rowstart, rowend, colstart, colend

rng.Select
Call Update_Rev(rng)

End Sub


Function getData(currentWorksheet As Worksheet, dataStartRow As Integer, dataEndRow As Integer, DataStartCol As Integer, dataEndCol As Integer)

Dim dataTable As Range
Set dataTable = currentWorksheet.Range(currentWorksheet.Cells(dataStartRow, DataStartCol), currentWorksheet.Cells(dataEndRow, dataEndCol))

Set getData = dataTable

End Function
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the Board!

This line:
VBA Code:
Set rng = getData activesheetstr, rowstart, rowend, colstart, colend
should look like this:
VBA Code:
Set rng = getData(activesheetstr, rowstart, rowend, colstart, colend)

But noe that your first argument is problematic.
You declared "activesheetstr" as string like this:
VBA Code:
Dim activesheetstr As String
but your function is looking for a worksheet object, not a string:
Rich (BB code):
Function getData(currentWorksheet As Worksheet, dataStartRow As Integer, dataEndRow As Integer, DataStartCol As Integer, dataEndCol As Integer)
 
Upvote 0
Solution
Excellent!
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

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