剪切和粘贴动态数据范围,反复进行操作。

huangapple go评论93阅读模式
英文:

Cut and paste dynamic ranges of data repeatedly

问题

我正在尝试对180万个数据点进行数据分析。

我需要自动化以下操作:

将列B中具有相同序列号的连续行的数据范围切割出来。
然后将其粘贴到之前范围的右侧。

我找到了这个链接:https://stackoverflow.com/questions/65541826/excel-vba-cut-and-paste-range-repeatedly

以下是提供的VBA代码,该代码可以切割和粘贴预定范围的单元格(124x14)。在我的数据中,要切割和粘贴的行数需要根据具有相同序列号的行数而变化。

  1. Sub Cutrange()
  2. Dim i As Long
  3. Dim Lrow As Long
  4. Lrow = Cells(Rows.Count, 1).End(xlUp).Row
  5. Dim oRange As Range, dRange As Range
  6. Set oRange = Range(Cells(1, 1), Cells(124, 14))
  7. Set dRange = Cells(1, 1)
  8. For i = 1 To Lrow
  9. oRange.Offset(124 * i).Cut Destination:=dRange.Offset(, 14 * i)
  10. Next i
  11. End Sub
英文:

I am trying to sort 1,8 million data points for data analysis.

I need to automate the following:

Cut a range of data in x sequential rows with identical Sequence numbers in column B.
剪切和粘贴动态数据范围,反复进行操作。

Then paste it onto the right next to the previous range.
剪切和粘贴动态数据范围,反复进行操作。

I found https://stackoverflow.com/questions/65541826/excel-vba-cut-and-paste-rage-repeatedly

  1. Sub Cutrange()
  2. Dim i As Long
  3. Dim Lrow As Long
  4. Lrow = Cells(Rows.Count, 1).End(xlUp).Row
  5. Dim oRange As Range, dRange As Range
  6. Set oRange = Range(Cells(1, 1), Cells(124, 14))
  7. Set dRange = Cells(1, 1)
  8. For i = 1 To Lrow
  9. oRange.Offset(124 * i).Cut Destination:=dRange.Offset(, 14 * i)
  10. Next i
  11. End Sub

This code cuts and pastes a pre-determined range of cells (124x14). In my data, the number of rows (to be cut and pasted) needs to change based on how many rows have the same Sequence number.

答案1

得分: 1

以下是你要翻译的代码部分:

  1. Option Explicit
  2. Sub HStackGroups()
  3. Const SRC_NAME As String = "Sheet1"
  4. Const SRC_FIRST_CELL As String = "B2"
  5. Const DST_NAME As String = "Sheet2"
  6. Const DST_FIRST_CELL As String = "B2"
  7. Const UNIQUE_COLUMN As Long = 1
  8. Const COLUMN_GAP As Long = 1
  9. Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿
  10. Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)
  11. Dim srg As Range: Set srg = sws.Range(SRC_FIRST_CELL).CurrentRegion
  12. Dim hData(): hData = srg.Rows(1).Value
  13. Dim cCount As Long: cCount = srg.Columns.Count
  14. Dim srCount As Long: srCount = srg.Rows.Count - 1 ' 无标题行
  15. Dim sData(): sData = srg.Resize(srCount).Offset(1).Value
  16. Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
  17. Dim sr As Long, drCount As Long, srString As String
  18. For sr = 1 To srCount
  19. srString = CStr(sData(sr, UNIQUE_COLUMN))
  20. If Not dict.Exists(srString) Then
  21. Set dict(srString) = New Collection
  22. End If
  23. dict(srString).Add sr
  24. If dict(srString).Count > drCount Then drCount = dict(srString).Count
  25. Next sr
  26. drCount = drCount + 1 ' 1 用于标题行
  27. Dim dCount As Long: dCount = cCount + COLUMN_GAP
  28. Dim dcCount As Long
  29. dcCount = dict.Count * dCount - COLUMN_GAP
  30. Dim dData(): ReDim dData(1 To drCount, 1 To dcCount)
  31. Dim Coll, Item, sc As Long, d As Long, dr As Long, dc As Long
  32. For Each Coll In dict.Items
  33. dc = d * dCount
  34. For sc = 1 To cCount
  35. dData(1, dc + sc) = hData(1, sc)
  36. Next sc
  37. dr = 1
  38. For Each Item In Coll
  39. dr = dr + 1
  40. For sc = 1 To cCount
  41. dData(dr, dc + sc) = sData(Item, sc)
  42. Next sc
  43. Next Item
  44. d = d + 1
  45. Next Coll
  46. Dim dws As Worksheet: Set dws = wb.Sheets(DST_NAME)
  47. Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
  48. Dim drg As Range: Set drg = dfCell.Resize(drCount, dcCount)
  49. With dfCell
  50. .Resize(dws.Rows.Count - .Row + 1, dws.Columns.Count - .Column + 1) _
  51. .Clear
  52. End With
  53. drg.Value = dData
  54. drg.EntireColumn.AutoFit
  55. MsgBox "分组堆叠完成。", vbInformation
  56. End Sub

