excel表格中如何使用VB将第一列相同内容合并居中,同时第二列不变,第三列内容居中
答案:1 悬赏:50 手机版
解决时间 2021-11-27 08:55
- 提问者网友:wodetian
- 2021-11-27 04:26
excel表格中如何使用VB将第一列相同内容合并居中,同时第二列不变,第三列内容居中
最佳答案
- 五星知识达人网友:长青诗
- 2021-11-27 05:40
你的第一列相同类容是间隔的吗?如果是间隔的,可以尝试使用如下代码实现:
假设要处理的数据放在Sheet1中,结果放到sheet2中。
Sub XXX()
Application.DisplayAlerts = False
Dim i As Integer, j As Integer, KeyValue As String, Coumx As Integer
i = 1
j = 1
While Sheets("Sheet1").Cells(1, 1) <> ""
Coumx = j
KeyValue = Sheets("Sheet1").Cells(1, 1)
While Sheets("Sheet1").Cells(i, 1) <> ""
If Sheets("Sheet1").Cells(i, 1) = KeyValue Then
Sheets("Sheet2").Cells(j, 1) = Sheets("Sheet1").Cells(i, 1)
Sheets("Sheet2").Cells(j, 2) = Sheets("Sheet1").Cells(i, 2)
Sheets("Sheet2").Cells(j, 3) = Sheets("Sheet1").Cells(i, 3)
Sheets("Sheet2").Cells(j, 3).HorizontalAlignment = xlCenter
j = j + 1
Sheets("Sheet1").Rows(i & ":" & i).Delete Shift:=xlUp
Else
i = i + 1
End If
Wend
i = 1
Sheets("sheet2").Range(Cells(Coumx, 1), Cells(j - 1, 1)).Merge
Wend
Application.DisplayAlerts = True
End Sub
追问谢谢咯~☺ 我看到了 不过还想问问 内容间隔和内容不间隔解法是否一样 辛苦追答内容不间隔代码要简单点,间隔的代码适合于不间隔
假设要处理的数据放在Sheet1中,结果放到sheet2中。
Sub XXX()
Application.DisplayAlerts = False
Dim i As Integer, j As Integer, KeyValue As String, Coumx As Integer
i = 1
j = 1
While Sheets("Sheet1").Cells(1, 1) <> ""
Coumx = j
KeyValue = Sheets("Sheet1").Cells(1, 1)
While Sheets("Sheet1").Cells(i, 1) <> ""
If Sheets("Sheet1").Cells(i, 1) = KeyValue Then
Sheets("Sheet2").Cells(j, 1) = Sheets("Sheet1").Cells(i, 1)
Sheets("Sheet2").Cells(j, 2) = Sheets("Sheet1").Cells(i, 2)
Sheets("Sheet2").Cells(j, 3) = Sheets("Sheet1").Cells(i, 3)
Sheets("Sheet2").Cells(j, 3).HorizontalAlignment = xlCenter
j = j + 1
Sheets("Sheet1").Rows(i & ":" & i).Delete Shift:=xlUp
Else
i = i + 1
End If
Wend
i = 1
Sheets("sheet2").Range(Cells(Coumx, 1), Cells(j - 1, 1)).Merge
Wend
Application.DisplayAlerts = True
End Sub
追问谢谢咯~☺ 我看到了 不过还想问问 内容间隔和内容不间隔解法是否一样 辛苦追答内容不间隔代码要简单点,间隔的代码适合于不间隔
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