excel - Excel 2013 VBA - 查找特定标题并将下面的所有值相加
问题描述
问题 - 我想找到一个特定的标题 [例如。“不包括 GST 的金额”] 在工作表中并不总是在同一个位置,通常在前 5 行。然后我想将所有从下面的 1 个单元格开始的所有值相加到最后一个有值的单元格(有时只有 1 个单元格,其他 1000 个)并将特殊的值粘贴到另一个 ws 中:SourceShtClm.Range("D" & Last_Row).Value
我研究了 [ VBA - 查找具有特定标题的列并查找该列中所有行的总和并找到一些代码,但我正在努力修改以满足我的特定需求。
Sub Coles_straight_consolidation()
'Coles Straight Claims Import Macro
Dim SourceWB As Workbook 'Coles Consolidate Promo Claims
Dim SourceShtClm As Worksheet
Dim SourceShtPCD As Worksheet
Dim SourceShtFrml As Worksheet
Dim SourceShtMcrRng As Range
Dim SourceShtFrmlRng As Range
Dim FPath As String 'csv Folder containing raw data export
Dim fCSV As String
Dim wbCSV As Workbook
Dim wbMST As Workbook
Dim FiName As String 'saves promo claims file to new xls file
Dim FiPath As String
Dim StartTime As Double 'time elapsed counter
Dim MinutesElapsed As String
Dim xColIndex As Integer
Dim xRowIndex As Integer
Dim ws As Worksheet
Dim shtSrc As Worksheet
Dim f As Range
StartTime = Timer 'starts timer - Remember time when macro starts
NeedForSpeed 'speeds up macro
Set SourceWB = ThisWorkbook 'Set workbook
Set SourceShtMcr = SourceWB.Sheets("Macro") 'set worksheets
Set SourceShtClm = SourceWB.Sheets("Claim Summary")
Set SourceShtPCD = SourceWB.Sheets("Promo Claim Details")
FPath = ThisWorkbook.Path & "\csv_macro\" 'path to CSV files, include the final \
fCSV = Dir(FPath & "*.csv") 'start the CSV file listing
On Error Resume Next
Do While fCSV <> ""
Set wbCSV = Workbooks.Open(FPath & fCSV) 'opens workbook
Last_Row = SourceShtClm.Range("C" & Rows.Count).End(xlUp).Row + 1
SourceShtClm.Range("C" & Last_Row).Value = Range("G2").Value
SourceShtClm.Range("F" & Last_Row).Value = Range("L2").Value
SourceShtClm.Range("G" & Last_Row).Value = Range("Q2").Value
SourceShtClm.Range("H" & Last_Row).Value = Range("I2").Value
SourceShtClm.Range("I" & Last_Row).Value = Range("J2").Value
'Amount Excluding GST
Set shtSrc = wbCSV.Sheets(1)
Set f = shtSrc.UsedRange.Find(What:="Amount Excluding GST", After:=shtSrc.Range("A1"), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not f Is Nothing Then
Set pRng = shtSrc.Range(f.Offset(1, 0), _
shtSrc.Cells(shtSrc.Rows.Count, f.Column).End(xlUp))
Else
MsgBox "Required header 'Amount Excluding GST' not found!"
End If
SourceShtClm.Range("D" & Last_Row).Value = Application.WorksheetFunction.Sum(pRng)
'Amount Including GST
'copy code from above
wbCSV.Close SaveChanges:=False
fCSV = Dir 'ready next CSV
Loop
Set wbCSV = Nothing
SourceWB.Activate
SourceShtClm.Select
'Columns("B:J").AutoFit 'Auto fits Columns - update as not all col need auto fit
ActiveWorkbook.RefreshAll
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss") 'stops timer - Determine how many seconds code took to run
MsgBox "This code ran successfully in " & MinutesElapsed, vbInformation & " Make sure to save file as MMM Straights" 'Msg box for elapsed time & Claims consldaited
ResetSpeed
End Sub
解决方案
对于此类任务,这是一个很好的通用方法。Find()
请注意,在尝试访问找到的单元格的属性之前确保成功通常是一种很好的做法......
Dim shtSrc As Worksheet
Dim f As Range
Set shtSrc = wbCSV.Sheets(1)
Set f = shtSrc.UsedRange.Find(What:="Amount Excluding GST", After:=shtSrc.Range("A1"), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not f Is Nothing Then
Set pRng = shtSrc.Range(f.Offset(1,0), _
shtSrc.Cells(shtSrc.Rows.Count, f.Column).End(xlUp))
Else
Msgbox "Required header 'Amount Excluding GST' not found!"
End If
推荐阅读
- javascript - 如何在日期选择器中显示唯一即将到来的 3 个月
- visual-studio-code - 在 VS Code 中更改颜色主题后,建议和 Intellicode 不再起作用
- bluetooth - Zephyr 操作系统理解
- javascript - 有没有办法查看二维数组中的值是否与另一个二维数组中的任何值匹配?
- excel - 使用换行符设置范围等于多个命名范围
- php - 无法删除名为默认连接的数据库`symfony-project`驱动程序中发生异常:SQLSTATE [HY000] [2002]连接被拒绝
- python-3.x - python - 如何使用选项作为python中的coconstructor args动态地将成员添加到类中?
- java - 如何修复垂直 RecyclerView 内的水平 ViewPager2 和 RecyclerView 的滚动问题?
- ruby-on-rails - 作为守护进程启动时无法重新启动生产 Rails 服务器
- r - Flexdashboard 中循环内的 GGPlot