[英]Alter/Change Nth Element of Cell Value Excel VBA
I was creating a macro to reverse the capitalization of cell values, to explain better. 我正在创建一个宏以反转单元格值的大写形式,以进行更好的解释。
Original values- 原始值-
hh3crd220
xmi4Idc200
TEst02NoW
Output- 输出 -
HH3CRD220
XMI4iDC200
teST02nOw
I think there must already be macros which would do the job, but i was coding one myself, everything works fine except changing the nth value, Mid is not working since it will only extract the value, i tried Character but that will only format the element, i wanted something like character.value or mid.value function to work. 我认为必须已经有宏可以完成这项工作,但是我自己编写了一个代码,除了更改第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
Just Need Help changing/controlling the nth character of cell value, like we use cell(x,y).value = XXX. 就像我们使用cell(x,y).value = XXX一样,只需要帮助即可更改/控制单元格值的第n个字符。
try this: 尝试这个:
variant 1 using SUB() 使用SUB()的变体1
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
variant 2 using Function() 使用Function()的变体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
test for function() 测试功能()
both variants are tested, works fine 两种变体都经过测试,可以正常工作
Something like the below function would be far easier to re-use! 类似于下面的功能将更容易重用!
Here is how to use it : 这是使用方法:
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
And the function that invert the capatilization of the inputed string: 以及反转输入字符串的容量的函数:
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
You can use the Mid
statement , which allows in place modification of a string: 您可以使用
Mid
语句 ,该语句允许就地修改字符串:
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
You're looking for the replace
-Function ( See this link ). 您正在寻找
replace
-Function( 请参阅此链接 )。 An example: 一个例子:
Replace("abCabCde", "C", "c", , 1)
This will find the first (and only the first) occurrence of "C" in "abCabCde" and replaces it with "c" to get "abcabCde". 这将在“ abCabCde”中找到“ C”的第一个(也是第一个),并将其替换为“ c”以获得“ abcabCde”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.