我正在创建一个宏来反转单元格值的大写,以便更好地解释。
原始值 -
hh3crd220
xmi4Idc200
TEst02NoW
输出 -
HH3CRD220
XMI4iDC200
teST02nOw
我认为必须已经有宏可以完成这项工作,但我自己编码,除了更改第n个值之外一切正常,中不起作用,因为它只会提取值,我尝试字符,但这只会格式化元素,我想要像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
只需要帮助更改/控制单元格值的第n个字符,就像我们使用单元格(x,y).value = XXX。
答案 0 :(得分:2)
试试这个:
使用SUB() 变体1
使用Function() 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
测试功能() 两种变体都经过测试,效果很好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
答案 1 :(得分:1)
像下面这样的功能会更容易重复使用!
以下是如何使用它:
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
反转输入字符串的capatilization的函数:
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
答案 2 :(得分:1)
您可以使用Mid
statement,它允许修改字符串:
Shopify -> Admin Panel -> Online Store -> Themes -> Edit HTML/CSS
答案 3 :(得分:-1)
您正在寻找replace
- 功能(See this link)。
一个例子:
Replace("abCabCde", "C", "c", , 1)
这将找到&#34; C&#34;的第一个(也是唯一的第一个)事件。 in&#34; abCabCde&#34;并将其替换为&#34; c&#34;获得&#34; abcabCde&#34;。