gpt4 book ai didi

vba - 自动排序和格式化以及 Excel

转载 作者:行者123 更新时间:2023-12-03 02:10:48 26 4
gpt4 key购买 nike

我在 Excel 中有一个客户表,我希望能够将新客户添加到表的最后一行,并且 Excel 会自动对表进行排序,以便客户的姓名按字母顺序排序。

此外,格式将与上一行类似。例如,第二列是 DOB,所以我希望格式与上一行 MM/DD/YYYY 相同

谢谢

最佳答案

将附加的代码放入您的工作表模块中,它会自动对您的 A 列进行排序。

Private Sub Worksheet_Change(ByVal Target As Range)
'turn off updates to speed up code execution
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
.DisplayAlerts = False
End With

If Not Intersect(Target, Columns(1)) Is Nothing Then

With ActiveSheet.Sort
.SetRange Range("A1:X" & Cells(Rows.Count, 1).End(xlUp).Row)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Columns("B").NumberFormat = "MM/DD/YYYY"

End If

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
End With


End Sub

关于vba - 自动排序和格式化以及 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13020878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com