循环遍历表列中的所有行,Excel-VBA [英] Looping through all rows in a table column, Excel-VBA
问题描述
我目前正在处理一个数据集,格式为表,标题。我需要做的是循环遍历特定列中的所有单元格,并更改内容。通过对MSDN的研究,我想出了以下循环
I'm currently working on a data set which is formatted as a table, with headers. What I need to do is cycle through all cells in a specific column and change the contents. Through research on MSDN I came up with the following for loop
for i = 1 to NumRows
Cells(i,23).Value = "PHEV"
next i
所以这会改变所有的单元格在第23列中读取PHEV。但是,我不建立与我自己合作的表,所以我不能保证我感兴趣的列将是第23列。
So this would change all the cells in column 23 to read "PHEV". However, I do not build the table I'm working with myself, so I can't guarantee that the column I'm interested in will be column 23.
我'd喜欢实现类似于以下内容:
I'd like to implement something similar to the following:
for i = 1 to NumRows
Cells(i,[@[columnHeader]]).Value = "PHEV"
next i
我知道这个语法是不正确的,但希望它充分说明了我的目标。
Of course, I know that that syntax is incorrect, but hopefully it sufficiently illustrates my goal.
推荐答案
您可以在作业前搜索列: p>
You can search column before assignments:
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
这篇关于循环遍历表列中的所有行,Excel-VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!