在Excel中使用VBA只有在第2行被填充的情况下复制第1行

我只想在B2:BV2被填充的情况下复制A1:AV1中的内容。 我想复制空白而不结束在空白单元格中粘贴选项卡或空格。 作为第二步,我需要将B2:BV2复制到C行,以消除任何空白单元格。 第三步,我需要从行C中取得这些条目,以便只有4个条目填充下面的行D到最后(不超过10行)。 我想出了以下只有部分粘贴(我可以做的最好的)。

Sub Copy() If IsEmpty(Range("A2").Value) = False Then ActiveSheet.Range("A1").Copy Range("A3") End If If IsEmpty(Range("B2").Value) = False Then ActiveSheet.Range("B1").Copy Range("B3") End If If IsEmpty(Range("C2").Value) = False Then ActiveSheet.Range("C1").Copy Range("C3") End If If IsEmpty(Range("D2").Value) = False Then ActiveSheet.Range("D1").Copy Range("D3") End If If IsEmpty(Range("E2").Value) = False Then ActiveSheet.Range("E1").Copy Range("E3") End If Sheet1.Range("a3:Y3").SpecialCells(xlCellTypeConstants).Copy ActiveSheet.Range("A4") End Sub 

这工作,直到AO后,它崩溃了,并没有复制正确的单元格。 我知道这应该做的是数组来的types,但我无法弄清楚循环。

首先制作一个更好的单元格检查和复制循环,这将包括所有你想要的条件:PS。 我的意思是代码中的逻辑不仅仅是我写的代码:

  For I = 1 To Sheet1.Columns.Count If Sheet1.Cells(1, I).Value <> "" and not IsNull(Sheet1.Cells(1, I).Value) Then I2=I2+1 Sheet1.Cells(2, I2).Value=Sheet1.Cells(1, I).Value End if If Sheet1.Cells(2, I2).Value <> "" and not IsNull(Sheet1.Cells(2, I2).Value) Then I3=I3+1 Sheet1.Cells(3, I3).Value=Sheet1.Cells(1, I2-1).Value End if 

在这之后,我认为你可以继续前进。 否则写你面对的。