2013-11-20 53 views
0

我是这里的总新手。 尝试以下操作:我已将行中的数据配对,一个在另一个下面,我希望它是另一个的旁边的数据(所以不是A2到FP2和A3到FP3的范围,我希望它是A2到MF2 )。所以,基本上,我需要将每隔一行追加到前一行。在另一行值之后复制行值,之后将整行复制到另一个工作表

我一直试图做一个循环到该行复制,然后切到另一个工作表,使病情保持不变(总是复制行未来3至第2行),但我不能让它复制到第二张纸的新的空闲行中。我已经在调试过程中遇到的各种问题(福尔,如果,列...)

不管怎么说,这是当前的代码,虽然可怕,感谢很多提前!

Sub pairs() 

Application.CutCopyMode = False 
For Each cell In Sheets("Sve").Range("A2") 
     Sheets("Sve").Select 
     Range("A3:FQ3").Select 
     Selection.Cut 
     Range("FR2").Select 
     ActiveSheet.Paste 
     Rows("3:3").Select 
     Selection.Delete Shift:=xlUp 
     Rows("2:2").Select 
     Selection.Cut 
     Sheets("Gotovo").Select 
     sourceCol = 1 
     rowCount = Cells(Rows.Count, sourceCol).End(x1Up).Row 
     For currentRow = 1 To rowCount 
     currentRowValue = Cells(currentRow, sourceCol).Value 
      If IsEmpty(currentRowValue) Or currentRowValue = "" Then 
       Cells(currentRow, sourceCol).Select 
      End If 
     Next 
     ActiveSheet.Paste 
     Sheets("Sve").Select 
Next 

End Sub 

回答

0

我发现,如果您从底部开始并按照自己的方式工作,那么在数据循环中更容易删除行。

Dim myRange As Range 
Dim rowCount As Integer 

' Get the range of cells and the count of cells in the A Column 
Set myRange = Range("A1", Range("A1").End(xlDown)) 
rowCount = myRange.Cells.Count 

' Start loop at the last row and decrease by 2 
For i = rowCount To 2 Step -2 

    ' Get the data in the i-th row, inclusive of blank cells between data 
    Set secondrow = Range(myRange.Cells(i, 1), Cells(i, Columns.Count).End(xlToLeft)) 

    ' place i-th row at the end of the (i-1)th row, inclusive of any 
    ' blank cells b/t data 
    secondrow.Copy Cells(i - 1, Columns.Count).End(xlToLeft).Offset(0, 1) 

    ' Delete the second row 
    myRange.Cells(i, 1).EntireRow.Delete 
Next i 

现在,你有与行的表格式,你想要的方式,您可以更新您的代码以所有这些数据交给你的另一纸上复印。

修订

此代码包括用于与空白数据捕获的行的方法。由于数据的一排可具有空白的细胞(例如A B(空白)d)中,我使用Columns.Count得到行的最后列中,然后使用.End(XlToLeft)方法来获得该行的最后一个非空白单元格。粘贴数据时,只需将列号增加1(即Offset(0,1)),以便不复制该行中最后一个数据单元。

+0

嗯,也许我没有理解错的话做了什么,但我不希望被删除了我的所有其他行 - 我想它上一行数据后复制。当我使用这些代码时,我不明白这一点。 我很抱歉,如果我做错了什么...... – user3013123

+0

示例代码中有你删除第三排它复制到第二排后..? – Jaycal

+0

是的,但请记住,此时该行完全是空的,因为它的数据已被复制。 – user3013123

相关问题