[英]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.