문제

Ok so this is hard to explain, Will be better if I show you.

I'm pulling data from page 2 - Cells A & B

Each time the amount of rows will be different so I'm trying to make a Macro copy the cells from a pre-made box on page one to the end of the row on page 2.

Say on page 2 the rows stop at 25, I only want it to copy 25x on page 1

Here's how it looks, and if I highlight the box's then put the mouse at the bottom right I can drag it down and it copies it for me.. This is what I'm trying to make the macro do.

However, I don't know what to use for my Macro?!

enter image description here

Sub Test()'

    Range("G2:J3").Select
    Range("J3").Activate
    Selection.AutoFill Destination:=Range("G2:J5"), Type:=xlFillDefault
    Range("G2:J5").Select
End Sub
도움이 되었습니까?

해결책

Okay, so starting from LastCell thing, here you got some information about it: http://www.cpearson.com/excel/LastCell.aspx

It was not so easy, because you have merged cells so I had to make a little workaround. First step I am counting number of cells in Worksheets List from your example. Later I am using autofill to fill as many cells as needed - columns G:I and copying formats in the same Cells in column J. Last step is to copy values from column J.

It's kinda strange but it is all thanks to merged cells ;)

Hope it works.

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

Editted code version 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

Edition 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top