VBA coding to count SELECTED rows and columns

Poorcoding

New Member
Joined
Feb 11, 2020
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hi
HELP NEEDED. I'm trying to create a macro that counts selected rows and columns and display in msgbox the total number of selected columns and rows. Here is my coding, but its running an error, see below:

Sub range_reporter()
Dim r As Range
Dim s As String
Dim nLastRow As Long, nLastColumn As Long
Dim nFirstRow As Long, nFirstColumn As Long

Set r = Selection

numrow = r.Rows.Count
MsgBox ("number of rows " & numrow)

numcol = r.Columns.Count
MsgBox ("number of columns " & numcol)


End Sub



1581433568843.png
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Add this above Set r
VBA Code:
dim numrow as long
Dim numcol As Long

Also, you can remove

VBA Code:
Dim nLastRow As Long, nLastColumn As Long

Dim nFirstRow As Long, nFirstColumn As Long

Dim s as String
 
Upvote 0
Thank you this worked

Sub range_reporter()
Dim r As Range
Dim s As String
Dim nLastRow As Long, nLastColumn As Long
Dim nFirstRow As Long, nFirstColumn As Long


Set r = Selection

Dim numrow As Long

numrow = r.Rows.Count
MsgBox ("number of rows " & numrow)

Dim numcol As Long

numcol = r.Columns.Count
MsgBox ("number of columns " & numcol)
 
Upvote 0
VBA Code:
Sub range_reporter()

Dim r As Range

Dim numrow As Long

Dim numcol As Long

Set r = Selection

numrow = r.Rows.Count

numcol = r.Columns.Count

MsgBox ("number of rows: " & numrow & "number of columns: " & numcol)

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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