如何更改此脚本对数据进行分组的方向?

时间:2018-12-21 16:57:26

标签: vba excel-vba

我有一个脚本,该脚本基于column A中的重复值在总体数组中形成数组。

manager 1自己的工作簿

manager 2

manager 2这两个将被分组到另一个工作簿中,依此类推。

问题是,现在A列中的这些单元格已作为第1行中的标题转置。

我将如何编辑该脚本以现在按行标题将数据分组并占据整列而不是最初编写脚本的方式?

我认为这与交换Last = Data(1,i)有关。

Option Explicit

Sub Main()
  Dim wb As Workbook
  Dim Data, Last, JobFamily
  Dim i As Long, j As Long, k As Long, a As Long
  Dim Dest As Range

  'Refer to the template
  Set wb = Workbooks("Book2.xlsx")
  'Refer to the destination cell
  Set Dest = wb.Sheets("Sheet11").Range("B1")
  'Read in all data
  With ThisWorkbook.Sheets("Sheet1")
    Data = .Range("bj2", .Range("A" & Rows.Count).End(xlUp))
  End With
  wb.Activate
  Application.ScreenUpdating = False

  'Process the data
  For i = 1 To UBound(Data)
    'Manager changes?
    If Data(i, 1) <> Last Then
      'Skip the first
      If i > 1 Then
        'Scroll into the view
        Dest.Select
        'Save a copy
        wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
          ValidFileName(Last & ".xlsx")
      End If
      'Clear the employees
      Dest.Resize(, Columns.Count - Dest.Column).EntireColumn.ClearContents
      'Remember this manager
      Last = Data(i, 1)
      'Start the next round
      j = 0
    End If
    'Write the employee data into the template
    a = 0
    For k = 1 To UBound(Data, 2)
      Dest.Offset(a, j) = Data(i, k)
      a = a + 1
    Next
    'Next column
    j = j + 1
  Next
End Sub

1 个答案:

答案 0 :(得分:0)

您给它打电话了。必须将所有对“ Data(i,1)”的引用替换为“ Data(1,i)”,以将范围的第一列转置为第一行。