Excel 引用列以供以后在VBA中的循环中使用
我试图创建一个VBA宏,以便使用SUMIFS函数将数据从原始图纸导入图纸表数组。此函数需要循环列出的每个站点的列,并根据SUMIFS设置单元格的值 然而,我有一个问题,我相信这与我如何引用专栏有关 列查找部分应该查找第7行中包含“Total”的列左侧的列,然后将preCol设置为该列编号 我收到错误13:preCol=.Find(“Total”,After:=“OI7”,LookIn:=xlValues).Offset(0,-1).Column上的类型不匹配,这很有意义,但我想不出一种方法来查找列,然后根据该列的位置将其转换为整数 非常感谢您的任何建议或见解Excel 引用列以供以后在VBA中的循环中使用,excel,vba,Excel,Vba,我试图创建一个VBA宏,以便使用SUMIFS函数将数据从原始图纸导入图纸表数组。此函数需要循环列出的每个站点的列,并根据SUMIFS设置单元格的值 然而,我有一个问题,我相信这与我如何引用专栏有关 列查找部分应该查找第7行中包含“Total”的列左侧的列,然后将preCol设置为该列编号 我收到错误13:preCol=.Find(“Total”,After:=“OI7”,LookIn:=xlValues).Offset(0,-1).Column上的类型不匹配,这很有意义,但我想不出一种方法来查找
Option Explicit
Sub ImportFile()
'Select import file
On Error GoTo err
Dim importFilePath As String
Dim fileExplorer As FileDialog
Set fileExplorer = Application.FileDialog(msoFileDialogFilePicker)
With fileExplorer
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm; *.xlsb", 1
.Show
If .SelectedItems.Count > 0 Then
importFilePath = .SelectedItems.Item(1)
Else
GoTo err
MsgBox "Import cancelled."
End If
End With
'Beginning processes
Application.ScreenUpdating = False
Application.EnableEvents = False
'Defining and setting variables
'Loop variables
Dim i As Integer
Dim j As Integer
Dim s As Integer
'RAW workbook
Dim dataFile As Worksheet
Set dataFile = Workbooks.Open(importFilePath).Sheets("Cons Tx excluding credits")
'Worksheet variables
Dim wsBOS As Worksheet
Set wsBOS = ThisWorkbook.Sheets("FY19 Weekly Boston")
Dim wsMilford As Worksheet
Set wsMilford = ThisWorkbook.Sheets("FY19 Weekly Milford")
Dim wsMansfield As Worksheet
Set wsMansfield = ThisWorkbook.Sheets("FY19 Weekly Mansfield")
Dim wsSSH As Worksheet
Set wsSSH = ThisWorkbook.Sheets("FY19 Weekly SSH")
Dim wsLP As Worksheet
Set wsLP = ThisWorkbook.Sheets("FY19 Weekly Libbey Park")
Dim sheetArray As Variant
sheetArray = Array(wsBOS, wsMilford, wsMansfield, wsSSH, wsLP)
'SUMIF function variables
Dim sumIfRange As Range 'Quantity
Set sumIfRange = dataFile.Range("M:M")
Dim cSiteRange As Range 'Disease site
Set cSiteRange = dataFile.Range("AM:AM")
Dim criteriaSite As Range
Dim cDeptRange As Range 'Department
Set cDeptRange = dataFile.Range("B:B")
Dim criteriaDept As Range
Dim cTherapyRange As Range 'Therapy used
Set cTherapyRange = dataFile.Range("E:E")
Dim criteriaTherapy As Range
Dim c2TherapyRange As Range
Set c2TherapyRange = dataFile.Range("E:E")
Dim criteria2Therapy As Range
Dim cGlandGURange As Range
Set cGlandGURange = dataFile.Range("AM:AM")
Dim criteriaGlandGU As Range
'Insert before column containing "Total"
Dim f As Range
Dim firstAddress As String
For s = LBound(sheetArray) To UBound(sheetArray)
With sheetArray(s)
With .Rows(7).SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues)
Set f = .Find(what:="Total", LookIn:=xlValues, lookat:=xlWhole)
If Not f Is Nothing Then
firstAddress = f.Offset(, 1).Address '<-- offset by one column since f will be shifted one column to the right in subsequent statement
Do
f.EntireColumn.Insert
Set f = .FindNext(f)
Loop While f.Address <> firstAddress
End If
End With
End With
Next s
Dim preCol As Long
With Sheets("FY19 Weekly Boston")
With .Rows(7).SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues)
preCol = .Find("Total", After:="OI7", LookIn:=xlValues).Offset(0, -1).Column
End With
End With
For s = 1 To UBound(sheetArray)
With sheetArray(s)
For i = 8 To 21
Set criteriaDept = sheetArray(s).Cells("B7")
Set criteriaSite = sheetArray(s).Cells(i, 2)
Set criteriaTherapy = sheetArray(s).Cells("C6")
Set criteria2Therapy = sheetArray(s).Cells("C7")
sheetArray.Cells(i, preCol) = Application.WorksheetFunction.SumIfs(sumIfRange, cSiteRange, criteriaSite, cDeptRange, criteriaDept, cTherapyRange, criteriaTherapy) + Application.WorksheetFunction.SumIfs(sumIfRange, cSiteRange, criteriaSite, cDeptRange, criteriaDept, c2TherapyRange, criteria2Therapy)
Next i
End With
Next s
Set criteriaDept = Nothing
Set criteriaSite = Nothing
Set criteriaTherapy = Nothing
Set criteria2Therapy = Nothing
'Ending processes
Application.ScreenUpdating = True
Application.EnableEvents = True
err:
Exit Sub
End Sub
选项显式
子导入文件()
'选择导入文件
上错下错
将importFilePath设置为字符串
将fileExplorer设置为文件对话框
设置fileExplorer=Application.FileDialog(msoFileDialogFilePicker)
使用fileExplorer
.AllowMultiSelect=False
.Filters.Add“Excel文件”,“*.xls;*.xlsx;*.xlsm;*.xlsb”,1
显示
如果.SelectedItems.Count>0,则
importFilePath=.SelectedItems.Item(1)
其他的
后悔莫及
MsgBox“导入已取消。”
如果结束
以
"启动过程",
Application.ScreenUpdating=False
Application.EnableEvents=False
'定义和设置变量
'循环变量
作为整数的Dim i
作为整数的Dim j
将s变为整数
'原始工作簿
将数据文件设置为工作表
设置数据文件=工作簿.Open(导入文件路径).Sheets(“不包括学分”)
'工作表变量
将WSBO设置为工作表
设置wsBOS=ThisWorkbook.Sheets(“2019财年波士顿周报”)
将米尔福德作为工作表
设置wsMilford=ThisWorkbook.Sheets(“2019财年每周米尔福德”)
将字段设置为工作表
设置wsMansfield=ThisWorkbook.Sheets(“FY19每周Mansfield”)
将wsSSH设置为工作表
设置wsSSH=ThisWorkbook.Sheets(“FY19每周SSH”)
将wsLP设置为工作表
设置wsLP=ThisWorkbook.Sheets(“2019财年每周Libbey公园”)
作为变体的Dim数组
sheetArray=Array(wsBOS、wsMilford、wsMansfield、wsSSH、wsLP)
'SUMIF函数变量
Dim SUMIFFRANGE作为“范围”数量
设置sumIfRange=dataFile.Range(“M:M”)
Dim Csitrange作为范围内的疾病部位
设置cSiteRange=dataFile.Range(“AM:AM”)
作为范围的模糊标准
Dim Cdept Range作为“Range”部门
设置cDeptRange=dataFile.Range(“B:B”)
模糊标准范围
使用Dim cTherapyRange作为“范围”治疗
设置cTherapyRange=dataFile.Range(“E:E”)
模糊标准治疗范围
Dim c2TherapyRange As范围
设置c2TherapyRange=dataFile.Range(“E:E”)
Dim Criteria2治疗范围
暗cGlandGURange As范围
设置cGlandGURange=dataFile.Range(“AM:AM”)
模糊标准作为范围
'在包含“总计”的列之前插入'
变暗f As范围
将第一个地址设置为字符串
对于s=LBound(sheetArray)到UBound(sheetArray)
带单张纸阵列
带.Rows(7).SpecialCells(XlCellType.xlCellTypeConstants,xlTextValues)
设置f=.Find(what:=“总计”,LookIn:=xlValues,lookat:=xlother)
如果不是的话,那么f什么都不是
firstAddress=f.Offset(,1)。地址'类似于以下内容:
Dim f As Range, preCol As Long
With ActiveSheet.Rows(7)
'Range() below is *relative* to the With range
Set f = .Find("Total", After:=.Range("OI1"), LookIn:=xlValues)
If Not f Is Nothing Then
preCol = f.Column - 1
Else
'handle missing column header
End If
End With
Debug.Print preCol
它应该是Column
而不是Columns
。我不确定您是否可以在之后的中仅使用String
引用:=“OI7”
。。。此外,代码中是否有“下一步继续执行错误时的”
?如果是这样的话,就有可能发生了一个你看不到的错误。这是否编译过<代码>单元格(“B7”)
的语法无效<代码>范围(“B7”)
是正确的。另外-在Find()
loop.FYI中插入列时,我会非常小心。对于运行代码时出现的错误,“获取错误”并不是一个有用的描述。可能是因为您没有指定要查看的工作簿,所以出现了该错误。您应该始终完全限定所有范围/单元格/工作表引用,这样就不会有歧义,并且您的代码永远不应该依赖于某个工作簿/工作表在运行时处于活动状态。我将从SJR关于在尝试访问(例如)列
之前检查Find()结果的评论开始。将该行拆分为两部分:一部分用于查找单元格,另一部分用于获取列。为了安全起见,将所有整数类型交换为Long。