0

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

开头文字:

  1. DTest dk;G3Gj
  2. _dsc0148A
  3. 1陶器2
  4. Articiart147
  5. 如果你不需要它
  6. Pressed_pleasure
  7. 在地上

期望的结果:

  1. Dtest Dk;G3gj
  2. _Dsc0148a
  3. 1陶器2
  4. Articiart147
  5. 如果你不需要它
  6. Pressed_Pleasure
  7. 在地上

尝试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. 1陶器2
  4. Articiart147
  5. 如果你不需要它
  6. Pressed_Pleasure
  7. 在地上

一些字符在数字或特殊字符之后得到大写。

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

尝试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. 1陶器2
  4. Articiart147
  5. 如果你不需要它
  6. Pressed_pleasure
  7. 在地上

在第二个和第三个字符中,第二个字符不是大写,而在 6 p 中,快乐也不是大写。

4

3 回答 3

1

这个/满足你的要求

更新:我添加了修复以解决正确大小写函数和撇号的限制

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
于 2013-07-08T12:20:17.167 回答
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

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

于 2013-07-07T17:53:15.750 回答
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
于 2017-03-04T19:42:55.960 回答