如何在Excel VBA的列中插入特定值?

我正在尝试在F列中每个文本的开头插入此字符“-”。

我的F coloum看起来像这样:

BP850
BP851
BT100
GP160
GP161

我已经尝试使用此代码,但未成功:

Option Explicit

Sub test()

    Dim LastRow As Long,i As Long
    Dim str As String

'Change sheet if needed
    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last row of column F
        LastRow = .Cells(.Rows.Count,"F").End(xlUp).Row

'Loop column F
        For i = 1 To LastRow
            'Replace the first occurance
            str = Replace(.Range("F" & i).Value,"","-",1,1)
            .Range("F" & i).Value = str
        Next i

    End With

End Sub

我期望的结果是这样:

-BP850
-BP851
-BT100
-GP160
-GP161

我真的希望在您的帮助下,谢谢您。

a771635193 回答:如何在Excel VBA的列中插入特定值?

作为替代,您可以尝试使用.Evaluate。这样可以避免任何循环:

enter image description here

Option Explicit

Sub test()

    Dim LastRow As Long
    Dim rng As Range

'Change sheet if needed
    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last row of column F
        LastRow = .Cells(.Rows.Count,"F").End(xlUp).Row
        Set rng = .Range("F1:F" & LastRow)
        rng.Value = .Evaluate("""'-""&" & rng.Address)
    End With

End Sub

enter image description here

,

代替使用替换,只需在str之前串联“-”

Sub test()

    Dim LastRow As Long,i As Long
    Dim str As String

'Change sheet if needed
    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last row of column F
        LastRow = .Cells(.Rows.Count,"F").End(xlUp).Row

'Loop column F
        For i = 1 To LastRow
            'Replace the first occurance
            str = "-" & .Range("F" & i).Value  '<== EDIT
            .Range("F" & i).Value = str
        Next i

    End With

End Sub
,

您的str = Replace(.Range("F" & i).Value,"","-",1,1)有问题。将其更改为以下内容:

    For i = 1 To LastRow
        .Range("F" & i).Value = "-" & .Range("F" & i).Value
    Next i

无需在此处使用str变量。

,

尝试一下,即。在值前面添加“'-”。注意-

之前的单引号
For i = 1 To lastRow
  If .Range("F" & i).Value <> "" Then
    If Left(.Range("F" & i).Value,1) <> "-" Then
      .Range("F" & i).Value = "'-" & .Range("F" & i).Value
    End If
  End If
Next i
,

查看以下方法是否有帮助。 它还会检查

  1. 单元格不是空白

  2. 单元格不是以“-”开头

在意外重新运行宏的情况下应该有帮助!

Option Explicit

Sub test2()
    Dim LastRow As Long,i As Long
    'Change sheet if needed
    Application.ScreenUpdating = False
    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last row of column F
         LastRow = .Cells(.Rows.Count,"F").End(xlUp).Row
        'Loop column F
        For i = 1 To LastRow
            If Len(.Range("F" & i).Value) > 0 And Left(.Range("F" & i).Value,1) <> "-" Then _
            .Range("F" & i).Value = "-" & .Range("F" & i).Value
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
,
Option Explicit

Sub test()

    Dim LastRow As Long,i As Long
    Dim AdditionString As String,CellString As String,CompleteString As String

    AdditionString = "'-"

    With ThisWorkbook.Worksheets("Sheet1")

         LastRow = .Cells(.Rows.Count,"A").End(xlUp).Row

         For i = 1 To LastRow

            CellString = .Range("A" & i).Value
            CompleteString = AdditionString & CellString
            .Range("A" & i).Value = CompleteString

         Next i

    End With

End Sub
本文链接:https://www.f2er.com/3168766.html

大家都在问