2017-04-06 85 views
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 

回答

0

相反的:

thisCell = Sheets("Names").Select: Range("B" & i).Select 

務必:

'## Use the SET keyword to assign an object variable 
Set thisCell = Sheets("Names").Range("B" & i) 

此外,而不是在函數體依靠ActiveCell,更改爲:

strTest = WorksheetFunction.Proper(thisCell.Value) 

然後調用你的功能:

Call arrayManip(thisCell) 

如果您功能沒有一個值返回給調用者,應該可能是一個Sub代替。將其更改爲Sub和上面的Call聲明仍應起作用。

參見:

How to make Excel VBA variables available to multiple macros?

+0

我結束了使用。選擇要使用的範圍內爲活動單元格,因此並不需要通過價值。 – m4sterbunny

+0

@ m4sterbunny可能在這裏工作,以及其他非常簡單的情況下,但它通常依賴於'ActiveCell'和'選擇'不被贊同。閱讀:http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros –