如何在更改列时复制数据

我再次…我有一些代码,从一个特定的列(从表“转换器”)复制单元格,并将其粘贴到不同的列(表“未分配”)。 然后将这些值(ID)用作参考点,将每行(logging)的其余单元格移动到我需要的正确位置。

但是我不能让代码连续复制ID到一个空行,以便他们不覆盖以前的设置。 我认为这是与行Master.Cells(rowB, colB) = yourData但我无法弄清楚。 我试图改变rowB是相同的xlUpfind列中最后一个未使用的单元格(与lastA = Slave.Cells(Rows.Count, colA).End(xlUp).Row ),但我无法得到它上class。 有任何想法吗?

当前代码:

 Private Sub CommandButton21_Click() Dim colA As Integer, colB As Integer Dim rowA As Integer, rowB As Integer Dim Master As Worksheet, Slave As Worksheet 'declare both Application.ScreenUpdating = False Set Master = ThisWorkbook.Worksheets("Unallocated") Set Slave = ThisWorkbook.Worksheets("Convertor") colA = 17 colB = 29 rowA = 1 rowB = 1 lastA = Slave.Cells(Rows.Count, colA).End(xlUp).Row 'This finds the last row of the data of the column FROM which i'm copying For x = rowA To lastA 'Loops through all the rows of A yourData = Cells(x, colA) Master.Cells(rowB, colB) = yourData rowB = rowB + 1 'Increments the current line of destination workbook Next x 'Skips to next row For j = 1 To 5000 '(the master sheet) For i = 1 To 5000 '(the slave sheet) 'for first 1000 cells If Trim(Master.Cells(j, 29).Value2) = vbNullString Then Exit For 'if ID cell is blank exit If Master.Cells(j, 29).Value = Slave.Cells(i, 17).Value Then If IsEmpty(Slave.Cells(i, 3)) Then Exit Sub Master.Cells(j, 2).Value = Slave.Cells(i, 3).Value 'Move all other data based on the ID Master.Cells(j, 8).Value = Slave.Cells(i, 4).Value Master.Cells(j, 9).Value = Slave.Cells(i, 5).Value Master.Cells(j, 10).Value = Slave.Cells(i, 6).Value Master.Cells(j, 11).Value = Slave.Cells(i, 7).Value Master.Cells(j, 12).Value = Slave.Cells(i, 8).Value Master.Cells(j, 13).Value = Slave.Cells(i, 9).Value Master.Cells(j, 4).Value = Slave.Cells(i, 10).Value Master.Cells(j, 23).Value = Slave.Cells(i, 11).Value Master.Cells(j, 24).Value = Slave.Cells(i, 12).Value Master.Cells(j, 25).Value = Slave.Cells(i, 13).Value Master.Cells(j, 26).Value = Slave.Cells(i, 14).Value Master.Cells(j, 27).Value = Slave.Cells(i, 15).Value Master.Cells(j, 28).Value = Slave.Cells(i, 16).Value If Not IsEmpty(Slave.Cells(i, 3)) Then _ Slave.Cells(i, 3).EntireRow.Delete 'deletes row after it has been copied End If Next Next Application.ScreenUpdating = True End Sub 

我们从一个简单的循环开始,为每一行复制数据。 然后你可以添加你的支票。

您可以使用worksheet.range写入单元格(列行),如(“A4”)或(“A”和计数器)。

 Private Sub CommandButton21_Click() Dim ws As Excel.Worksheet Dim wsMaster As Excel.Worksheet Dim strValue As String Set ws = ActiveWorkbook.Sheets("Convertor") Set wsMaster = ActiveWorkbook.Sheets("Unallocated") 'Count of row to read from Dim lRow As Long lRow = 1 'Count of row to write to Dim jRow As Long jRow = 1 ws.Activate 'Loop through and copy what is in the rows Do While lRow <= ws.UsedRange.Rows.count wsMaster.Range("AC" & jRow).Value = ws.Range("Q" & lRow).Value wsMaster.Range("B" & jRow).Value = ws.Range("C" & lRow).Value wsMaster.Range("H" & jRow).Value = ws.Range("D" & lRow).Value wsMaster.Range("I" & jRow).Value = ws.Range("E" & lRow).Value wsMaster.Range("J" & jRow).Value = ws.Range("F" & lRow).Value wsMaster.Range("K" & jRow).Value = ws.Range("G" & lRow).Value wsMaster.Range("L" & jRow).Value = ws.Range("H" & lRow).Value wsMaster.Range("M" & jRow).Value = ws.Range("I" & lRow).Value wsMaster.Range("D" & jRow).Value = ws.Range("J" & lRow).Value wsMaster.Range("W" & jRow).Value = ws.Range("K" & lRow).Value wsMaster.Range("X" & jRow).Value = ws.Range("L" & lRow).Value wsMaster.Range("Y" & jRow).Value = ws.Range("M" & lRow).Value wsMaster.Range("Z" & jRow).Value = ws.Range("N" & lRow).Value wsMaster.Range("AA" & jRow).Value = ws.Range("O" & lRow).Value wsMaster.Range("AB" & jRow).Value = ws.Range("P" & lRow).Value ws.Rows(lRow).EntireRow.Delete 'Increment counters for both sheets. We can actually use just one counter, but if there is ever a condition that will cause us to not copy a row, then we will need two counters. jRow = jRow + 1 'lRow = lRow + 1 'This is commented out because we are deleting rows after we copy them. Loop End Sub 

如果你真的需要删除复制后的行,那么我们将不得不增加lRow值。

细胞正在限制你的方法。

考虑改变使用范围(“A1:C3000”)表示它更强大。

Range.Select Range.Paste(在目的地UsedRows.Count的新标记)

另外,除非你有5000行,这是不正确的,

试验

ActiveSheet.UsedRange.Rows.Count