0

Final Goal: I am trying to edit existing text in a column from UPPER CASE to Proper (Upper Case)- With an exception whereby if the word follows a hyphen It Will-remain Lower Case. I am outputting it 2 columns across.

My first sub needs to pass my active cell to my Function that applies the formatting (function was tested and works).

I don't know how to make that work.

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
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
m4sterbunny
  • 87
  • 1
  • 2
  • 8

1 Answers1

1

Instead of:

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

Do:

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

Also, instead of relying on ActiveCell in the function body, change to:

strTest = WorksheetFunction.Proper(thisCell.Value)

Then call your function:

Call arrayManip(thisCell)

If your function doesn't return a value to the caller, it should probably be a Sub instead. Change it to a Sub and the above Call statement should still work.

See also:

How to make Excel VBA variables available to multiple macros?

Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • I ended up using .select to use the range as the active cell therefore did not need to pass value. – m4sterbunny Apr 06 '17 at 17:08
  • @m4sterbunny that might work here, and other very simple cases, but it's generally frowned upon to rely on `ActiveCell` and `Select`. Read on: http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros – David Zemens Apr 06 '17 at 17:13