From 541bbfe0ad6a8d2109e2aa188df6ce8ef3ca57cf Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Tue, 24 Feb 2026 12:44:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20replace=20CODE=20field=20source=20f?= =?UTF-8?q?rom=20column=20C=20to=20column=20I=20(66=E4=BB=A3=E7=A0=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add COL_IDX_CODE66 constant for column I in M04_Config.bas - Extend data reading range to include column I in M02_DataIO.ReadSourceData - Use .Text property for column I to preserve leading zeros - Update baseInfo array to use column I instead of column C in M01_Main.bas - Set output code column to text format to prevent numeric conversion Co-Authored-By: Claude Sonnet 4.5 --- VBA_BOMConverter/Modules/M01_Main.bas | 4 +-- VBA_BOMConverter/Modules/M02_DataIO.bas | 38 ++++++++++++++++++++++--- VBA_BOMConverter/Modules/M04_Config.bas | 1 + 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/VBA_BOMConverter/Modules/M01_Main.bas b/VBA_BOMConverter/Modules/M01_Main.bas index 4074d1c..f500767 100644 --- a/VBA_BOMConverter/Modules/M01_Main.bas +++ b/VBA_BOMConverter/Modules/M01_Main.bas @@ -95,8 +95,8 @@ Public Sub RunBOMConversion() catData.Add strCat, New Collection End If - ' 基础信息: 代号, 名称, 数量 - baseInfo = Array(arrData(i, M04_Config.COL_IDX_CODE), _ + ' 基础信息: 66代码, 名称, 数量(使用I列的66代码作为编码) + baseInfo = Array(arrData(i, M04_Config.COL_IDX_CODE66), _ arrData(i, M04_Config.COL_IDX_NAME), _ arrData(i, M04_Config.COL_IDX_QTY)) diff --git a/VBA_BOMConverter/Modules/M02_DataIO.bas b/VBA_BOMConverter/Modules/M02_DataIO.bas index e4a7a7a..fcb1d8b 100644 --- a/VBA_BOMConverter/Modules/M02_DataIO.bas +++ b/VBA_BOMConverter/Modules/M02_DataIO.bas @@ -6,7 +6,12 @@ Option Explicit Public Function ReadSourceData(ws As Worksheet) As Variant Dim lastRow As Long - ' 查找C列最后一行 + Dim i As Long, r As Long + Dim arrRaw As Variant + Dim arrResult() As Variant + Dim cell As Range + + ' 查找C列最后一行(保持原有逻辑,使用代号列判断) lastRow = ws.Cells(ws.Rows.count, M04_Config.COL_IDX_CODE).End(xlUp).row If lastRow < M04_Config.SRC_START_ROW Then @@ -14,8 +19,28 @@ Public Function ReadSourceData(ws As Worksheet) As Variant Exit Function End If - ' 读取整张表的数据区域 (假设列数不超过20列,足以覆盖到类别列) - ReadSourceData = ws.Range(ws.Cells(M04_Config.SRC_START_ROW, 1), ws.Cells(lastRow, M04_Config.COL_IDX_CAT)).Value + ' 第一步:使用数组方式读取整个数据区域(高性能) + arrRaw = ws.Range(ws.Cells(M04_Config.SRC_START_ROW, 1), ws.Cells(lastRow, M04_Config.COL_IDX_CODE66)).Value + + ' 第二步:对66代码列(第9列)使用.Text属性重新读取,以保留前导0 + ' 因为.Value会将"00123"转换为123,而.Text会保留显示的"00123" + ReDim arrResult(1 To UBound(arrRaw, 1), 1 To UBound(arrRaw, 2)) + + ' 复制原始数据 + For r = 1 To UBound(arrRaw, 1) + For i = 1 To UBound(arrRaw, 2) + If i = M04_Config.COL_IDX_CODE66 Then + ' 对于66代码列(I列),使用.Text属性读取显示文本 + Set cell = ws.Cells(M04_Config.SRC_START_ROW + r - 1, M04_Config.COL_IDX_CODE66) + arrResult(r, i) = cell.Text + Else + ' 其他列使用原始值 + arrResult(r, i) = arrRaw(r, i) + End If + Next i + Next r + + ReadSourceData = arrResult End Function Public Sub WriteCategoryToNewBook(catData As Object) @@ -105,9 +130,14 @@ Public Sub WriteCategoryToNewBook(catData As Object) finalArr(r + 1, colOffset + 3) = baseInfo(2) ' Qty Next r - ' 4. 写入Excel + + ' 4 设置编码列为文本格式(防止数字编码被转换为科学计数法或丢失前导零) + ws.Columns(colOffset + 2).NumberFormat = "@" + + ' 4.1 写入Excel ws.Range("A1").Resize(UBound(finalArr, 1), UBound(finalArr, 2)).Value = finalArr ws.Range("A1").Resize(1, totalCols).Font.Bold = True + ws.Columns.AutoFit End If Next catName diff --git a/VBA_BOMConverter/Modules/M04_Config.bas b/VBA_BOMConverter/Modules/M04_Config.bas index 10c413a..71d5852 100644 --- a/VBA_BOMConverter/Modules/M04_Config.bas +++ b/VBA_BOMConverter/Modules/M04_Config.bas @@ -10,6 +10,7 @@ Public Const COL_IDX_NAME As Long = 4 ' 名称 (D列) Public Const COL_IDX_QTY As Long = 5 ' 数量 (E列) Public Const COL_IDX_COND As Long = 6 ' 选择条件 (F列) Public Const COL_IDX_CAT As Long = 8 ' 类别 (H列) +Public Const COL_IDX_CODE66 As Long = 9 ' 66代码 (I列) - NEW Public Const SRC_START_ROW As Long = 4 ' 数据起始行 ' ------------------------------------------------------------------------------