Skip to content

Instantly share code, notes, and snippets.

@yankchina
Last active August 20, 2016 09:17
Show Gist options
  • Save yankchina/992b6e6274df466d47e5eb056a82be45 to your computer and use it in GitHub Desktop.
Save yankchina/992b6e6274df466d47e5eb056a82be45 to your computer and use it in GitHub Desktop.
将Excel中某一列相同数值合并居中
Sub 某列相同数值合并居中()
Application.DisplayAlerts = False
Dim i, j As Integer
Dim cel As String
col = Application.InputBox("请输入需要合并的列名(如A):")
i = 1
Value = Cells(1, col)
For j = 2 To Cells(Rows.Count, col).End(xlUp).Row
If Cells(j, col) <> Value Then
Range(Cells(i, col), Cells(j - 1, col)).Merge
i = j
Value = Cells(i, col)
End If
Next j
If i <> j Then
Range(Cells(i, col), Cells(j - 1, col)).Merge
End If
Application.DisplayAlerts = True
End Sub
@yankchina
Copy link
Author

问题

  • Excel中某个列中存在许多相同的数值
  • 按照中国人的惯例,需要将这些列合并居中
  • 如果数据很多,需要太多的重复性劳动

解决方法

  • 用VBA脚本
  • 录制一个宏,然后打开该宏,将如下脚本拷贝,运行即可

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment