复制/粘贴/排序多列

发布于 2025-02-12 04:10:43 字数 1238 浏览 0 评论 0原文

在下面的脚本中复制/粘贴数据问题。计划将原始数据从列T,C,R列为列x,y,z然后对列x降序。开始使用索引/匹配,但更易于复制/粘贴/排序。由于Worksheet_calc事件,将必须复制/粘贴/对每毫秒进行复制/排序。

Private Sub Worksheet_Calculate()

Dim total_data As Range
Dim specific_column As Range

If Worksheets("Dashboard").ToggleButton1.Value = True Then
   On Error GoTo SafeExit
   Application.EnableEvents = False
   Application.ScreenUpdating = False

Set sht1 = ThisWorkbook.Sheets("Log")
Set cpyRng = sht1.Range("T3:T60000,C3:C60000,R3:R60000")
Set pstRng = sht1.Range("X3:X60000,Y3:Y60000,Z3:Z60000")

            cpyRng.Copy
            pstRng.PasteSpecial xlPasteValues
            Application.CutCopyMode = False ' Remove the copy area marker
            
Set total_data = Worksheets("Log").Range("X:Z")
Set specific_column = Worksheets("Log").Range("X:X")

total_data.Sort Key1:=specific_column, Order1:=xlDescending, Header:=xlYes
Worksheets("Log").Cells(1, 1).Select

End If

SafeExit:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

Having issues copying/pasting data in script below. Plan to copy raw data from Columns T,C,R to Columns X,Y,Z then sort Column X descending. Started using Index/Match but easier to copy/paste/sort. Will have to copy/paste/sort every millisecond due to Worksheet_Calc event.

enter image description here

Private Sub Worksheet_Calculate()

Dim total_data As Range
Dim specific_column As Range

If Worksheets("Dashboard").ToggleButton1.Value = True Then
   On Error GoTo SafeExit
   Application.EnableEvents = False
   Application.ScreenUpdating = False

Set sht1 = ThisWorkbook.Sheets("Log")
Set cpyRng = sht1.Range("T3:T60000,C3:C60000,R3:R60000")
Set pstRng = sht1.Range("X3:X60000,Y3:Y60000,Z3:Z60000")

            cpyRng.Copy
            pstRng.PasteSpecial xlPasteValues
            Application.CutCopyMode = False ' Remove the copy area marker
            
Set total_data = Worksheets("Log").Range("X:Z")
Set specific_column = Worksheets("Log").Range("X:X")

total_data.Sort Key1:=specific_column, Order1:=xlDescending, Header:=xlYes
Worksheets("Log").Cells(1, 1).Select

End If

SafeExit:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2025-02-19 04:10:43

您可以在X3中使用公式(如果使用Excel 365) - 它将以新顺序溢出所有三列并对其进行排序,

=SORT(CHOOSE({1,2,3},$T$3:$T$60000,$C$3:$C$60000,$R$3:$R$60000),1,1)

或者您在VBA中使用此公式,然后将输出切换到值-STH。喜欢:

sht1.Range("X3").Formula2 = "=SORT(CHOOSE({1,2,3},$T$3:$T$60000,$C$3:$C$60000,$R$3:$R$60000),1,1)"
sht1.Range("X3:Z60000").value = sht1.Range("X3:Z60000").value

You can use a formula in X3 (if you use Excel 365) - it will spill down all three columns in the new order and sort them

=SORT(CHOOSE({1,2,3},$T$3:$T$60000,$C$3:$C$60000,$R$3:$R$60000),1,1)

Or you use this formula in VBA and then switch the output to values - sth. like:

sht1.Range("X3").Formula2 = "=SORT(CHOOSE({1,2,3},$T$3:$T$60000,$C$3:$C$60000,$R$3:$R$60000),1,1)"
sht1.Range("X3:Z60000").value = sht1.Range("X3:Z60000").value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文