turn sub into function with arguments

spencer_time

Board Regular
Joined
Sep 19, 2019
Messages
55
Office Version
  1. 365
  2. 2016
  3. 2007
Platform
  1. Windows
Hello all, I have some code that works in a sub, but I need to use it several times over the course of my spreadsheets timeline. It currently only works on the active sheet, which I also need to change to be the sheet/variable that is called for in the argument when calling the function.

This is the sub that works on an active sheet:
Code:
Sub count()
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = ActiveSheet
    Dim numRow As Long
    Dim numCol As Long
    Dim nr As Long
    Dim nc As Long
    nr = 1 'n for row
    nc = 1 'n for column

Do While ws.Cells(Rows.count, nr).End(xlUp).row <= 1 'keep looping until number of rows counted is larger than 1
nr = nr + 1
Loop
Do While ws.Cells(nc, Columns.count).End(xlToLeft).Column <= 1 'keep looping until number of columns counted is larger than 1
nc = nc + 1
Loop

numRow = ws.Cells(Rows.count, nr).End(xlUp).row
numCol = ws.Cells(nc, Columns.count).End(xlToLeft).Column

MsgBox "Rows = " & numRow
MsgBox "Columns = " & numCol
MsgBox numRow & " rows and " & numCol & " columns"
MsgBox "Sum of number of rows and number of columns = " & (numRow + numCol)

End Sub

This is where I'm at so far in trying to turn this into a function, but it doesn't work:
Code:
Public Function cntRowCol(wb As Workbook, ws As Worksheet)

    Public Dim numRow As Long
    Public Dim numCol As Long
    Dim nr As Long
    Dim nc As Long
    nr = 1 'n for row
    nc = 1 'n for column

Do While wb.ws.Cells(Rows.count, nr).End(xlUp).row <= 1
nr = nr + 1
Loop
Do While wb.ws.Cells(nc, Columns.count).End(xlToLeft).Column <= 1
nc = nc + 1
Loop

numRow = wb.ws.Cells(Rows.count, nr).End(xlUp).row
numCol = wb.ws.Cells(nc, Columns.count).End(xlToLeft).Column

End Function

I need the function to be run on the workbook and worksheet called for in the arguments, and the variables numRow and numCol to be globally accessible so I can use them in other sub's as well as them be updated each time this function is ran.

I anticipate this being able to be implemented as something similar to the following (to get results that match the original sub):

Code:
Sub callFun()

    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = ActiveSheet
    
    Call cntRowCol(wb, ws)
    
    MsgBox "Rows = " & numRow
    MsgBox "Columns = " & numCol
    MsgBox numRow & " rows and " & numCol & " columns"
    MsgBox "Sum of number of rows and number of columns = " & (numRow + numCol)

End Sub

I tried to explain what I'm trying to do as clear as I could, if further explanation is needed, let me know and I will expand on my needs.

Any suggestions as to how to make this work are appreciated.

EDIT 09:42: TYPO
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi, does this code replicate what you are trying to do?

Code:
Public Function cntRow(ws As Worksheet) As Long
    cntRow = ws.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
End Function


Public Function cntCol(ws As Worksheet) As Long
    cntCol = ws.Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
End Function


Sub CallFun()


Dim numRow As Long
Dim numCol As Long
Dim wb As Workbook
Dim ws As Worksheet


Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet


numRow = cntRow(ws)
numCol = cntCol(ws)


MsgBox "Rows = " & numRow
MsgBox "Columns = " & numCol
MsgBox numRow & " rows and " & numCol & " columns"
MsgBox "Sum of number of rows and number of columns = " & (numRow + numCol)


End Sub
 
Upvote 0
Solution
Hi, does this code replicate what you are trying to do?

Yes FormR, it works great.

Thank you for your help. All you guys on this forum have been so helpful as I have been working on my big spreadsheet (and consequently as I learn to use excel with VBA).

EDIT 10:37: I don't really understand your version too well, but it does work and that is the important thing right now. I will get to where I will understand things better in time.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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