Alter/Change 单元格值的第 N 个元素 Excel VBA

Alter/Change Nth Element of Cell Value Excel VBA

我正在创建一个宏来反转单元格值的大写,以便更好地解释。

原值-

hh3crd220
xmi4Idc200
TEst02NoW

输出-

HH3CRD220
XMI4iDC200
teST02nOw

我认为一定已经有宏可以完成这项工作,但我自己编写了一个,除了更改第 n 个值外一切正常,Mid 不工作,因为它只会提取值,我试过 Character 但这只会格式化元素,我想要像 character.value 或 mid.value 这样的函数来工作。

Sub CapsChange()
Dim letr As String
Dim Val1 As String

Dim sr As Range
lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

Set sr = Range("A1:A" & lastrow)

For Each r In sr
Fval = r.Value
Val1 = Left(r.Value, 1)

If Val1 <> UCase(Val1) Then

For i = 1 To Len(Fval)
    letr = Mid(Fval, i, 1)
    If letr = UCase(letr) Then

**'First Code try**
    letr = LCase(letr)
**'Second Code try**
    r.Characters(i, 1).Value = LCase(letr)
    Else
    letr = UCase(letr)
    r.Characters(i, 1).Value = UCase(letr)
    End If
Next i

End If

Next

End Sub

需要帮助 changing/controlling 单元格值的第 n 个字符,就像我们使用 cell(x,y).value = XXX.

您正在查找 replace-函数 (See this link)。 一个例子:

Replace("abCabCde", "C", "c", , 1)

这将在 "abCabCde" 中找到第一个(也是唯一一个)出现的 "C" 并将其替换为 "c" 以获得 "abcabCde".

试试这个:

变体 1 使用 SUB()

Sub Test()
    Dim rng As Range, cl As Range, i&
    Set rng = Range("A1:A" & Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row)
    For Each cl In rng.Cells
        For i = Len(cl.Value) To 1 Step -1
            With cl.Characters(i, 1)
                If .Text = UCase(.Text) Then
                    .Text = LCase(.Text)
                ElseIf .Text = LCase(.Text) Then
                    .Text = UCase(.Text)
                End If
            End With
    Next i, cl
End Sub

变体 2 使用 Function()

Public Function ReverseCase(cl As Range)
    Dim StringOutput$, i&
    For i = Len(cl.Value) To 1 Step -1
        With cl.Characters(i, 1)
            If .Text = UCase(.Text) Then
                StringOutput = LCase(.Text) & StringOutput
            ElseIf .Text = LCase(.Text) Then
                StringOutput = UCase(.Text) & StringOutput
            End If
        End With
    Next i
    ReverseCase = StringOutput
End Function

函数测试()

两个变体都经过测试,工作正常

类似下面的函数会更容易重用!

以下是使用方法:

Option Explicit

Sub test_Angad_Arora()
Dim wS As Worksheet, _
    LastRow As Long, _
    i As Long

Set wS = ActiveSheet
With wS
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For i = 1 To LastRow
        .Cells(i, 1) = InvertCaseCore(.Cells(i, 1))
    Next i
End With
End Sub

以及反转输入字符串大写的函数:

Public Function InvertCaseCore(StringToReCapitalize As String)

Dim l As Integer, _
    c As String, _
    OutPut As String, _
    i As Integer

l = Len(StringToReCapitalize)
For i = 1 To l
     c = Mid(StringToReCapitalize, i, 1)
     If (c >= "A") And (c <= "Z") Then
         c = LCase(c)
     ElseIf (c >= "a") And (c <= "z") Then
         c = UCase(c)
     End If
     OutPut = OutPut & c
 Next i

    InvertCaseCore = OutPut
End Function

您可以使用 Mid statement ,它允许就地修改字符串:

Sub CapsChange()
    Dim letr As String
    Dim Val1 As String

    Dim sr As Range
    lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

    Set sr = Range("A1:A" & lastrow)

    For Each r In sr
        Fval = r.Value
        Val1 = Left(r.Value, 1)

        If Val1 <> UCase(Val1) Then

            For i = 1 To Len(Fval)
                letr = Mid(Fval, i, 1)
                If letr = UCase(letr) Then
                    Mid(Fval,i,1) = LCase(letr)
                else
                    Mid(Fval,i,1) = UCase(letr)
                End If
            Next i

        End If

    Next

End Sub