Excel VBA - 如果 B 列包含特定字母,则向 A 列添加连字符
Excel VBA - Add hyphens to column A if column B contains specific letter
请帮助新手。
使用 Excel VBA,我试图用连字符格式化 A 列中的文本,但前提是 B 列包含字母 B.
我找到了下面的代码,一个用连字符格式化 A 列中的单元格,另一个代码检查 B 列的值是否正确,但似乎无法将它们组合起来工作。请帮助。
谢谢。
Sub AddDashes()
Dim Cell As Range
On Error GoTo NoFilledCells
For Each Cell In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeConstants)
Cell.Value = Format(Replace(Cell.Value, "-", ""), "@@@@@-@@@-@@@@")
Next
NoFilledCells:
End Sub
和
Sub ChangeColumn()
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("B" & i).Value = "B" Then
Range("A" & i).Value = "Formatted text with hyphens as above"
End If
Next i
End Sub
Option Explicit
Sub AddDashes()
Dim ws As Worksheet, cell As Range
Dim LastRow As Long
Set ws = ActiveSheet
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For Each cell In ws.Range("A2:A" & LastRow)
If cell.Offset(0, 1) = "B" Then ' col B
cell.Value = Format(Replace(cell.Value, "-", ""), "@@@@@-@@@-@@@@")
End If
Next
End Sub
请帮助新手。 使用 Excel VBA,我试图用连字符格式化 A 列中的文本,但前提是 B 列包含字母 B.
我找到了下面的代码,一个用连字符格式化 A 列中的单元格,另一个代码检查 B 列的值是否正确,但似乎无法将它们组合起来工作。请帮助。 谢谢。
Sub AddDashes()
Dim Cell As Range
On Error GoTo NoFilledCells
For Each Cell In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeConstants)
Cell.Value = Format(Replace(Cell.Value, "-", ""), "@@@@@-@@@-@@@@")
Next
NoFilledCells:
End Sub
和
Sub ChangeColumn()
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("B" & i).Value = "B" Then
Range("A" & i).Value = "Formatted text with hyphens as above"
End If
Next i
End Sub
Option Explicit
Sub AddDashes()
Dim ws As Worksheet, cell As Range
Dim LastRow As Long
Set ws = ActiveSheet
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For Each cell In ws.Range("A2:A" & LastRow)
If cell.Offset(0, 1) = "B" Then ' col B
cell.Value = Format(Replace(cell.Value, "-", ""), "@@@@@-@@@-@@@@")
End If
Next
End Sub