每次单击按钮时都能向下选择下一个单元格 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43266204/
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
Be able to select next cell down every time button is clicked Excel VBA
提问by Kyle Marvin
Below is the code that I have so far I am able to click the button and every time the button is clicked the cell selection is moved down the row by 1. What I need is to start the selection on F3 and select down until about F35 but when I range it doesn't select the cells one by one.
下面是到目前为止我能够单击按钮的代码,每次单击按钮时,单元格选择都会向下移动 1 行。我需要的是在 F3 上开始选择并向下选择直到大约 F35但是当我选择范围时,它不会一一选择单元格。
Here is my code:
这是我的代码:
Dim rng As Range
Dim row As Range
Dim cell As Range
Set rng = Range("F2")
rng.Select
For Each row In rng.Rows
For Each cell In row.Cells
ActiveCell.Offset(1, 0).Select
Next cell
Range("G66") = ActiveCell
Next row
采纳答案by user3598756
if you have a Form
button called Button1
then attach it a sub called Button1_Click()
(or whatever, but be consistent with the name of the attached Sub) and place the following code in any module:
如果您有一个Form
按钮调用,Button1
则将其附加一个子调用Button1_Click()
(或其他任何名称,但与附加子的名称保持一致)并将以下代码放在任何模块中:
Option Explicit
Dim notFirst As Boolean
Dim rng As Range
Sub Button1_Click()
If notFirst Then
If rng.row = 35 Then
MsgBox "Sorry: you've already reached last valid cell in column F"
Exit Sub
Else
Set rng = rng.Offset(1)
End If
Else
Set rng = Range("F3")
notFirst = True
End If
Range("G66").Value = rng.Value
End Sub
if you have a ActiveX
button called Button1
then write the same code as above in its sheet code pane
如果您有一个ActiveX
按钮,Button1
则在其工作表代码窗格中编写与上述相同的代码