程序段落基本是deepseek写的,博主只是整理了一下变量,合并了功能,实现的功能是读取两个外部文件的指定内容,根据一个两个表中的某个关系型键值(这里是关联号)来汇总成一张表格以方便查阅,最后合并同类项,把单列中上下内容完全一致的单元格合并为一个。程序中涉及了一些读取文件提取数据的语法,记录在博客上,以备查询。
程序全文
Option Explicit
Sub main()
Dim folderPath As String
Dim dict As Object
Dim f1 As String
Dim f2 As String
Dim wb As Workbook
Dim wsIn As Worksheet
Dim wsOut As Worksheet
Dim rowArr() As Variant
Dim firstRow As Long, lastRow As Long, outRow As Long
Dim firstCol As Long, lastCol As Long
Dim r As Long, c As Long
Dim cellVal As String
Dim inData As Boolean
Dim k As Variant, arr As Variant
'定义文件路径
folderPath = ThisWorkbook.Path & "\"
Set wsOut = ThisWorkbook.Sheets(1)
'获取数据表
f1 = Trim(CStr(wsOut.Range("B2").Value))
f2 = Trim(CStr(wsOut.Range("B3").Value))
If f1 = "" Or f2 = "" Then
MsgBox "B2 或 B3 为空,请先填文件名"
Exit Sub
End If
f1 = folderPath & f1
f2 = folderPath & f2
Set dict = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'读进货明细表
Set wb = Workbooks.Open(f1, ReadOnly:=True)
Set wsIn = wb.Sheets(1)
lastRow = wsIn.Cells(wsIn.Rows.Count, 1).End(xlUp).Row
lastCol = 15
inData = False
For r = 1 To lastRow
cellVal = Trim(CStr(wsIn.Cells(r, 1).Text))
If cellVal = "序号" Then
inData = True
ReDim rowArr(1 To lastCol)
For c = 1 To lastCol
rowArr(c) = wsIn.Cells(r, c).Text
Next c
If Not dict.Exists(cellVal) Then dict.Add cellVal, rowArr
ElseIf cellVal = "合计" Then
inData = False
ElseIf inData And IsNumeric(cellVal) And cellVal <> "" Then
ReDim rowArr(1 To lastCol)
For c = 1 To lastCol
rowArr(c) = wsIn.Cells(r, c).Text
Next c
If Not dict.Exists(cellVal) Then dict.Add cellVal, rowArr
End If
Next r
wb.Close False
'输出进货明细表
firstRow = 1
Set wsOut = ThisWorkbook.Sheets(2)
wsOut.Cells.Clear
If dict.Count > 0 Then
wsOut.Range(wsOut.Cells(firstRow, 1), _
wsOut.Cells(firstRow + dict.Count, lastCol)).NumberFormat = "@"
End If
outRow = firstRow
For Each k In dict.Keys
arr = dict(k)
For c = 1 To lastCol
If IsError(arr(c)) Then
wsOut.Cells(outRow, c).Value = ""
Else
wsOut.Cells(outRow, c).Value = CStr(arr(c))
End If
Next c
outRow = outRow + 1
Next k
Application.ScreenUpdating = True
Application.DisplayAlerts = False
'读出口明细表
Set wb = Workbooks.Open(f2, ReadOnly:=True)
Set wsIn = wb.Sheets(1)
Set dict = CreateObject("Scripting.Dictionary")
lastRow = wsIn.Cells(wsIn.Rows.Count, 1).End(xlUp).Row
lastCol = 14
inData = False
For r = 1 To lastRow
cellVal = Trim(CStr(wsIn.Cells(r, 1).Text))
If cellVal = "序号" Then
inData = True
ElseIf cellVal = "合计" Then
inData = False
ElseIf inData And IsNumeric(cellVal) And cellVal <> "1" Then
cellVal = Trim(CStr(wsIn.Cells(r, 2).Text))
ReDim rowArr(1 To lastCol)
For c = 1 To lastCol
rowArr(c) = wsIn.Cells(r, c).Text
Next c
If Not dict.Exists(cellVal) Then dict.Add cellVal, rowArr
End If
Next r
wb.Close False
'根据字典补充表格
Set wsOut = ThisWorkbook.Sheets(2)
Dim keyVal As String
lastRow = wsOut.Cells(wsOut.Rows.Count, 2).End(xlUp).Row
' 表头(可选)
wsOut.Cells(firstRow, 16).Value = "出口发票号"
wsOut.Cells(firstRow, 17).Value = "美元离岸价"
firstRow = firstRow + 1
wsOut.Cells(firstRow, 16).Value = "16"
wsOut.Cells(firstRow, 17).Value = "17"
For outRow = firstRow + 1 To lastRow
keyVal = Trim(CStr(wsOut.Cells(outRow, 2).Text))
If keyVal <> "" And dict.Exists(keyVal) Then
arr = dict(keyVal)
wsOut.Cells(outRow, 16).Value = CStr(arr(3))
wsOut.Cells(outRow, 17).Value = CStr(arr(11))
Else
wsOut.Cells(outRow, 16).Value = ""
wsOut.Cells(outRow, 17).Value = ""
End If
Next outRow
'合并单元格
Dim mergeStart As Long
Dim mergeVal As String
Dim curVal As String
mergeStart = firstRow + 1
mergeVal = Trim(CStr(wsOut.Cells(mergeStart, 16).Text))
For outRow = firstRow + 2 To lastRow + 1 ' 多循环一行,处理最后一段
If outRow <= lastRow Then
curVal = Trim(CStr(wsOut.Cells(outRow, 16).Text))
Else
curVal = vbNullString ' 哨兵值,强制收尾
End If
If curVal <> mergeVal Then
If outRow - 1 > mergeStart Then
With wsOut.Range(wsOut.Cells(mergeStart, 16), wsOut.Cells(outRow - 1, 16))
.Merge
.VerticalAlignment = xlCenter
End With
End If
mergeStart = outRow
mergeVal = curVal
End If
Next outRow
End Sub

