|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
现有代码如下:
Sub MergeHouseholdHeads()
Application.DisplayAlerts = False ' 关闭Excel警告提示
Application.ScreenUpdating = False ' 关闭屏幕更新以提高性能
Dim ws As Worksheet
Set ws = ActiveSheet
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
' 在A列插入新列(原A列及之后的所有列右移)
ws.Columns("A:A").Insert Shift:=xlToRight
ws.Cells(1, 1).Value = "新序" ' 设置新列标题
' 记录户主顺序和数量
Dim householdCount As Long
householdCount = 0
Dim householdOrder As Object
Set householdOrder = CreateObject("Scripting.Dictionary")
' 第一遍遍历:确定户主顺序并填充新序列
Dim i As Long
For i = 2 To lastRow
' 注意:现在B列变成了C列(因为插入了新列A)
Dim currentHead As String
currentHead = ws.Cells(i, 3).Value ' C列是原来的B列(户主列)
If Not householdOrder.Exists(currentHead) Then
householdCount = householdCount + 1
householdOrder.Add currentHead, householdCount
End If
' 设置新序值(插入到A列)
ws.Cells(i, 1).Value = householdOrder(currentHead)
Next i
' 第二遍遍历:合并户主列单元格(现在是C列)和新序列单元格(A列)
Dim startMergeRow As Long
startMergeRow = 2
Dim currentOwner As String
currentOwner = ws.Cells(startMergeRow, 3).Value ' C列是户主列
For i = 3 To lastRow + 1
' 检查是否到达最后一行或户主发生变化
If i > lastRow Or ws.Cells(i, 3).Value <> currentOwner Then
' 合并A列(新序列)单元格
If i - 1 > startMergeRow Then
With ws.Range(ws.Cells(startMergeRow, 1), ws.Cells(i - 1, 1))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else
' 单行也设置居中
ws.Cells(startMergeRow, 1).HorizontalAlignment = xlCenter
ws.Cells(startMergeRow, 1).VerticalAlignment = xlCenter
End If
' 合并C列(户主列)单元格
If i - 1 > startMergeRow Then
With ws.Range(ws.Cells(startMergeRow, 3), ws.Cells(i - 1, 3))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Else
' 单行也设置居中
ws.Cells(startMergeRow, 3).HorizontalAlignment = xlCenter
ws.Cells(startMergeRow, 3).VerticalAlignment = xlCenter
End If
If i <= lastRow Then
currentOwner = ws.Cells(i, 3).Value
End If
startMergeRow = i
End If
Next i
' 恢复设置
Application.DisplayAlerts = True
Application.ScreenUpdating = True
' 显示户主数量
MsgBox "共有 " & householdCount & " 位户主。"
End Sub
现在的问题是同名同姓的C列户主名(其实D列和E列值不同)会赋予相同的A列“新序号”,请问能区分开吗?
我明白您的需求了!您希望在完成单元格合并操作后,再根据D列和E列的值来区分那些C列相同但实际上不同的户主,并为它们赋予不同的序号。
以下是修改后的代码,它会在合并完成后,再根据D列和E列的值重新检查并修正序号:
- Sub MergeHouseholdHeads()
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
-
- Dim ws As Worksheet
- Set ws = ActiveSheet
-
- Dim lastRow As Long
- lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
-
- ' 在A列插入新列
- ws.Columns("A:A").Insert Shift:=xlToRight
- ws.Cells(1, 1).Value = "新序"
-
- ' 第一遍:按C列(户主)分配序号
- Dim householdCount As Long
- householdCount = 0
- Dim householdOrder As Object
- Set householdOrder = CreateObject("Scripting.Dictionary")
-
- For i = 2 To lastRow
- Dim currentHead As String
- currentHead = ws.Cells(i, 3).Value
-
- If Not householdOrder.Exists(currentHead) Then
- householdCount = householdCount + 1
- householdOrder.Add currentHead, householdCount
- End If
-
- ws.Cells(i, 1).Value = householdOrder(currentHead)
- Next i
-
- ' 合并单元格
- Dim startMergeRow As Long
- startMergeRow = 2
- Dim currentOwner As String
- currentOwner = ws.Cells(startMergeRow, 3).Value
-
- For i = 3 To lastRow + 1
- If i > lastRow Or ws.Cells(i, 3).Value <> currentOwner Then
- ' 合并A列和C列单元格
- If i - 1 > startMergeRow Then
- With ws.Range(ws.Cells(startMergeRow, 1), ws.Cells(i - 1, 1))
- .Merge
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
-
- With ws.Range(ws.Cells(startMergeRow, 3), ws.Cells(i - 1, 3))
- .Merge
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- Else
- ws.Cells(startMergeRow, 1).HorizontalAlignment = xlCenter
- ws.Cells(startMergeRow, 1).VerticalAlignment = xlCenter
- ws.Cells(startMergeRow, 3).HorizontalAlignment = xlCenter
- ws.Cells(startMergeRow, 3).VerticalAlignment = xlCenter
- End If
-
- If i <= lastRow Then
- currentOwner = ws.Cells(i, 3).Value
- startMergeRow = i
- End If
- End If
- Next i
-
- ' 第二遍:根据D列和E列的值重新检查并修正序号
- Dim correctedOrder As Object
- Set correctedOrder = CreateObject("Scripting.Dictionary")
- Dim newHouseholdCount As Long
- newHouseholdCount = 0
-
- For i = 2 To lastRow
- ' 只处理合并区域的第一个单元格
- If Not IsEmpty(ws.Cells(i, 1).Value) Then
- Dim uniqueID As String
- uniqueID = ws.Cells(i, 3).Value & "|" & ws.Cells(i, 4).Value & "|" & ws.Cells(i, 5).Value
-
- If Not correctedOrder.Exists(uniqueID) Then
- newHouseholdCount = newHouseholdCount + 1
- correctedOrder.Add uniqueID, newHouseholdCount
- End If
-
- ' 获取当前合并区域的范围
- Dim mergeArea As Range
- Set mergeArea = ws.Cells(i, 1).MergeArea
-
- ' 取消合并
- mergeArea.UnMerge
-
- ' 为新序号赋值
- mergeArea.Value = correctedOrder(uniqueID)
-
- ' 重新合并
- mergeArea.Merge
- mergeArea.HorizontalAlignment = xlCenter
- mergeArea.VerticalAlignment = xlCenter
- End If
- Next i
-
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "处理完成!共识别出 " & newHouseholdCount & " 个不同的户主。"
- End Sub
复制代码
这个代码的工作流程是:
1. 首先按原逻辑根据C列(户主)分配序号并合并单元格
2. 然后再次遍历数据,根据C列+D列+E列的组合创建唯一标识
3. 对于每个唯一标识,分配一个新的序号
4. 取消原有的合并,更新序号,然后重新合并
这样就能确保即使C列(户主)相同,但只要D列或E列不同,就会被视为不同的户主并分配不同的序号。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|