[英]Simple loop iterating over row of ActiveCell in VBA
我正在VBA中编写一个非常基本的宏,这是我第一次在其中编写任何内容,因此我很费劲。 我完全知道我需要做的逻辑,只是我还没有获得VBA的语法。 如果该行的第一个单元格包含特定的子字符串,我想遍历该行的每个单元格。 在for循环内,如果该单元格不为空,我想将该子字符串附加到该行中每个单元格的末尾。 我没有超越我的for循环声明
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”的单元格中添加“ 123”,但不将其添加到包含数字或公式的单元格中。
编辑:上面的代码中有一个秃头的错误。 它应该测试col 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行可能会生成运行时错误。
这可以实现您想要的,而无需引入任何新变量(尽管我确实将无用的“ 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
它还不会留下任何不合格的变量范围,这是一个好主意。 通常,您可以通过引入ws
类的变量来引用您要处理的工作表来避免这种情况。 但是这段代码很简单,并且基于Activecell
因此我只使用Activecell.Parent
。
我看到您找到了解决方案。 我只是坚持“您是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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.