使用VBA转换字母大小写

时间:2013-07-07 11:45:15

标签: excel vba

我正在编写VBA宏,它将在适当的情况下转换字符串,但有一些限制。

开头文字:

  1. DTest dk; G3Gj
  2. _dsc0148A
  3. 1pottery2
  4. Articiart147
  5. 如果你没有接受它
  6. Pressed_pleasure
  7. On_The-地板
  8. 期望的结果:

    1. Dtest Dk; G3gj
    2. _Dsc0148a
    3. 1Pottery2
    4. Articiart147
    5. 如果你不需要它
    6. Pressed_Pleasure
    7. On_The-地板
    8. 尝试1:

      Sub ChangePCase()
       rownum = 2
       colnum = 1
      
       ActiveSheet.Range("a2", ActiveSheet.Range("a2").End(xlDown)).Select
      
           For Each Cell In Selection
             mystr = Cells(rownum, colnum).value
      
              Text = WorksheetFunction.Proper(mystr)
              Cells(rownum, colnum + 1) = Text
              rownum = rownum + 1
           Next
      End Sub
      

      结果

      1. Dtest Dk; G3Gj
      2. _Dsc0148A
      3. 1Pottery2
      4. Articiart147
      5. 如果你不需要它
      6. Pressed_Pleasure
      7. On_The-地板
      8. 某些角色在数字或特殊字符后获得资金。

        如何限制特殊字符后的字符不是大写字母,但如果单词的第一个字符是特殊字符,那么第二个字符应该是大写字母作为第二个字符。

        尝试2:

        我的其他代码提供了一些更好的结果,但它不会在特殊字符后大写单词的第二个字符。带输出的代码是

        Sub Propername()
        rownum = 2
        colnum = 1
        
        ActiveSheet.Range("a2", ActiveSheet.Range("a2").End(xlDown)).Select
        
        For Each Cell In Selection
        
         Text = Trim(StrConv(Cells(rownum, colnum).value, vbProperCase))
         Cells(rownum, colnum + 1) = Text
        rownum = rownum + 1
        Next
         End Sub
        

        结果

        1. Dtest Dk; k3kj
        2. _dsc0148a
        3. 1pottery2
        4. Articiart147
        5. 如果你不需要它
        6. Pressed_pleasure
        7. On_the#FL#OOR
        8. 第二和第三个角色不是资本,而6 p的快乐也不是资本。

3 个答案:

答案 0 :(得分:1)

/ 符合您的要求

更新:我添加了修补程序以解决Proper case function and apostrophes 的限制

Sub NotScared_butConfused()
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim X
Dim lngCnt As Long

X = Range([a2], Cells(Rows.Count, "A").End(xlUp))
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "([\_\-\$;:\s])+([a-z])"
.Global = True
For lngCnt = 1 To UBound(X)
X(lngCnt, 1) = Application.Proper(LCase$(X(lngCnt, 1)))
If .test(X(lngCnt, 1)) Then
Set objRegMC = .Execute(X(lngCnt, 1))
For Each regm In objRegMC
Mid$(X(lngCnt, 1), regm.firstindex + 2, 1) = UCase$(regm.submatches(1))
Next
End If
With Application
X(lngCnt, 1) = .Substitute(.Proper(.Substitute(X(lngCnt, 1), "'", "zyx")), "zyx", "'")
End With
Next
End With
If lngCnt > 1 Then [b2].Resize(UBound(X), 1) = X
End Sub

答案 1 :(得分:0)

我不知道为什么人们害怕或无法理解这个问题。

excel的正确命令存在编程问题,因为它没有正确更改字符串的情况,例如在excel的单元格中写入此命令

=proper("If you don't Need It")

你会看到没有引号的结果

"If You Don'T Need It"

对于这个输出,人们永远不会害怕。

我编写了以下代码。

Sub Propername()
rownum = 2
colnum = 1

ActiveSheet.Range("a2", ActiveSheet.Range("a2").End(xlDown)).Select

For Each Cell In Selection
   FullName = Cells(rownum, colnum).value
'capitalize the first letter after _. 
   For i = 1 To Len(FullName)
     pos = InStr(1, FullName, "_", vbTextCompare)

    If pos > 1 Then
     text1 = Trim(StrConv(Mid(FullName, 1, pos), vbProperCase))
     text2 = Trim(StrConv(Mid(FullName, pos + 1, 999), vbProperCase))
     Cells(rownum, colnum + 1) = text1 & text2
     cont = 1
    If InStr(1, text2, "_") Then
     pos1 = InStr(1, text2, "_")
     text3 = Trim(StrConv(Mid(text2, 1, pos1), vbProperCase))
     text4 = Trim(StrConv(Mid(text2, pos1 + 1, 999), vbProperCase))
     Cells(rownum, colnum + 1) = text1 & text3 & text4
    End If
   End If
 If pos = 0 Or InStr(1, text2, "_") = False Then cont = 0: Exit For Next
 'capitalize the second letter as capital if first letter is a digit or some special character in string
 Select Case Left(Cells(rownum, colnum).value, 1)
   Case "_", "#", "$", "-", "'", ";", ":"
      Text0 = Mid(FullName, 1, 1)
      text1 = StrConv(Mid(FullName, 2, 1), vbProperCase)
      text2 = Trim(StrConv(Mid(FullName, 3, 999), vbLowerCase))
      Cells(rownum, colnum + 1) = Text0 & text1 & text2
   Case 0 To 9
      Text0 = Mid(FullName, 1, 1)
      text1 = StrConv(Mid(FullName, 2, 1), vbProperCase)
      text2 = Trim(StrConv(Mid(FullName, 3, 999), vbLowerCase))
      Cells(rownum, colnum + 1) = Text0 & text1 & text2

 Case Else
    If cont <> 0 Then
     Text = Trim(StrConv(FullName, vbProperCase))
     Cells(rownum, colnum + 1) = Text
   End If
 End Select

rownum = rownum + 1
Next
 End Sub

我可以改进,特别是如果可以检测到字符串中特殊字符的位置。

答案 2 :(得分:0)

Function mySC(txt, how) As String 'ST = string convert
'vbUpperCase   1   Converts the string to all uppercase.
'vbLowerCase   2   Converts the string to all lowercase.
'vbProperCase  3   Converts the first letter to every word to uppercase. All other characters are left as lowercase.
'vbUnicode     64  Converts the string to Unicode.
'vbFromUnicode 128 Converts the string from Unicode to the default code page of the system.
'
    mySC = StrConv(txt, how)
End Function