vba 如何使用宏选择填充单元格的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3652376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:04:39  来源:igfitidea点击:

How to select range of filled cells using macro

excelvba

提问by gizgok

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

我在 sheet3 中有一个按钮。在按钮单击事件中,我正在调用一个宏。在宏中,我想选择在 sheet13 中填充的单元格数。我该怎么做

回答by variant

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

回答by Alen

As Variant says You cannot select cells without changing the focus of your sheet.

正如 Variant 所说,您不能在不更改工作表焦点的情况下选择单元格。

But you can use SpecialCellsto select the cells

但是您可以使用SpecialCells来选择单元格

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 SpecialCellshave aditional info of what can be selected.

SpecialCells有什么可以选择的附加信息的帮助。