excel 如何修改脚本,根据物料编码及其后续信息创建页签?[重复]

r8xiu3jd  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(220)

此问题在此处已有答案

Need to modify VBA script to create tabs based on item codes and subsequent information for them(1个答案)
19天前关闭
我有一个Excel表格“Sheet 1”,看起来像这样:
| 项目代码|项目描述|单位编号|数量|
| --|--|--|--|
| ABCD010204| ABCD 010204描述|U1234| 10 |
| ABCD010204| ABCD 010204描述|U3421| 10 |
| ABCD020206| ABCD 020206描述|U2345| 30 |
| ABCD020206| ABCD 020206描述|U3542| 30 |
| ABCD020206| ABCD 020206描述|U2456| 30 |
| ABCD020408| ABCD 020408描述|U3456| 20 |
| BCDE010203| BCDE 010203描述|U4567| 5 |
| BCDE010203| BCDE 010203描述|U6457| 5 |
| BCDE 020206| BCDE 020206描述|U5678| 15 |
| BCDE 020206| BCDE 020206描述|U8567| 15 |
| BCDE020410| BCDE 020410描述|U6789| 20 |
项目代码(大约40个)是连续的,不是标准的逻辑,也不总是相同的。但它们总是4个字母,后面通常是6个数字,有时是4个。

我想实现的目标:

1.目前,我可以为每种项目类型创建单独的选项卡(因此,在同一选项卡中有abcd 010204、abcd 020206等。在另一个选项卡中有bcde 0100203、bcde 020206、bcde 020410等。)
我想修改我的JavaScript脚本,从“Sheet 1”中获取所有数据,并为每个ItemCode及其后续数据创建选项卡(如下所示)。因此,每个选项卡一个唯一的项目。
| 项目代码|项目描述|单位编号|数量|
| --|--|--|--|
| ABCD010204| ABCD 010204描述|U1234| 10 |
| | | U3421 | 10 |
| 项目代码|项目描述|单位编号|数量|
| --|--|--|--|
| ABCD020206| ABCD 020206描述|U2345| 30 |
| | | U3542 | 30 |
| | | U2456 | 30 |
| 项目代码|项目描述|单位编号|数量|
| --|--|--|--|
| BCDE010203| BCDE 010203描述|U4567| 5 |
| | | U6457 | 5 |
1.有时,ItemDescr字段的数据长度约为40到60个字符。这会导致单元格无法显示整个说明。是否可以让该单元格使用该项目说明的最大长度的整个宽度?这样,我就不需要手动拖动单元格宽度来显示整个说明。

我当前的验证码如下所示:

  1. Sub CreateTabsFromData()
  2. Dim wsSource As Worksheet
  3. Set wsSource = ThisWorkbook.Sheets("Sheet1") ' This is the name of my main sheet
  4. Dim i As Long
  5. Dim lastRow As Long
  6. ' Sort source data
  7. With wsSource
  8. lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  9. .Range("A1").CurrentRegion.Sort Key1:=.Range("A2:A" & lastRow), Header:=xlYes
  10. End With
  11. Dim wsDestination As Worksheet
  12. Dim currentCode As String
  13. Dim newRow As Long
  14. Dim lastCode As String
  15. Dim startRow as Long
  16. lastCode = Left(wsSource.Cells(2, 1).Value, 4)
  17. startRow = 2
  18. For i = 2 To lastRow + 1 ' My sheet has a header, so starting from row 2.
  19. currentCode = Left(wsSource.Cells(i, 1).Value, 4) ' Extracting the first 4 characters of the Itemcode
  20. If currentCode <> lastCode Then
  21. Set wsDestination = Nothing
  22. On Error Resume Next
  23. Set wsDestination = ThisWorkbook.Sheets(lastCode)
  24. On Error GoTo 0
  25. If wsDestination Is Nothing Then
  26. ' Creating a new worksheet for the code
  27. Set wsDestination = ThisWorkbook.Sheets.Add(, wsSource)
  28. wsDestination.Name = lastCode
  29. wsDestination.Cells(1, 1).Resize(1, 4).Value = Array("ItemCode", "ItemDescr", "UnitNo", "Qty") ' Header row
  30. End If
  31. newRow = wsDestination.Cells(wsDestination.Rows.Count, 1).End(xlUp).Row + 1
  32. wsSource.Rows(startRow & ":" & i - 1).Copy wsDestination.Cells(newRow, 1)
  33. lastCode = currentCode
  34. startRow = i
  35. End If
  36. Next i
  37. End Sub

字符串

s71maibg

s71maibg1#

我可以给你一个解决方案,这是在一个点击。也许你会改变你的解决方案的方法。
一个数据透视表可以是这样的解决方案(它看起来完全像你说的,可以在一个点击刷新):


的数据
这是构建数据透视表的代码。

  1. Sub Macro2()
  2. ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
  3. [A1].CurrentRegion, Version:=8).CreatePivotTable TableDestination:= _
  4. "Sheet1!R3C6", TableName:="MyPT", DefaultVersion:=8
  5. With ActiveSheet.PivotTables("MyPT")
  6. .ColumnGrand = True
  7. .HasAutoFormat = True
  8. .DisplayErrorString = False
  9. .DisplayNullString = True
  10. .EnableDrilldown = True
  11. .ErrorString = ""
  12. .MergeLabels = False
  13. .NullString = ""
  14. .PageFieldOrder = 2
  15. .PageFieldWrapCount = 0
  16. .PreserveFormatting = True
  17. .RowGrand = True
  18. .SaveData = True
  19. .PrintTitles = False
  20. .RepeatItemsOnEachPrintedPage = True
  21. .TotalsAnnotation = False
  22. .CompactRowIndent = 1
  23. .InGridDropZones = False
  24. .DisplayFieldCaptions = True
  25. .DisplayMemberPropertyTooltips = False
  26. .DisplayContextTooltips = True
  27. .ShowDrillIndicators = False
  28. .PrintDrillIndicators = False
  29. .AllowMultipleFilters = False
  30. .SortUsingCustomLists = True
  31. .FieldListSortAscending = False
  32. .ShowValuesRow = False
  33. .CalculatedMembersInFilters = False
  34. .RowAxisLayout xlCompactRow
  35. With .PivotCache
  36. .RefreshOnFileOpen = False
  37. .MissingItemsLimit = xlMissingItemsDefault
  38. End With
  39. .RepeatAllLabels xlDoNotRepeatLabels
  40. With .PivotFields("ItemCode")
  41. .Orientation = xlRowField
  42. .Position = 1
  43. .Subtotals = _
  44. Array(False, False, False, False, False, False, False, False, False, False, False, False)
  45. End With
  46. With .PivotFields("ItemDescr")
  47. .Orientation = xlRowField
  48. .Position = 2
  49. .Subtotals = _
  50. Array(False, False, False, False, False, False, False, False, False, False, False, False)
  51. End With
  52. With .PivotFields("UnitNo")
  53. .Orientation = xlRowField
  54. .Position = 3
  55. End With
  56. .AddDataField .PivotFields("Qty"), "Sum of Qty", xlSum
  57. .InGridDropZones = True
  58. .RowAxisLayout xlTabularRow
  59. .ColumnGrand = False
  60. End With
  61. End Sub

字符串

展开查看全部

相关问题