跳过非空单元格以粘贴特殊数据

我只想在范围(a3:M3)为空的情况下,将工作表“SL”中的范围(a3:M3)中的数据复制到工作表“EL”中的范围(a3:m3) 否则将所选数据复制到下一行(a4:m4)。

下面是我试图解决的代码..但它不工作… PLZ的帮助

Range("C9:G10").Select Selection.Copy Sheets("EL").Select For n = 1 To n = 100 If Cells(n, 2).Value <> "" Then Cells(n + 1, 2).Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False End If Next n 

你的代码中有一些点我不明白:

  • 为什么它指的是C9:G10
  • 为什么有一个循环从n=1 to 100
  • For n = 1 To n = 100的语法不能像您所期望的那样工作 – >将其replace为For n = 1 To 100

这是我对你的问题的解决scheme:

 Sub copyRange() ' Look if destination cells are empty Dim isempty As Boolean isempty = True For Each cell In Sheets("EL").Range("a3:m3").Cells If cell.Value! = "" Then isempty = False Next ' Copy content from SL to EL into the correct line Sheets("SL").Range("a3:m3").Select Selection.Copy If isempty Then Sheets("EL").Range("a3:m3").PasteSpecial Paste:=xlPasteValues Else Sheets("EL").Range("a4:m4").PasteSpecial Paste:=xlPasteValues End If End Sub