PDA

View Full Version : Count number of cells in worksheet_change



YasserKhalil
11-22-2012, 12:16 PM
Hello
I know my problem is strange.

I want to write a code in Worksheet_Change that count the number of cells to any selection as soon as I copy these cells..

In other words : say I select the range ("A1:B5") then right click and copy.

At this moment I want a message telling me the number of cells selected and copied.

Is that possible?

Bob Phillips
11-22-2012, 01:26 PM
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Cells.Count
End Sub

YasserKhalil
11-23-2012, 09:57 AM
Thanks for your reply
It works fine when entering data in a cell and when use the Paste command.
I don't want that action when entering data.
I just want to do that after copying a range..

Bob Phillips
11-23-2012, 11:35 AM
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CutCopyMode Then MsgBox Target.Cells.Count
Application.CutCopyMode = False
End Sub

YasserKhalil
11-23-2012, 12:15 PM
I appreciate your help Mr. xld But it doesn't work
I want the message to appear immediately after copying and don't cancel the process of copying

YasserKhalil
11-23-2012, 01:16 PM
Is it possible to find out the number of cells after copying or not?

mikerickson
11-23-2012, 08:34 PM
You may not need VBA for this.

I'm using Excel 2011. When I select a range, the number of rows and columns selected shows in the Name box (to the left of the Formula box)

For example
Select C3:E6 and (as long as the mouse is depressed) "4R X 3C" shows in the name box.

If you can multiply the numbers you are likely to encounter in your head, there is no need for a count of cells selected message box.

YasserKhalil
11-24-2012, 02:45 AM
thanks for the information you provided.
I still want it by code

mikerickson
11-24-2012, 09:47 AM
Perhaps something like this, in the sheet's code module will work for you.
If the user selects a range and then copies (or cuts) that range, when the next range is selected, a "row by column" value will be shown in the status bar.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static rngLast As Range

If rngLast Is Nothing Then
Set rngLast = Target
Else
With Application
If .CutCopyMode Then
.StatusBar = rngLast.Rows.Count & "R X " & rngLast.Columns.Count & "C copied"
Else
Application.StatusBar = False
Set rngLast = Target
End If
End With
End If
End Sub
Excel has no OnCopy event, so the message won't show when the user presses copy, but when the next range is selected.

One other bug with this approach is that if the user copies one range, selects another range (data from one range is shown), and doesn't paste or Esc, but copies again, the data from the first range will be continue to be shown.

YasserKhalil
11-24-2012, 10:52 AM
thank you very much for your help Mr. Mikerickson