[英]AutoFill macro, error occured when there is merged cell in the column
I have the following code: it works well enough, but when the column gets merged cell it turns out to be in error. 我有以下代码:它工作得很好,但是当列合并单元格时,它证明是错误的。 If there is any way to avoid count for the merged cell or be more specific - to count only data that contain only 7 characters in column B:
如果有任何方法可以避免对合并单元格进行计数或更具体地讲-仅对B列中仅包含7个字符的数据进行计数:
Sub Second()
Range("B15:I15").Select
Selection.AutoFill Destination:=Range("B15:I" & Range("B" & Rows.Count).End(xlUp).Row)
End Sub'
If the merged cell is always at the last row then it is easy. 如果合并的单元格始终位于最后一行,则很容易。 All you need to do is use the
.MergeArea.Count
to check if the last cell is merged. 您只需要使用
.MergeArea.Count
来检查最后一个单元格是否已合并。
Is this what you are trying? 这是您要尝试的吗?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim Lrow As Long
Set ws = ThisWorkbook.Sheets("Sheet2")
With ws
Lrow = .Range("B" & .Rows.Count).End(xlUp).Row
If .Range("B" & Lrow).MergeArea.Count > 1 Then
Lrow = Lrow - 1
End If
.Range("C15:I15").AutoFill Destination:=.Range("C15:I" & Lrow)
End With
End Sub
And if you want to ignore the blank cell above the merged cell then, you can use this 如果要忽略合并单元格上方的空白单元格,则可以使用此
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim Lrow As Long
Set ws = ThisWorkbook.Sheets("Sheet2")
With ws
Lrow = .Range("B" & .Rows.Count).End(xlUp).Row
If .Range("B" & Lrow).MergeArea.Count > 1 Then
If Len(Trim(.Range("B" & Lrow - 1))) = 0 Then
Lrow = .Range("B" & Lrow).End(xlUp).Row
Else
Lrow = Lrow - 1
End If
End If
.Range("C15:I15").AutoFill Destination:=.Range("C15:I" & Lrow)
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.