Excel VBA一个阵列上的两个函数

时间:2016-09-08 13:52:50

标签: excel vba excel-vba

我需要在字符串的开头,结尾处删除空格并使字符串成为正确的大小写。

我找到了两个脚本:

Sub Function01()

Dim arrData() As Variant
Dim arrReturnData() As Variant
Dim rng As Excel.Range
Dim lRows As Long
Dim lCols As Long
Dim i As Long, j As Long

  Range("R1", Range("R1").End(xlDown)).Select
  lRows = Selection.Rows.Count
  lCols = Selection.Columns.Count

  ReDim arrData(1 To lRows, 1 To lCols)
  ReDim arrReturnData(1 To lRows, 1 To lCols)

  Set rng = Selection
  arrData = rng.Value

  For j = 1 To lCols
    For i = 1 To lRows
      arrReturnData(i, j) = Trim(arrData(i, j))
      ///ADDING HERE(read below)

    Next i
  Next j

  rng.Value = arrReturnData

  Set rng = Nothing


End Sub

删除字符串和另一个脚本上的空格:

Sub ChangeCase()
    Dim Rng As Range
    On Error Resume Next
    Err.Clear
    Application.EnableEvents = False
    For Each Rng In Selection.SpecialCells(xlCellTypeConstants, _
             xlTextValues).Cells
        If Err.Number = 0 Then
            Rng.Value = StrConv(Rng.Text, vbProperCase)
        End If
    Next Rng
    Application.EnableEvents = True
End Sub

正在制作正确的字符串案例。这两个脚本正在使用范围来选择R列中的所有非空单元格。我需要在第一个脚本中创建函数第二个脚本。

在(/// ADDING HERE)点的第一个脚本中添加此代码:      arrReturnData(i,j)= StrConv(arrData(i,j),vbProperCase)

在Proper Case中输出我的输出但是有空格。 你们可以建议如何制作两个脚本函数吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这将完成整个没有循环:

Sub Function01()    

  Dim rng As Range   

  Set rng = Selection
  rng.Value = rng.Parent.Evaluate("INDEX(PROPER(TRIM(" & rng.Address & ")),)")    

End Sub

在:

enter image description here

后:

enter image description here