写一个去掉D列单元格内最后一位符号- 的VBA代码
时间: 2024-06-04 07:12:40 浏览: 65
Sub RemoveLastSymbol()
Dim lastRow As Long
Dim lastCol As Long
Dim i As Long
Dim j As Long
Dim cellValue As String
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To lastRow
For j = 1 To lastCol
If j = 4 Then 'D列
cellValue = ActiveSheet.Cells(i, j).Value
If Right(cellValue, 1) = "-" Then
ActiveSheet.Cells(i, j).Value = Left(cellValue, Len(cellValue) - 1)
End If
End If
Next j
Next i
End Sub
相关问题
写一个把D列单元格内的()替换成-,并去掉最后一位符号-的VBA代码
Sub replace_parentheses()
Dim lastRow As Long
lastRow = Range("D" & Rows.Count).End(xlUp).Row '获取最后一行的行号
For i = 1 To lastRow '循环每一行
If InStr(1, Range("D" & i), "(") > 0 Then '判断是否有(符号
Range("D" & i) = Replace(Range("D" & i), "(", "-") '替换
End If
If Right(Range("D" & i), 1) = "-" Then '判断最后一位是否为-符号
Range("D" & i) = Left(Range("D" & i), Len(Range("D" & i)) - 1) '去掉最后一位
End If
Next i
End Sub
写一个把D列单元格内的()替换成-,然后最后一位符号- 去掉的VBA代码
Sub ReplaceParentheses()
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
For i = 1 To lastRow
If Right(ActiveSheet.Range("D" & i).Value, 1) = "-" Then
ActiveSheet.Range("D" & i).Value = Replace(ActiveSheet.Range("D" & i).Value, "(", "-")
ActiveSheet.Range("D" & i).Value = Replace(ActiveSheet.Range("D" & i).Value, ")", "-")
ActiveSheet.Range("D" & i).Value = Left(ActiveSheet.Range("D" & i).Value, Len(ActiveSheet.Range("D" & i).Value) - 1)
Else
ActiveSheet.Range("D" & i).Value = Replace(ActiveSheet.Range("D" & i).Value, "(", "-")
ActiveSheet.Range("D" & i).Value = Replace(ActiveSheet.Range("D" & i).Value, ")", "-")
End If
Next i
End Sub
阅读全文