Yellow one – merge range 1 (E5:F5)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Red one – merge range 2(E6:H6)
Attached the merged spreadsheet and also the screen shot.
Suppose, I want to merge between E4:F8, I would want the yellow merge and red merge to be removed dynamically so that the merge for E4:F8 works. In this case, I know that we have only two conflicting merges, but in my scenario there could be numerous conflicting merges and I have to identify it dynamically and resolve it.
If you do it, vba it will work fine. The highlighted code in red will work fine.
The vba code is as below –
Sub Macro1()
'
' Macro1 Macro
'
'
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("E6:H6").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
Range("C5:F5").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("E6:H6").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("E4:F8").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
End Sub
C# code –
public ReportFormatter(string file)
{
ReportWorkbook = new Workbook(file);
ReportWorksheet = ReportWorkbook.Worksheets["Sheet1"];
ReportCells = ReportWorksheet.Cells;
}
public void FormatMethod()
{
Range mergeRange = ReportCells.CreateRange("E4", "F8");
mergeRange.Merge();
ReportWorkbook.Save("C:\bulb.xls");
}
Please note that your previous sample workaround is based on previous cell and you check whether it is merged are not.
Thanks!