0
最终目标:我试图编辑现有的文本从大写字母的列到正确(大写) - 有一个例外,如果这个单词后跟一个连字符它会保持小写。我输出2列。VBA通过变量功能
我的第一个子需要将我的活动单元格传递给我的函数,该函数应用了格式(函数已经过测试和工作)。
我不知道如何使这项工作。
Option Explicit
Dim txt As String
Dim i As Long
Dim strTest As String
Dim strArray() As String
Dim lCaseOn As Boolean
Dim firstRow As Long, startIt As Long
Dim thisCell As Range
Dim lastRow As Long
Sub throughCols()
Dim thisCell As Range
dataRange
startIt = firstRow + 1
For i = startIt To lastRow
' No idea how to pass my cell I am picking up through to my function
thisCell = Sheets("Names").Select: Range("B" & i).Select
arrayManip (thisCell)
'I need to pass this ActiveCell to arrayManip- not sure how
Next i
End Sub
'====================================================================
Function arrayManip(thisCell)
'Hard coded source cell for testing
' Now trying to itterate through column
' clear out all data
Erase strArray
txt = ""
'set default case
lCaseOn = False
' string into an array using a " " separator
strTest = WorksheetFunction.Proper(ActiveCell.Value)
strTest = Replace(strTest, "-", " - ")
strArray = Split(strTest, " ")
' itterate through array looking to make text foll
For i = LBound(strArray) To UBound(strArray)
If strArray(i) = "-" Then
lCaseOn = True
GoTo NextIteration
End If
If lCaseOn Then
strArray(i) = LCase(strArray(i))
lCaseOn = False
NextIteration:
End If
' loop through the array and build up a text string for output to the message box
txt = txt & strArray(i) & " "
' remove the space
txt = Trim(Replace(txt, " - ", "-"))
ActiveCell.Offset(0, 2).Select: ActiveCell.Value = txt
Next i
' MsgBox txt
End Function
'====================================================================
Sub dataRange()
With Sheets("Names").Columns("B")
If WorksheetFunction.CountA(.Cells) = 0 Then '<--| if no data whatever
MsgBox "Sorry: no data"
Else
With .SpecialCells(xlCellTypeConstants) '<--| reference its cells with constant (i.e, not derived from formulas) values)
firstRow = .Areas(1).Row
lastRow = .Areas(.Areas.Count).Cells(.Areas(.Areas.Count).Rows.Count).Row
End With
' MsgBox "the first row is " & firstRow
' MsgBox "last row is " & lastRow
End If
End With
End Sub
我结束了使用。选择要使用的范围内为活动单元格,因此并不需要通过价值。 – m4sterbunny
@ m4sterbunny可能在这里工作,以及其他非常简单的情况下,但它通常依赖于'ActiveCell'和'选择'不被赞同。阅读:http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros –