我目前正在处理一个数据集,它被格式化为一个带有标题的表格。我需要做的是循环遍历特定列中的所有单元格并更改内容。通过对MSDN的研究,我提出了以下for循环
for i = 1 to NumRows
Cells(i,23).Value = "PHEV"
next i因此,这会将列23中的所有单元更改为"PHEV“。但是,我不会构建我自己正在使用的表,所以我不能保证我感兴趣的列将是列23。
我想实现类似于下面的东西:
for i = 1 to NumRows
Cells(i,[@[columnHeader]]).Value = "PHEV"
next i当然,我知道这种语法是不正确的,但希望它能充分说明我的目标。
发布于 2013-07-11 00:01:46
您可以在分配前搜索列:
Dim col_n as long
for i = 1 to NumCols
if Cells(1, i).Value = "column header you are looking for" Then col_n = i
next
for i = 1 to NumRows
Cells(i, col_n).Value = "PHEV"
next i发布于 2013-07-10 23:55:09
如果这实际上是一个ListObject表格(从功能区插入表格),那么您可以使用表格的.DataBodyRange对象来获取行数和列数。这会忽略标题行。
Sub TableTest()
Dim tbl As ListObject
Dim tRows As Long
Dim tCols As Long
Set tbl = ActiveSheet.ListObjects("Table1") '## modify to your table name.
With tbl.DataBodyRange
tRows = .Rows.Count
tCols = .Columns.Count
End With
MsgBox tbl.Name & " contains " & tRows & " rows and " & tCols & " columns.", vbInformation
End Sub如果需要使用标题行,只需使用tbl.Range,而不是使用tbl.DataBodyRange。
发布于 2015-10-14 22:33:49
假设您的表名为'Table1‘,您需要的列是' column’,您可以尝试这样做:
for i = 1 to Range("Table1").Rows.Count
Range("Table1[Column]")(i)="PHEV"
next ihttps://stackoverflow.com/questions/17574969
复制相似问题