excel - 从不同的工作表中读取列标题,略有差异
问题描述
这是正确的方法吗?
我有一些来自不同供应商的表格,其中包含大部分重叠的产品列表。
我希望能够从这些供应商处读取数据并编制一份主表格,该表格可以比较这些供应商与我目前库存的产品的价格(并非供应商列出的所有产品都是我库存的产品)。
这样做的问题是,并非所有提供商都在其数据表中提供相同格式甚至精确的列标题(例如,价格、Price_$、Price1,作为来自不同提供商的列标题引用其数据中的同一列)。
有时,提供者甚至会更改他们显示标题的格式,公司名称会发生变化。例如一个月它的价格,下一个它的Price1。
我想到解决这个问题的方法是根据以前的变体编译一个潜在的标题名称列表,并尝试从供应商那里扫描新的价格表,并以此列表作为参考,以便正确识别列. 这将允许我在我的新主表中正确编译信息,然后能够执行价格检查等并在供应商中找到最优惠的价格。
自从我在 Excel 中做任何事情以来已经有一段时间了(最近主要是使用 Google Sheets)。据我所知,这将在 VBA 中。
我想我的问题是“这种方法看起来是正确的方法吗?VBA 是正确的编码方法吗?”
任何意见,指针或提示表示赞赏。
解决方案
相同数据的不同标头
- 我想这是几天前写的,我想是为了一个已经结束的问题。
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Loops through a list of strings ('Headers') and attempts to find
' a match in a cell of a row ('RowNumber') of a worksheet ('ws').
' Returns the column number of the cell of the first found string.
' Returns 0 if there is no match.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetFirstHeaderColumn( _
ByVal ws As Worksheet, _
ByVal RowNumber As Long, _
ParamArray Headers() As Variant) _
As Long
If ws Is Nothing Then Exit Function
If RowNumber < 1 Or RowNumber > ws.Rows.Count Then Exit Function
Dim rrg As Range: Set rrg = ws.Rows(RowNumber)
Dim cIndex As Variant
Dim n As Long
For n = LBound(Headers) To UBound(Headers)
cIndex = Application.Match(CStr(Headers(n)), rrg, 0)
If IsNumeric(cIndex) Then
GetFirstHeaderColumn = cIndex
Exit For
End If
Next n
End Function
- 您可以通过以下方式使用它。
Sub GetFirstHeaderColumnTEST()
Const FirstRow As Long = 1
Dim ws As Worksheet: Set ws = Sheet1
Dim Col1 As Long
Col1 = GetFirstHeaderColumn(ws, FirstRow, "Price", "Price1")
If Col1 > 0 Then
Debug.Print Col1
Else ' non of the headers were found
Debug.Print "Nope"
End If
Dim Col2 As Long
Col2 = GetFirstHeaderColumn(ws, FirstRow, "Sales", "Amount", "Sold")
If Col1 > 0 Then
Debug.Print Col2
Else ' non of the headers were found
Debug.Print "Nope"
End If
End Sub
推荐阅读
- authentication - 启用 Cloud Vision API 以访问 Cloud Storage 上的文件
- php - where 子句中的未知“sensorNamePerso”字段
- amazon-web-services - 雅典娜 vs Redshift Spectrum
- javascript - 使用纯 JavaScript 按钮来回更改文本
- c++ - C++ 如何在智能指针上使用 string::functions
- c# - 带有复选框的 Wpf MVVM 组合框并选择所有复选框
- node.js - 具有匹配、查找和项目的 mongodb 聚合
- android - Retrofit2 拦截器调用 2 API
- c++ - 在c ++中的结构内创建声明的数组a [1]的多个实例
- google-cloud-platform - Google BigQuery 对 AvrologicalTypes 的支持