Return value from function

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
Hi

How do I return a value to my sub procedure from a function?

I created two functions to get the last row and column of a worksheet. However, after the function is ran, it resets to zero. How do I save the function amount?

Code:
Option Explicit
Option Base 1
Dim colNm As String
Dim rowNum As Integer
Sub bu_Mapping()

Sheets("Write Sheet").Activate

Sheets("1. Voting Copy").Activate

    colNm = "A": rowNum = 1
    
    wColLastRow (colNm)
    
    wRowLastColNum (rowNum)

End Sub

Function wColLastRow(colNm As String) As Integer
    wColLastRow = Range(colNm & Rows.Count).End(xlUp).Row
End Function

Function wRowLastColNum(rowNum) As Integer
    wRowLastColNum = Range(colNm & rowNum).End(xlToRight).Column
End Function
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You need to assign the function result to another variable. Your sub doesn't do anything with the information so it's not really clear what you want, but your row function should return a Long, not Integer, and the lastColNum function needs to take the colNm as an argument as well.
 
Upvote 0
Hello Certified,

If you want to save to results to the worksheet, say A1 and B1, you would do this...

Code:
Option Explicit
Option Base 1
Dim colNm As String
Dim rowNum As Integer
Sub bu_Mapping()


Sheets("Write Sheet").Activate


Sheets("1. Voting Copy").Activate


    colNm = "A": rowNum = 1
    
    Range("A1").Value = wColLastRow (colNm)
    
    Range("B1").Value = wRowLastColNum (rowNum)


End Sub


Function wColLastRow(colNm As String) As Integer
    wColLastRow = Range(colNm & Rows.Count).End(xlUp).Row
End Function


Function wRowLastColNum(rowNum) As Integer
    wRowLastColNum = Range(colNm & rowNum).End(xlToRight).Column
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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