문제

I am writing small code to automate some of my work. I am using Autofill to fill range. Everything works ok as long as the range stays the same. When I change range it shows error 1004: This operation requires the merged cells to be identically sized.

One more thing to add, it shows only when range becomes bigger.

Here is part of my code, when range changes line below 'autofill first column becomes yellow:

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
Dim LastCell As Range, ActiveWS As String

Application.ScreenUpdating = False

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
End With

ActiveWS = ActiveSheet.Name

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

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

j = 7
k = 8
l = 7
m = 8

'activate Barcodes sheet
WSB.Activate

'autofill first column
WSB.Range(Cells(first_r, first_c), Cells(first_r + 1, first_c)).AutoFill _
Destination:=Range(Cells(first_r, first_c), Cells((first_r + 1) + (first_col * 2) - 4, _
first_c)), Type:=xlFillDefault 'filling column G

WSB.Range(Cells(first_r, first_c + 1), Cells(first_r + 1, first_c + 2)).AutoFill _
Destination:=Range(Cells(first_r, first_c + 1), Cells(first_r + 1 + (first_col * 2) - 4, _
first_c + 2)), Type:=xlFillFormats 'filling with columns H:I

'autofill second column
WSB.Range(Cells(second_r, second_c), Cells(second_r + 1, second_c)).AutoFill _
Destination:=Range(Cells(second_r, second_c), Cells(second_r + 1 + (second_col * 2), _
second_c)), Type:=xlFillDefault 'filling column K

WSB.Range(Cells(second_r, second_c + 1), Cells(second_r + 1, second_c + 2)).AutoFill _
Destination:=Range(Cells(second_r, second_c + 1), Cells(second_r + 1 + (second_col * 2), _
second_c + 2)), Type:=xlFillFormats 'filling with columns L:M
도움이 되었습니까?

해결책

Follow up from comments:

As your error message says, you have merged cells with different "shape". Say in first row C1:D1 merged, but in second D2:E2. I suggest you at first make range("C200:D200").UnMerge to unmerge all cells in destination range (except first row).

Something like this should work (just suit your ranges to correct one)

With wbs
    'unmerge all cells
    .Range(.Cells(first_r + 1, first_c), .Cells((first_r + 1) + (first_col * 2) - 4, _
        first_c)).UnMerge

    'autofill first column
    .Range(.Cells(first_r, first_c), .Cells(first_r + 1, first_c)).AutoFill _
    Destination:=.Range(.Cells(first_r, first_c), .Cells((first_r + 1) + (first_col * 2) - 4, _
        first_c)), Type:=xlFillDefault 'filling column G
End With

Btw, one more tip for you: how to avoid using select/active statements

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top