How to select range of filled cells using macro
I have a button in sheet3.On the button click event I'm calling a macro.In the macro I want to select the number of cells that are filled in sheet13.开发者_开发知识库How do I do this
You cannot select cells without changing the focus of your sheet.
Sheets("sheet13").Activate
ActiveSheet.UsedRange.Select
You can, however, apply changes or read data from another sheet without changing focus.
Sheets("sheet13").UsedRange.Font.Bold = True
Msgbox Sheets("sheet13").UsedRange.Cells.Count
As Variant says You cannot select cells without changing the focus of your sheet.
But you can use SpecialCells
to select the cells
Sub tester()
Dim x1 As Range
Dim x2 As Range
Dim bigRange As Range
Sheets("sheet2").Select 'the page you need
Range("E9").Select ' any select will do
Selection.SpecialCells(xlCellTypeFormulas, 23).Select 'select numbers, text, etc.
Set x1 = Selection
Range("E9").Select ' any select will do
Selection.SpecialCells(xlCellTypeConstants, 23).Select 'select formulas
Set x2 = Selection
Set bigRange = Application.Union(x1, x2) 'join both ranges
bigRange.Select
Sheets("sheet1").Select 'return to the page with the button
End Sub
the help of SpecialCells
have aditional info of what can be selected.
精彩评论