简单的循环遍历VBA中的ActiveCell行

我在VBA中写了一个非常基本的macros,这是我第一次写任何东西,所以我挣扎了很多。 我完全知道我需要做什么的逻辑,我还没有得到VBA的语法。 如果该行中的第一个单元格包含特定的子string,我想遍历一行中的每个单元格。 在for循环中,如果该单元格不是空的,我想将该子string追加到行中每个单元格的末尾。 我没有越过我的循环声明

Sub changeRow() Dim txt As String txt = Cells(ActiveCell.Row, 1) If InStr(1, txt, "123") > 0 Then For Each b In Range(Rows(ActiveCell.Row).Select) If Len(b.Value) > 0 Then b.Value = b.Value & " 123" End If Next b End If End Sub 

单程

 Sub changeRow() Const sTxt As String = "123" Dim cell As Range With Cells(ActiveCell.Row, "A") If VarType(.Value) = vbString Then If InStr(.Value, sTxt) Then For Each cell In .EntireRow.SpecialCells(xlCellTypeConstants, xlTextValues) cell.Value = cell.Value & " " & sTxt Next cell End If End If End With End Sub 

这也会将“123”添加到它所在的单元格中,但不会添加到包含数字或公式的单元格中。

编辑:在上面的代码中有一个秃头的错误。 它应该testing列A中的单元格不包含公式:

 Sub changeRow() Const sTxt As String = "123" Dim cell As Range With Cells(ActiveCell.Row, "A") If VarType(.Value) = vbString And Not .HasFormula Then If InStr(.Value, sTxt) Then For Each cell In .EntireRow.SpecialCells(xlCellTypeConstants, xlTextValues) cell.Value = cell.Value & " " & sTxt Next cell End If End If End With End Sub 

否则,SpecialCells行可能会产生运行时错误。

这就是你想要的,而不需要引入任何新的variables(尽pipe我确实将无意义的“b”更改为cell

 Sub changeRow() Dim txt As String Dim cell As Excel.Range txt = ActiveCell.EntireRow.Cells(1) If InStr(1, txt, "123") > 0 Then For Each cell In Intersect(ActiveCell.EntireRow, ActiveCell.Parent.UsedRange) If Len(cell.Value) > 0 Then cell.Value = cell.Value & " 123" End If Next cell End If End Sub 

它也不会留下任何不合格的variables范围,这是一个好主意。 通常你会避免通过引入像ws这样的variables来引用你正在处理的表单。 但是这个代码很简单,基于Activecell所以我只是用Activecell.Parent来代替。

我看到你find了解决办法。 我只是坚持“你是VBA新手”这个事实。 如果您有疑问,请随时发表评论。 保持大部分原有的逻辑,

 '-- this line is very important, it keeps "in order" to write code '-- Forces explicit declaration of all variables in a file, or allows implicit declarations of variables. '-- please explore on that Option Explicit '-- writing this with more comments since you said you are new to VBA Sub changeRow() Dim ws As Worksheet Dim b As Range Dim activeRange As Range Dim fullRange As Range Dim lastColumn As Long Dim txt As String '-- set current sheet into a reference object variable called WS Set ws = ActiveWorkbook.Sheets("Sheet1") '-- similarly the range you plan to start the validation Set activeRange = ws.Range("B2") txt = activeRange.Value2 '-- get last column in the row, 2 refers to rows 2 where you have data lastColumn = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column '-- get range until the last column in the row Set fullRange = Sheets("Sheet1").Range("B2").Resize(, lastColumn) '-- this allows you to view the Address of this range in the Immediate Window, just to make sure you get the desired range right Debug.Print fullRange.Address If InStr(1, txt, "123") > 0 Then '-- iterating through each range in full range that we specified above For Each b In fullRange '-- there's no need to check this because when InStr() fails, '-- the compiler will not come to this validation! If Len(b.Value) > 0 Then '-- b.Offset(3) refers to 3 rows down from cell B2 which is B5 '-- output into 5th row for demonstration purpose '-- you may set it back to b.Value per your requirement. '-- Please explore offset property of Range object b.Offset(3).Value = b.Value & " 123" End If Next b End If '-- release the memory allocated to these reference object variables '-- the order is: first ranges, then worksheet Set activeRange = Nothing Set fullRange = Nothing Set ws = Nothing End Sub 

在这里输入图像说明