我正在VBA写一个非常基本的宏,这是我第一次写任何东西,所以我很挣扎。我完全知道我需要做什么的逻辑,我还没有得到VBA的语法。如果该行中的第一个单元格包含特定的子字符串,我想迭代遍历每个单元格。 在for循环中,如果该单元格不为空,我想将该子字符串附加到行中每个单元格的末尾。我没有通过我的for-loop声明
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
答案 0 :(得分:2)
单程
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"到找到它的单元格,但不包含包含数字或公式的单元格。
编辑:上面的代码中有一个光头错误。它应该测试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行可能会生成运行时错误。
答案 1 :(得分:0)
这可以做你想要的而不引入任何新的变量(虽然我确实改变了无信息的" 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
。
答案 2 :(得分:0)
我看到你找到了解决方案。我只是坚持“你是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