Sub FormatAndMergeCells()
Dim ws As Worksheet
Dim rng As Range
Dim i As Integer
Dim beforeDash As String
Dim afterDash As String
Dim myCell As Range
' 遍历所有工作表
For Each ws In ThisWorkbook.Worksheets
' 设置H3:H100的数据格式
Set rng = ws.Range("H3:H100")
With rng.Font
.Name = "宋体" ' 对应宋体
.Size = 10
.Bold = False
End With
' 设置J3:J100的数据格式
Set rng = ws.Range("J3:J100")
With rng.Font
.Name = "华文行楷" ' 对应华文行楷
.Size = 10
.Bold = False
End With
' 合并H列和I列的同一行数据,输出到C列
For i = 3 To 100
ws.Range("C" & i).Value = ws.Range("H" & i).Value & "-" & ws.Range("J" & i).Value
' 假设我们要处理的单元格是Ci
Set myCell = ws.Range("C" & i)
' 找到"-"因素在单元格中的位置
Dim dashPos As Integer
dashPos = InStr(myCell.Value, "-")
' 如果"-"存在,则分割字符串并设置字体和字号
If dashPos > 0 Then
' 分割字符串
beforeDash = Left(myCell.Value, dashPos - 1)
afterDash = Mid(myCell.Value, dashPos + 1)
' 清空C2中的内容
myCell.Value = ""
' 首先输入"-"之前的部分,并设置字体和字号
myCell.Value = beforeDash
With myCell.Characters(Start:=1, Length:=Len(beforeDash)).Font
.Name = "宋体"
.Size = 10
.Bold = False
End With
' 然后输入 "-" 和 "-"之后的部分,并设置字体和字号
myCell.Value = myCell.Value & afterDash
With myCell.Characters(Start:=Len(beforeDash) + 1, Length:=Len(afterDash) + 1).Font
.Name = "华文行楷"
.Size = 10
.Bold = False
End With
End If
Next i
Next ws
End Sub