What Variable type to use

Exceladd1ct

Board Regular
Joined
Feb 10, 2019
Messages
76
Hello,

This might be a stupid question but what variable type should be to used for lrow_range variable?

The point is to get the worksheet row number according to the last row of the selected range.

Thank you!

VBA Code:
Option Explicit

Sub test_range()
    Dim lrow_range As String 'This works
    'Dim lrow_range As Range 'This gives an error
    
    Dim source As Range
    Dim lrow As Long
    
    Set source = Application.InputBox("Select region", "Get Range", Type:=8)
    
    'Get the last row of the range relative to the worksheet
    lrow_range = source.Cells(source.Rows.Count, source.Columns.Count).Address

    lrow = Range(lrow_range).Row
    
    Debug.Print lrow
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
As long as you are select a single area try
VBA Code:
Sub test_range()
    
    Dim source As Range
    Dim lrow As Long
    
    Set source = Application.InputBox("Select region", "Get Range", Type:=8)
    
    'Get the last row of the range relative to the worksheet
    lrow = Split(source.Address, "$")(4)
    
    Debug.Print lrow
End Sub
 
Upvote 0
Solution
As long as you are select a single area try
VBA Code:
Sub test_range()
   
    Dim source As Range
    Dim lrow As Long
   
    Set source = Application.InputBox("Select region", "Get Range", Type:=8)
   
    'Get the last row of the range relative to the worksheet
    lrow = Split(source.Address, "$")(4)
   
    Debug.Print lrow
End Sub
I didn't know that is possible to split and get an array element in the same statement in a single line of code.

Thanks.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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