I'm writing a pretty basic macro in VBA and this is my first time writing anything in it, so I'm struggling a lot. I completely know the logic of what I need to do, I just don't get VBA's syntax yet. I want to iterate over each cell in a row if the first cell in that row contains a particular substring. Inside the for-loop, I want to append that substring to the end of each cell in the row if that cell isn't empty. I'm not getting past my for-loop declaration
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
One way
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
That will also add " 123" to the cell in which it is found, but not to cells containing numbers or formulas.
EDIT: There's a bald mistake in the code above. It should test that the cell in col A does not contain a formula:
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
Otherwise the SpecialCells line could generate a runtime error.
This does what you want without introducing any new variables (although I did change the uninformative "b" to 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
It also doesn't leave any unqualified variable ranges, which is a good idea. Normally you'd avoid that by introducing a variable like ws
to refer to the sheet you are dealing with. But this code is simple and based on the Activecell
so I just used Activecell.Parent
instead.
I see you have found a solution. I am just sticking to the fact that "your are new to VBA". If you have doubts, feel free to comment. Keeping most of your original logic in place,
'-- 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.