我正在将BO的报告提取到excel中,导出会导致合并的单元格。 BO报告是日期驱动器列的宽度变化,所以今天合并的单元格,明天不匹配合并的单元格,这就是为什么硬编码单元格不是一个选项。
我有一段代码可以成功取消合并列,但结果是我需要删除的重复列。我不断得到一个没有定义方法对象的错误。
Private Sub Workbook_Open()
Dim rows_used As Integer
Dim i As Integer
Dim j As Integer
Dim ws_1 As Workbook
rows_used = ActiveSheet.UsedRange.Rows.Count
For i = 1 To 35
For j = 1 To rows_used
If Cells(j, i).MergeCells Then
Cells(j, i).UnMerge
Else
Exit For
End If
Next j
Next i
For i = 1 To 35
k = 0
Do
If Range(Cells(1, i)).Value = Range(Cells(1, i + 1)).Value Then
Range(Cells(1, i + 1)).EntireColumn.Delete
k = k + 1
End If
Loop Until k = 3
Next i
End Sub
我已经厌倦了在没有Cells(1,i)
标记的情况下使用Range()
,但也失败了。
UnMerge之前的列
UnMerge之后的列
答案 0 :(得分:0)
第二个循环中的范围参考未正确格式化。这对我有用
Private Sub Workbook_Open()
Dim rows_used As Integer
Dim i As Integer
Dim j As Integer
Dim ws_1 As Workbook
rows_used = ActiveSheet.UsedRange.Rows.Count
For i = 1 To 35
For j = 1 To rows_used
If Cells(j, i).MergeCells Then
Cells(j, i).UnMerge
Else
Exit For
End If
Next j
Next i
For i = 1 To 35
k = 0
Do
If Cells(1, i).Value = Cells(1, i + 1).Value Then
Cells(1, i + 1).EntireColumn.Delete
k = k + 1
End If
Loop Until k = 3
Next i
End Sub
答案 1 :(得分:0)
如果归结为我在If语句中没有Else的事实。缺少Else阻止了k的增加。
感谢@ Adach1979建议我回到细胞。
Dim i As Integer
Dim j As Integer
For i = 1 To 35
For j = 1 To counter
If Cells(j, i).MergeCells Then
Cells(j, i).UnMerge
Else
Exit For
End If
Next j
Next i
For i = 1 To 35
k = 0
Do
If Cells(1, i).Value = Cells(1, i + 1).Value Then
Cells(1, i + 1).EntireColumn.Delete
k = k + 1
Else
Exit Do
End If
Loop Until k = 5
Next i