希望这对你有所帮助。如果你有任何其他问题,不要犹豫,可以随时提出。

英文:

HStack Groups of Data (VBA)

剪切和粘贴动态数据范围,反复进行操作。

<!-- language: lang-vb -->

  1. Option Explicit
  2. Sub HStackGroups()
  3. Const SRC_NAME As String = &quot;Sheet1&quot;
  4. Const SRC_FIRST_CELL As String = &quot;B2&quot;
  5. Const DST_NAME As String = &quot;Sheet2&quot;
  6. Const DST_FIRST_CELL As String = &quot;B2&quot;
  7. Const UNIQUE_COLUMN As Long = 1
  8. Const COLUMN_GAP As Long = 1
  9. Dim wb As Workbook: Set wb = ThisWorkbook &#39; workbook containing this code
  10. Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)
  11. Dim srg As Range: Set srg = sws.Range(SRC_FIRST_CELL).CurrentRegion
  12. Dim hData(): hData = srg.Rows(1).Value
  13. Dim cCount As Long: cCount = srg.Columns.Count
  14. Dim srCount As Long: srCount = srg.Rows.Count - 1 &#39; no headers
  15. Dim sData(): sData = srg.Resize(srCount).Offset(1).Value
  16. Dim dict As Object: Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
  17. Dim sr As Long, drCount As Long, srString As String
  18. For sr = 1 To srCount
  19. srString = CStr(sData(sr, UNIQUE_COLUMN))
  20. If Not dict.Exists(srString) Then
  21. Set dict(srString) = New Collection
  22. End If
  23. dict(srString).Add sr
  24. If dict(srString).Count &gt; drCount Then drCount = dict(srString).Count
  25. Next sr
  26. drCount = drCount + 1 &#39; 1 for headers
  27. Dim dCount As Long: dCount = cCount + COLUMN_GAP
  28. Dim dcCount As Long
  29. dcCount = dict.Count * dCount - COLUMN_GAP
  30. Dim dData(): ReDim dData(1 To drCount, 1 To dcCount)
  31. Dim Coll, Item, sc As Long, d As Long, dr As Long, dc As Long
  32. For Each Coll In dict.Items
  33. dc = d * dCount
  34. For sc = 1 To cCount
  35. dData(1, dc + sc) = hData(1, sc)
  36. Next sc
  37. dr = 1
  38. For Each Item In Coll
  39. dr = dr + 1
  40. For sc = 1 To cCount
  41. dData(dr, dc + sc) = sData(Item, sc)
  42. Next sc
  43. Next Item
  44. d = d + 1
  45. Next Coll
  46. Dim dws As Worksheet: Set dws = wb.Sheets(DST_NAME)
  47. Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
  48. Dim drg As Range: Set drg = dfCell.Resize(drCount, dcCount)
  49. With dfCell
  50. .Resize(dws.Rows.Count - .Row + 1, dws.Columns.Count - .Column + 1) _
  51. .Clear
  52. End With
  53. drg.Value = dData
  54. drg.EntireColumn.AutoFit
  55. MsgBox &quot;Groups hstacked.&quot;, vbInformation
  56. End Sub

huangapple
  • 本文由 发表于 2023年2月9日 02:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定