只有在Excel VBA中存在数据(如名称)时才复制并粘贴列

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

好的,这是我的问题,我一直在这个网站上的几个post执行脚本的“科学怪人”。 我想复制并粘贴特定标题下面的数组公式列,直到没有更多的标题。 例如,在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 

发表任何您可能有的问题,谢谢!

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

使用范围对象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