正确案例直到第一个实例","

时间:2014-02-23 18:20:02

标签: excel-vba vba excel

完全难以理解,我希望将列中的名称转换为正确的大小写,但我不想更改标题。

如何转换为适当的案例直到第一个,

BOB FEGESON
Sally Ran, Ph.D.
GREG HYMAN, MA, CPCC

我得到了

Bob Fegeson
Sally Ran, Ph.d.
Greg Hyman, Ma, Cpcc

Bob Fegeson
Sally Ran, Ph.D.
Greg Hyman, MA, CPCC

由于

如果If InStr(cell.Formula, ",") > 0

,则转换为正确的大小写
Sub FindChr()
Dim rAcells As Range
Dim rLoopCells As Range
Dim lReply As Long
Dim myRange As Range
Dim cell As Range

'Set variable to needed cells
Set rAcells = Range("D2", Range("D" & Rows.Count).End(xlUp))
Set rAcells = rAcells.SpecialCells(xlCellTypeConstants, xlTextValues)

Set myRange = Range("D2", Range("D" & Rows.Count).End(xlUp))

For Each cell In myRange
    If InStr(cell.Formula, ",") > 0 Then

    "Cant Not Figure out what goes here"

    Else
        ' Convert to Proper Case
      For Each rLoopCells In rAcells
          rLoopCells = StrConv(rLoopCells, vbProperCase)
      Next rLoopCells
    End If
  Next cell
End Sub

1 个答案:

答案 0 :(得分:1)

试试这个

Sub FindChr()
    Dim ws As Worksheet
    Dim myRange As Range, cell As Range
    Dim tmpString As String
    Dim MyString As Variant
    Dim i As Long

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        Set myRange = .Range("D2", .Range("D" & .Rows.Count).End(xlUp))

        For Each cell In myRange
            If InStr(1, cell.Formula, ",") Then
                MyString = Split(cell.Formula, ",")

                tmpString = StrConv(MyString(0), vbProperCase)

                For i = 1 To UBound(MyString)
                    tmpString = tmpString & "," & MyString(i)
                Next i

                cell.Formula = tmpString
            Else
                cell.Formula = StrConv(cell.Formula, vbProperCase)
            End If
        Next cell
    End With
End Sub

enter image description here