仅当Excel VBA

时间:2016-05-26 17:33:41

标签: excel vba excel-vba

我一直在使用StackOverflow一段时间,只是喜欢这个社区。我知道我希望能找到任何问题的答案。

好的,所以这是我的问题,我一直在这个网站的几个帖子中执行一个脚本的“Frankenstein”。我想在特定标题下复制并粘贴一个Array Formula列,直到没有更多标题。例如,在行F5到W5中,如果有名称,我想复制其下面有数组公式的范围,比如在F156:F323范围内,并将该公式粘贴到G列下的名称中, H列,依此类推,直到该范围之间没有其他名称......

以下是我尝试解决它但我一直收到错误

Dim lastCol As Long
Dim i As Long
Dim ws As Worksheet
Dim Formula As Range

Set ws = Sheets("Main")
lastCol = ws.Range("F" & Columns.Count).End(xlRight).Column
Set Formula = Sheets("Main").Range("F156:F323")

With ws
    For i = 6 To lastCol
    If len(trim.range("F" & i).Value)) <> 0 then _
    .Range(i & 156).formulaarray = 'my formula here'
Next i
End With

发布您可能遇到的任何问题,谢谢!

1 个答案:

答案 0 :(得分:1)

在许多情况下,您正在翻转列和行。

使用范围对象Cells代替Range。它允许使用数字而不是字母的列引用。

直接指定公式。

Dim lastCol As Long
Dim i As Long
Dim ws As Worksheet
Dim Frmla As Range

Set ws = Sheets("Main")
lastCol = ws.Cells(5, ws.Columns.Count).End(xlToLeft).Column
Set Frmla = ws.Range("F156:F323")

With ws
    For i = 6 To lastCol
    If Len(Trim(.Cells(5, i).Value)) <> 0 Then
        .Range(.Cells(156, i), .Cells(323, i)).FormulaR1C1 = Frmla.FormulaR1C1
    End If
Next i
End With