Need to save a range of cells into a variable and make the count show up in a MsgBox

Klash Ville

Board Regular
Joined
Sep 19, 2017
Messages
83
title says it all, here's what I have now, but for some reason, it's not working when it should...

Code:
    Range("A3").Select
    ActiveCell.Offset(1, 0).Activate


    Dim tbl As Range
    Set tbl = Range(Selection, Selection.End(xlDown)).Select
    
    MsgBox tbl.Rows.Count
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Remove the "Select" from your Set statement, i.e.
Code:
    Set tbl = Range(Selection, Selection.End(xlDown))

Also, what is the purpose of this?
Code:
    Range("A3").Select
    ActiveCell.Offset(1, 0).Activate
That could just be simplified to:
Code:
    Range("A4").Select

Actually, your whole code block could be simplified to this:
Code:
    Dim tbl As Range
    Set tbl = Range(Range("A4"), Range("A4").End(xlDown))
    
    MsgBox tbl.Rows.Count
 
Last edited:
Upvote 0
Thanks, I adapted most of your simplified code into the already existing one, and it worked just like I wanted.

Here's the final result in case you are curious:

Code:
    Sheets("Feuil1").Select    Cells.Find(What:="Nom", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(1, 0).Activate
    Dim tbl1 As Range
    Set tbl1 = Range(Selection, Selection.End(xlDown))
    
    Sheets("Country").Select
    Cells.Find(What:="Nom", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(1, 0).Activate
    Dim tbl2 As Range
    Set tbl2 = Range(Selection, Selection.End(xlDown))
    
    If tbl1.Rows.Count <> tbl2.Rows.Count Then
        MsgBox ("Based on the mutual column 'Nom' in the sheets 'Feuil1' and 'Country', the total number of rows is NOT equal..." & vbNewLine & vbNewLine & _
        "It has been detected " & tbl1.Rows.Count & " row(s) in the sheet 'Feuil1'" & vbNewLine & _
        "It has been detected " & tbl2.Rows.Count & " row(s) in the sheet 'Country'"), vbCritical
        
        Exit Sub
        End If

Also, is there a smaller and simplified way in Excel VBA for it to select a cell based on a certain real, integer or string value rather than using the entire Search feature?
 
Last edited:
Upvote 0
Also, is there a smaller and simplified way in Excel VBA for it to select a cell based on a certain real, integer or string value rather than using the entire Search feature?
Not that I know of. You could loop through all cells to look for it, but that is a very inefficient way of doing it (loops, by nature, are inefficient).
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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