Range("A1")
applies to a specific cell on the currently active worksheet. Your error is because you're trying to select a cell on whatever sheet was active when you defined the range (i.e. made the function call) while you're on a different worksheet.
This will work for you:
Function selectingCells(myWs As String, myCell As String)
Sheets(myWs).Activate
Range(myCell).Select
End Function
Sub callingFunction()
Call selectingCells("Data", "A1")
Call selectingCells("Picklist", "A1")
End Sub
A couple of notes:
- Unless this is a smallest case reproducible bit of code, there's no need for
selectingCells
to be a Function
since it doesn't return anything. You'd be better served by making it a Sub
- There's no need for
Call
in callingFunction
. Call
is deprecated and only maintained for backward compatibility
Again, unless there's more to your code than this sample, callingFunction
could be rewritten as:
sub callingFunction()
Sheets("Data").Range("A1").select
Sheets("Picklist").Range("A1").select
end sub
and be much more efficient