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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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