我正在尝试创建一个以Range(A9)和Ends in Range(A38)max开头的循环计数器。上面的细胞" A9"及以下" A38"有文字,是锁定的单元格。我在这个网站上找到了一些代码,并尝试对其进行修改以满足我的需求,但我得到了一个("运行时错误' 91':对象变量或With block变量未设置& #34)。不确定这究竟意味着什么。这是我的代码。
Private Sub cmdsGO_Click()
Dim myWrkBk As Workbook
Dim mySheet As Worksheet
Dim startCell As Range
Dim lastRow As Range
Dim i As Long
Set myWrkBk = ActiveWorkbook
Set mySheet = myWrkBk.Sheets("Sheet1")
Set startCell = Range("'Sheet1'! A9")
lastRow = Range("'Sheet1'!A38")
While i <= lastRow
i = i + 1
Wend
End Sub
我使用命令按钮启动循环。范围(&#34; A9&#34;)到范围(&#34; A38&#34;)是30个数字。所以,当然,计数器应该从1开始到30结束但不是。谢谢。
我找到此代码的原始位置是: Loop to start at a cell and delete the next 600 in the column
答案 0 :(得分:0)
尝试数据系列。
Option Explicit
Private Sub cmdsGO_Click()
Dim myWrkBk As Workbook
Dim mySheet As Worksheet
Set myWrkBk = ActiveWorkbook
Set mySheet = myWrkBk.Sheets("Sheet1")
With mySheet
.Cells(9, "A") = 1
.Range(.Cells(9, "A"), .Cells(38, "A")).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
End With
End Sub
由于范围对象(例如,lastRow)首先从Range(&#34;&#39; Sheet1&#39;!A38&#34;)分配了一个值,因此您自己的代码无法正常工作设置为范围对象,然后在For Next循环中用作整数。