复制列A,每隔一列插入一次,然后排序

时间:2019-04-10 12:46:36

标签: excel vba

我需要帮助来创建VBA代码以获取数据,并为每个插入的日期列(A列)进行插入,然后对每一对列进行排序。

数据开始像这样

Date, A, B, C, ...
01/02/2019, 100, 110, 120, ...
02/02/2019, 101, 107, 124, ...
03/02/2019, 102, 107, 123, ...
04/02/2019, 101, 108, 123, ...

首先,它应按照

插入日期
Date, A, Date, B, Date, C, ...
01/02/2019, 100, 01/02/2019, 110, 01/02/2019, 120, ...
02/02/2019, 101, 02/02/2019, 107, 02/02/2019, 124, ...
03/02/2019, 102, 03/02/2019, 107, 03/02/2019, 123, ...
04/02/2019, 101, 04/02/2019, 108, 04/02/2019, 123, ...

然后它应该成对地列,并按第二列排序,即

Date,  A,  Date,  B,  Date,  C,  ...
01/02/2019, 100,  02/02/2019, 107,  01/02/2019, 120,  ...
02/02/2019, 101,  03/02/2019, 107,  03/02/2019, 123,  ...
04/02/2019, 101,  04/02/2019, 108,  04/02/2019, 123,  ...
03/02/2019, 102,  01/02/2019, 110,  02/02/2019, 124, ...

到目前为止,我已经找到了查找行中最后一列的代码,该列将返回的最后一列作为字符串引用(例如“ GM”)

Function ColumnLetter() As String
    Dim sht As Worksheet
    Dim LastColumn As Long
    Dim LastColumnStr, ColumnPaste As String
    Dim p As Long

    Set sht = ThisWorkbook.Worksheets("srtData")

    LastColumn = sht.Range("A1").CurrentRegion.Columns.Count

    Dim n As Long
    Dim c As Byte
    Dim s As String

    n = LastColumn
    Do
        c = ((n - 1) Mod 26)
        s = Chr(c + 65) & s
        n = (n - c) \ 26
    Loop While n > 0
    ColumnLetter = s
End Function

但是我不知道如何迭代,然后按第二列排序。理想情况下,我只想具有以下功能 Function sortallData(dim wsTest as String),只需输入工作表名称即可调用该函数。

1 个答案:

答案 0 :(得分:0)

最后,我实际上遵循了蒂姆斯的建议。我将其分为3个不同的脚本,一个脚本通过选择列,粘贴和插入来进行迭代。然后,下一个对每列进行排序。