2017-05-07 58 views
1

僅當B2:BV2被填充時,我想複製A1:AV1中的內容。我想複製空白而不結束粘貼空白單元格中的選項卡或空格。作爲第二步,我需要將B2:BV2複製到C行以消除任何空白單元格。第三步,我需要從行C中取得這些條目,以便只有4個條目填充下面的行D到最後(不超過10行)。 我想出了以下僅部分粘貼(我可以做的最好的)。在excel中使用vba只有在第2行被填充的情況下才複製第1行

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之後,它崩潰了,並沒有複製正確的單元格。我知道這應該做的是數組的來,但我無法弄清楚循環。

回答

1

首先製作一個更好的單元格檢查和複製循環,像這樣將包含所有您想要的條件: 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 

。否則寫你面對的。

相關問題