Ahm*_*mad 5 excel vba excel-2007 excel-vba
我有以下宏需要循环Excel-2007表.该表有几列,我目前正在使用Index属性列找到正确的列位置.
使用索引是我能找到正确索引到fName对象的唯一方法.我希望的更好的选择是使用列名称/标题访问特定列.我怎么能这样做甚至可以做到这一点?
此外,一般来说,有没有更好的方法来构建这个循环?
Worksheets("Lists").Select
Dim filesToImport As ListObject
Dim fName As Object
Dim fileNameWithDate As String
Dim newFileColIndex As Integer
Dim newSheetColIndex As Integer
Set filesToImport = ActiveSheet.ListObjects("tblSourceFiles")
newFileColIndex = filesToImport.ListColumns("New File Name").Index // <- Can this be different?
For Each fName In filesToImport.ListRows // Is there a better way?
If InStr(fName.Range(1, col), "DATE") <> 0 Then
// Need to change the ffg line to access by column name
fileNameWithDate = Replace(fName.Range(1, newFileColIndex).value, "DATE", _
Format(ThisWorkbook.names("ValDate").RefersToRange, "yyyymmdd"))
wbName = OpenCSVFIle(fPath & fileNameWithDate)
CopyData sourceFile:=CStr(fileNameWithDate), destFile:=destFile, destSheet:="temp"
End If
Next fName2
Run Code Online (Sandbox Code Playgroud)
Sha*_*ter 33
我通过谷歌找到了这个,我发现它缺乏.所以我将填写更多信息,解释发生了什么,并稍微优化代码.
应该给你带来的明显答案是:
是的,可以做到.事实上,它比你想象的要简单.
我注意到你这样做了
newFileColIndex = filesToImport.ListColumns("New File Name").Index
Run Code Online (Sandbox Code Playgroud)
这给了你标题"新文件名"的索引.
然后,当您决定检查列时,您忘记了索引实际上也是相对列位置.
因此,您应该完成与以前相同的操作,而不是列号
InStr(fName.Range(1, filesToImport.ListColumns("Column Name")), "DATE")
Run Code Online (Sandbox Code Playgroud)
让我们深入挖掘,不仅用文字解释,而且用图片解释

在上图中,第一行显示绝对列索引,
其中A1的列索引为1,B1的列索引为2,依此类推.
所述ListObject的标头拥有自己的相关指标,其中,在该示例中,列1将具有列索引1,列2将具有列索引2,依此类推.这允许我们ListRow.Range在引用带有数字或名称的列时使用该属性.
为了更好地演示,这里是一个代码,用于打印上一个图像中"Column1" 的相对和绝对列索引.
Public Sub Example()
Dim wsCurrent As Worksheet, _
loTable1 As ListObject, _
lcColumns As ListColumns
Set wsCurrent = ActiveSheet
Set loTable1 = wsCurrent.ListObjects("Table1")
Set lcColumns = loTable1.ListColumns
Debug.Print lcColumns("Column1").Index 'Relative. Prints 1
Debug.Print lcColumns("Column1").Range.Column 'Absolute. Prints 3
End Sub
Run Code Online (Sandbox Code Playgroud)
由于ListRow.Range指的是范围,因此该范围在内部,因此成为相对论ListObject.

因此,例如,在每次迭代中引用Column2都ListRow可以这样做
Public Sub Example()
Dim wsCurrent As Worksheet, _
loTable1 As ListObject, _
lcColumns As ListColumns, _
lrCurrent As ListRow
Set wsCurrent = ActiveSheet
Set loTable1 = wsCurrent.ListObjects("Table1")
Set lcColumns = loTable1.ListColumns
For i = 1 To loTable1.ListRows.Count
Set lrCurrent = loTable1.ListRows(i)
'Using position: Range(1, 2)
Debug.Print lrCurrent.Range(1, 2)
'Using header name: Range(1, 2)
Debug.Print lrCurrent.Range(1, lcColumns("Column2").Index)
'Using global range column values: Range(1, (4-2))
Debug.Print lrCurrent.Range(1, (lcColumns("Column2").Range.Column - loTable1.Range.Column))
'Using pure global range values: Range(5,4)
Debug.Print wsCurrent.Cells(lrCurrent.Range.Row, lcColumns("Column2").Range.Column)
Next i
End If
Run Code Online (Sandbox Code Playgroud)
正如所承诺的,这是优化的代码.
Public Sub Code()
Dim wsCurrentSheet As Worksheet, _
loSourceFiles As ListObject, _
lcColumns As ListColumns, _
lrCurrent As ListRow, _
strFileNameDate As String
Set wsCurrentSheet = Worksheets("Lists")
Set loSourceFiles = wsCurrentSheet.ListObjects("tblSourceFiles")
Set lcColumns = loSourceFiles.ListColumns
For i = 1 To loSourceFiles.ListRows.Count
Set lrCurrent = loSourceFiles.ListRows(i)
If InStr(lrCurrent.Range(1, lcColumns("Column Name").Index), "DATE") <> 0 Then
strSrc = lrCurrent.Range(1, lcColumns("New File Name").Index).value
strReplace = Format(ThisWorkbook.Names("ValDate").RefersToRange, "yyyymmdd")
strFileNameDate = Replace(strSrc, "DATE", strReplace)
wbName = OpenCSVFile("Path" & strFileNameDate)
CopyData sourceFile:=CStr(strFileNameDate), _
destFile:="file", _
destSheet:="temp"
End If
Next i
End Sub
Run Code Online (Sandbox Code Playgroud)
个人经验.
MSDN
这是一个方便的功能:
Function rowCell(row As ListRow, col As String) As Range
Set rowCell = Intersect(row.Range, row.Parent.ListColumns(col).Range)
End Function
Run Code Online (Sandbox Code Playgroud)
如果要查找列标题中的特定值,可以使用 find 方法。find 方法返回一个范围,然后您可以将其用作执行其余操作的引用。find 方法有很多可选参数,如果您需要对其进行更多调整,请在帮助文档中阅读。
Dim cellsToSearch As Range
Dim foundColumn As Range
Dim searchValue As String
Set cellsToSearch = Sheet1.Range("A1:D1") ' Set your cells to be examined here
searchValue = "Whatever you're looking for goes here"
Set foundColumn = cellsToSearch.Find(What:=searchValue)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40252 次 |
| 最近记录: |