【问题标题】:Using arrays to populate adjacent cells使用数组填充相邻单元格
【发布时间】: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) 没有返回最后一个单元格,这让我很难过。

感谢任何帮助或建议。

谢谢

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    H 列从第 2 行开始,因为您要求 Range.Find method 开始 after:=Columns(j).Cells(1, 1)。我已将其更改为在列中的最后一个单元格之后开始,以便它循环回顶部。

    UBound(rarr,1) 是数组的上边界。 LBoundUBound 函数返回下边界和上边界或数组。它是rng 中的“位置”,而不是工作表上的实际行。

    Sub test()
    
        Dim i As Long
        Dim j As Long
        Dim lc As Long
        Dim lr 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
            lc = .Cells(1, 1).SpecialCells(xlCellTypeLastCell).Column
            lr = .Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row
            For j = 2 To lc Step 2
                If CBool(Application.CountA(.Columns(j))) Then
                    r = .Columns(j).Find(what:="*", after:=.Cells(lr, j), LookIn:=xlValues).Row
                    Set rng = .Range(.Cells(r, j), .Cells(.Rows.Count, j).End(xlUp))
                    rarr = rng.Value
                    For i = LBound(rarr, 1) To UBound(rarr, 1)
                        .Cells(i + (r - 1), j + 1).Value = "test " & i
                    Next i
                End If
            Next j
        End With
    
    End Sub
    

    虽然您已经实现了 With ... End With statement,但它并未在整个代码中用于表示父工作表。每个Range.CellsRange objectsRange.Columns 都必须使用前缀. 才能继承.Parent 工作表property.reference。

    【讨论】:

    • 这正是我想要做的。谢谢你的详细解释
    【解决方案2】:

    作为所有这些常量(数字),我建议让 Excel 为我们工作...

        Option Explicit
    
        Sub test()
    
            With ActiveSheet.Cells.SpecialCells(xlCellTypeConstants).Offset(, 1)
                .FormulaR1C1 = "=CONCATENATE(" & """" & "test" & """" & "&" & "COUNT(R1C[-1]:RC[-1]))"
                .Value = .Value
            End With
    
        End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      相关资源
      最近更新 更多