spencer_time
Board Regular
- Joined
- Sep 19, 2019
- Messages
- 55
- Office Version
- 365
- 2016
- 2007
- Platform
- 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:
This is where I'm at so far in trying to turn this into a function, but it doesn't work:
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):
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
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: