我正在配置一个较长的 IF 语句,如果财务期间相同,它会将单元格合并到同一个期间标题下,如果不同,它会创建一个具有新期间名称的新标题。格式化函数似乎可以单独正常工作。但是,我总是遇到编译错误,Else without If。即使前面有一个 If 语句,也会发生这种情况。我浏览了论坛,但似乎找不到解决方案。这是我的代码:
Dim LC As Long
LC = Cells(1, Columns.Count).End(xlToLeft).Column
If Range(LC & "1").Value = Range(LC - 1 & "2").Value Then
Dim prd As Long
prd = Range(LC - 1 & "2").MergeArea.Address
Range(prd).UnMerge
prd = Union(Range(prd), Range(LC & "2"))
Range(prd).Merge
With Range(prd)
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Range(LC & "3", LC & "24")
.Borders(xlEdgeLeft).LineStyle = xlNone
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
End With
Else
With Range(LC & "2")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Font.Bold = True
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
End With
End If
答案1
我认为你忘记End With
了With Range(prd)
。
尝试一下这个是否有效:
Dim LC As Long
LC = Cells(1, Columns.Count).End(xlToLeft).Column
If Range(LC & "1").Value = Range(LC - 1 & "2").Value Then
Dim prd As Long
prd = Range(LC - 1 & "2").MergeArea.Address
Range(prd).UnMerge
prd = Union(Range(prd), Range(LC & "2"))
Range(prd).Merge
With Range(prd)
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
End With
With Range(LC & "3", LC & "24")
.Borders(xlEdgeLeft).LineStyle = xlNone
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlMedium
End With
End With
Else
With Range(LC & "2")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Font.Bold = True
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
End With
End If