Excel 使用VBA合并相邻单元格的代码为';我不再工作了,我可以';我似乎没有发现问题
我试图找出代码的错误,但似乎无法找到。它与其他图纸配合得很好,但对于这一张,它总是暗示有错误,并且结构完全相同 “应用程序定义或对象定义错误”错误显示在代码的Do While行 它在第三排工作,但不是在第四排 以下是该工作表的图像: 有人知道如何修复此错误吗?提前谢谢Excel 使用VBA合并相邻单元格的代码为';我不再工作了,我可以';我似乎没有发现问题,excel,vba,merge,Excel,Vba,Merge,我试图找出代码的错误,但似乎无法找到。它与其他图纸配合得很好,但对于这一张,它总是暗示有错误,并且结构完全相同 “应用程序定义或对象定义错误”错误显示在代码的Do While行 它在第三排工作,但不是在第四排 以下是该工作表的图像: 有人知道如何修复此错误吗?提前谢谢 Sub BPS_MergeCells() Application.DisplayAlerts = False Dim rng As Range Dim LastColumn As Long Di
Sub BPS_MergeCells()
Application.DisplayAlerts = False
Dim rng As Range
Dim LastColumn As Long
Dim ColumnsInRange As Long
Dim r As Long
Dim c As Long
'Last Column Used
LastColumn = ThisWorkbook.Sheets("Blast Pro Series").Cells(3, Columns.Count).End(xlToLeft).Column
'Rows
For r = 3 To 4
'Columns
For c = 1 To LastColumn
Do While ThisWorkbook.Sheets("Blast Pro Series").Cells(r, c) = ThisWorkbook.Sheets("Blast Pro Series").Cells(r, c + 1)
ColumnsInRange = ColumnsInRange + 1
c = c + 1
Loop
Set rng = Range(Cells(r, c - ColumnsInRange), Cells(r, c))
rng.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ColumnsInRange = 0
Next c
Next r
Application.DisplayAlerts = True
End Sub
如注释中所述,您正在更改另一个循环的
for
循环中使用的变量,该变量可能超过最后一列。为此目的创建一个辅助变量,如下所示:
Sub BPS_MergeCells()
Application.DisplayAlerts = False
Dim rng As Range
Dim LastColumn As Long
Dim ColumnsInRange As Long
Dim r As Long
Dim c As Long
Dim c_aux As Long
'Last Column Used
LastColumn = ThisWorkbook.Sheets("Blast Pro Series").Cells(3, Columns.Count).End(xlToLeft).Column
'Rows
For r = 3 To 4
'Columns
For c = 1 To LastColumn
c_aux = c
Do While c_aux < LastColumn And ThisWorkbook.Sheets("Blast Pro Series").Cells(r, c_aux) = ThisWorkbook.Sheets("Blast Pro Series").Cells(r, c_aux + 1)
ColumnsInRange = ColumnsInRange + 1
c_aux = c_aux + 1
Loop
Set rng = Range(Cells(r, c), Cells(r, c_aux))
rng.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ColumnsInRange = 0
Next c
Next r
Application.DisplayAlerts = True
End Sub
Sub BPS\u MergeCells()
Application.DisplayAlerts=False
变暗rng As范围
将最后一列变长
暗柱与长柱一样宽
变暗,变长
尺寸c与长度相同
尺寸c_aux尽可能长
'使用的最后一列
LastColumn=ThisWorkbook.Sheets(“Blast Pro系列”).Cells(3,Columns.Count)。End(xlToLeft)。Column
“行
对于r=3到4
"栏目",
对于c=1到最后一列
c_aux=c
Do While c_aux
如注释中所述,您正在更改另一个循环的for
循环中使用的变量,该变量可能超过最后一列。为此目的创建一个辅助变量,如下所示:
Sub BPS_MergeCells()
Application.DisplayAlerts = False
Dim rng As Range
Dim LastColumn As Long
Dim ColumnsInRange As Long
Dim r As Long
Dim c As Long
Dim c_aux As Long
'Last Column Used
LastColumn = ThisWorkbook.Sheets("Blast Pro Series").Cells(3, Columns.Count).End(xlToLeft).Column
'Rows
For r = 3 To 4
'Columns
For c = 1 To LastColumn
c_aux = c
Do While c_aux < LastColumn And ThisWorkbook.Sheets("Blast Pro Series").Cells(r, c_aux) = ThisWorkbook.Sheets("Blast Pro Series").Cells(r, c_aux + 1)
ColumnsInRange = ColumnsInRange + 1
c_aux = c_aux + 1
Loop
Set rng = Range(Cells(r, c), Cells(r, c_aux))
rng.Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ColumnsInRange = 0
Next c
Next r
Application.DisplayAlerts = True
End Sub
Sub BPS\u MergeCells()
Application.DisplayAlerts=False
变暗rng As范围
将最后一列变长
暗柱与长柱一样宽
变暗,变长
尺寸c与长度相同
尺寸c_aux尽可能长
'使用的最后一列
LastColumn=ThisWorkbook.Sheets(“Blast Pro系列”).Cells(3,Columns.Count)。End(xlToLeft)。Column
“行
对于r=3到4
"栏目",
对于c=1到最后一列
c_aux=c
Do While c_aux
不知道这是否是问题的原因,但您在for
-循环中使用了c
和变量,并且在该循环中,您正在修改它的值(在内部中)。您永远不应该这样做(几乎所有编程语言都禁止这样做)。另外,在While
-循环中,您没有检查c
是否超过LastColumn
。您是对的!感谢您不知道这是否是问题的原因,但您在for
-循环中使用了c
和变量,并且在该循环中,您正在修改它的值(在内部中,而)。您永远不应该这样做(几乎所有编程语言都禁止这样做)。另外,在While
-循环中,您没有检查c
是否超过LastColumn
。您是对的!非常感谢你!成功了,我没想过,谢谢你们!非常感谢你!成功了,我没想过,谢谢你们!