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 –