动态查找和更改列的值

时间:2018-02-02 11:40:05

标签: excel vba excel-vba

我必须在VBA Excel中制作简单的anonmizer。

enter image description here

我有关于图片的文件。我必须找到带有“代码”的动态列:

我正在打开文件:

Set wbSrc = Workbooks.Open(Filename:=filePath, ReadOnly:=True)

并查找带有“code”的列

For Each ws In wbSrc.Sheets
'find word "code" to set column number
 Set r = ws.Cells.Find(What:="code", After:=ws.Range("A1"), 
LookIn:=xlValues, LookAt:= _
 xlPart, SearchOrder:=xlByRows, 
SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)

如果我们找到变量的写号:

 If Not r Is Nothing Then
            codeCol= r.Column

知道我不知道如何在“代码”标题下选择所有内容。 将会有一些代码,其中包含大约16个我要剪切的数字

Left(rowHere,6)

当然,列中每个非空行的页数都是动态的。所以我需要的是引用“代码”下面的行和for循环,它会在每个非空行中改变值。

1 个答案:

答案 0 :(得分:2)

要查找包含数据的最后一行然后循环,下面的内容将执行此操作:

LastRow = ws.Cells(ws.Rows.Count, codeCol).End(xlUp).Row
'get the last row with data on Column codeCol

For i = r.row + 1 to LastRow 'loop from row below "code" to last on given column
    If ws.Cells(i, codeCol) <> "" then 'if cell is not blank
        ws.Cells(i, codeCol).Value = Left(ws.Cells(i, codeCol).Value, 6)
    End If
Next i