【问题标题】:Copy from page 2 to last cell从第 2 页复制到最后一个单元格
【发布时间】:2014-03-10 11:54:43
【问题描述】:

好的,这很难解释,如果我告诉你会更好。

我正在从第 2 页提取数据 - 单元格 A 和 B

每次行的数量都会不同,所以我尝试将单元格从第一页的预制框中复制到第二页的行尾。

假设在第 2 页上的行停止在 25,我只希望它在第 1 页上复制 25 倍

这是它的外观,如果我突出显示该框,然后将鼠标放在右下角,我可以向下拖动它并为我复制它。这就是我试图让宏做的事情。

但是,我不知道宏要使用什么?!

Sub Test()'

    Range("G2:J3").Select
    Range("J3").Activate
    Selection.AutoFill Destination:=Range("G2:J5"), Type:=xlFillDefault
    Range("G2:J5").Select
End Sub

【问题讨论】:

  • 嗯...我很困惑。您想使用自动填充填充一定数量的单元格,对吗?如果是这样,您从 Selection.Autofill 开始的代码就可以完成这项工作。那你的问题是什么?
  • @lowak 我有两页,1) 列表 - 单元格 A 和 B 2) 带有条形码(上图)我将数据从 2 拉到 1。我想要一个宏这将复制条形码框(如图),直到第 1 页没有更多数据,所以我没有任何空白框......?
  • 两页,你是说床单吗?
  • @lowak 是的,抱歉,请查看我的编辑。
  • 所以我还是不知道你的问题是什么?是从一张纸复制到另一张纸吗?

标签: vba excel autofill


【解决方案1】:

好的,那么从 LastCell 开始,这里有一些关于它的信息:http://www.cpearson.com/excel/LastCell.aspx

这并不容易,因为您已经合并了单元格,所以我不得不做一些解决方法。第一步,我从您的示例中计算 Worksheets List 中的单元格数。后来我使用自动填充来填充所需数量的单元格 - G:I 列并在 J 列的相同单元格中复制格式。最后一步是从 J 列复制值。

这有点奇怪,但这全归功于合并的单元格;)

希望它有效。

Sub counting()

Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long

Set WS = Worksheets("List") 'your worksheet name
With WS
    Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
    LastCellRowNumber = LastCell.Row
End With

Worksheets("Barcodes").Range(Cells(2, 7), Cells(3, 9)).AutoFill _
Destination:=Range(Cells(5, 7), Cells(6 + (LastCellRowNumber * 2) - 4, 9)), Type:=xlFillDefault 'filling columns from G to I
Worksheets("Barcodes").Range(Cells(2, 10), Cells(3, 10)).AutoFill _
Destination:=Range(Cells(5, 10), Cells(6 + (LastCellRowNumber * 2) - 4, 10)), Type:=xlFillFormats ' filling with format J column

j = 4
k = 5

For i = 6 To LastCellRowNumber 'filling values in column J

    Cells(j, 10).Value = "=List!A" & i
    Cells(k, 10).Value = "=List!B" & i

    j = j + 2
    k = k + 2

Next

End Sub

编辑代码版本 2:

Sub counting()

Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long

Set WS = Worksheets("List") 'your worksheet name
With WS
    Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
    LastCellRowNumber = LastCell.Row
End With

Worksheets("Barcodes").Range(Cells(5, 7), Cells(6, 7)).AutoFill _
Destination:=Range(Cells(5, 7), Cells(6 + (LastCellRowNumber * 2) - 4, 7)), Type:=xlFillDefault 'filling column G

Worksheets("Barcodes").Range(Cells(5, 8), Cells(6, 9)).AutoFill _
Destination:=Range(Cells(5, 8), Cells(6 + (LastCellRowNumber * 2) - 4, 9)), Type:=xlFillFormats 'filling with columns H:J

j = 7
k = 8

For i = 3 To LastCellRowNumber 'copying values in columns I, J

    Cells(j, 9).Value = "=List!A" & i
    Cells(j, 8).Value = Cells(j - 2, 8).Value
    Cells(k, 9).Value = "=List!B" & i
    Cells(k, 8).Value = Cells(k - 2, 8).Value

    j = j + 2
    k = k + 2

Next

End Sub

版本 v3:

Sub auto_copy()

Dim WSL As Worksheet, WSB As Worksheet
Dim first_col As Long, second_col As Long
Dim first_r As Byte, first_c As Byte
Dim second_r As Byte, second_c As Byte
Dim LastCellRowNumber As Long, comeback As String
Dim LastCell As Range, ActiveWS As String

Application.ScreenUpdating = False

ActiveWS = ActiveSheet.Name

Set WSB = Worksheets("Barcodes") 'your worksheet name
Set WSL = Worksheets("List") 'your worksheet name
With WSL
    Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
    LastCellRowNumber = LastCell.Row - 3
End With

first_col = Round(LastCellRowNumber / 2)
second_col = LastCellRowNumber - first_col

first_r = 5 'position of "first column" row
first_c = 7 'position of "first column" column
second_c = 11 'position of "first column" column

WSB.Activate

comeback = ActiveCell.Address

For i = 1 To LastCellRowNumber

    If Application.IsOdd(i) = True Then

    WSB.Range(Cells(first_r, first_c), Cells(first_r + 1, first_c)).Copy
    WSB.Range(Cells(first_r + 2, first_c), Cells(first_r + 1 + 2, first_c)).PasteSpecial
    WSB.Range(Cells(first_r, first_c + 1), Cells(first_r + 1, first_c + 2)).Copy
    WSB.Range(Cells(first_r + 2, first_c + 1), Cells(first_r + 1 + 2, first_c + 2)).PasteSpecial

    Else

    WSB.Range(Cells(first_r, second_c), Cells(first_r + 1, second_c)).Copy
    WSB.Range(Cells(first_r + 2, second_c), Cells(first_r + 1 + 2, second_c)).PasteSpecial
    WSB.Range(Cells(first_r, second_c + 1), Cells(first_r + 1, second_c + 2)).Copy
    WSB.Range(Cells(first_r + 2, second_c + 1), Cells(first_r + 1 + 2, second_c + 2)).PasteSpecial

    first_r = first_r + 2

    End If

Next

Range(comeback).Activate

Worksheets(ActiveWS).Activate

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

【讨论】:

  • 我在代码的中间位收到错误“此操作要求合并单元格的大小相同”:(
  • 调试时哪个部分是黄色的?
  • Worksheets("Barcodes").Range(Cells(2, 7), Cells(3, 9)).AutoFill _ Destination:=Range(Cells(2, 7), Cells(3 + (LastCellRowNumber * 2) - 4, 9)), Type:=xlFillDefault '从 G 到 I 填充列——我想我知道为什么了。在我发送给您的示例模板上,我对其进行了一些更改。 “条形码”不再出现在 G2 中,现在出现在 G5 中
  • 嗯...两件事。 1. 一列适合多少条码? 2. 如果你有更多条码超过 1 张纸,那怎么办?
  • @Jacko058 我试图解决这个问题有点不同,你可以试试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多