【发布时间】:2016-03-17 13:02:09
【问题描述】:
我有一个工作表,每隔一列填充数字 1-9,从不同的行开始。我编写了以下子代码以在每列中填充单元格右侧的单元格中填充“测试 [数字]”。
我遇到了两个我无法弄清楚的不同问题。之前和之后的示例请参见下面的屏幕截图。
1. H 列的值从第 1 行开始,但代码从第 2 行开始填充
2。生成的“测试 [数字]”单元格均未超过第 9 行
代码:
Sub test()
Dim i As Long
Dim j As Long
Dim c As Long
Dim r As Long
Dim rng As Range
Dim crng As Range
Dim carr() As Variant
Dim rarr() As Variant
With ThisWorkbook.ActiveSheet
c = .Cells(1, 1).SpecialCells(xlCellTypeLastCell).Column
For j = 1 To c
If Not Columns(j).Find(what:="*", after:=Columns(j).Cells(1, 1), LookIn:=xlValues) Is Nothing And Columns(j).Find(what:="test", after:=Columns(j).Cells(1, 1), LookIn:=xlValues) Is Nothing Then
r = Columns(j).Find(what:="*", after:=Columns(j).Cells(1, 1), LookIn:=xlValues).Row
Set rng = .Range(.Cells(r, j), .Cells(.Rows.Count, j).End(xlUp))
rarr = rng.Value
For i = r To UBound(rarr, 1)
Cells(i, j + 1).Value = "test " & (i - r) + 1
Next i
End If
Next j
End With
End Sub
我对在我的代码中使用数组还很陌生,所以如果我的方法关闭,我不会感到惊讶。话虽如此,当rng = .Range(.Cells(r, j), .Cells(.Rows.Count, j).End(xlUp)) 时,UBound(rarr,1) 没有返回最后一个单元格,这让我很难过。
感谢任何帮助或建议。
谢谢
【问题讨论】: