VBA将变量传递给函数

VBA将变量传递给函数,vba,excel,parameter-passing,Vba,Excel,Parameter Passing,最终目标:我正在尝试从大写到正确(大写)编辑列中的现有文本-例外情况是,如果单词后跟连字符,它将保持小写。我将它输出为两列 我的第一个子节点需要将活动单元格传递给应用格式的函数(该函数已测试并运行) 我不知道该怎么做 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,

最终目标:我正在尝试从大写到正确(大写)编辑列中的现有文本-例外情况是,如果单词后跟连字符,它将保持小写。我将它输出为两列

我的第一个子节点需要将活动单元格传递给应用格式的函数(该函数已测试并运行)

我不知道该怎么做

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
选项显式
以字符串形式显示文本
我想我会坚持多久
将strTest设置为字符串
Dim strArray()作为字符串
作为布尔值的Dim lCaseOn
第一排一样长,第一排一样长
将此单元格设置为范围
最后一排一样长
Sub-throughCols()
将此单元格设置为范围
数据范围
startIt=第一行+1
对于i=从开始到最后一行
“不知道如何将我正在接收的手机传送到我的职能部门
此单元格=工作表(“名称”)。选择:范围(“B”和i)。选择
arrayManip(此单元格)
'我需要将此ActiveCell传递给arrayManip-不确定如何传递
接下来我
端接头
'====================================================================
函数arrayManip(此单元格)
'用于测试的硬编码源单元
“现在,我正试图通过这个专栏
“清除所有数据
抹去条纹
txt=“”
'设置默认情况
lCaseOn=False
使用“”分隔符将“”字符串插入数组中
strTest=WorksheetFunction.property(ActiveCell.Value)
strTest=替换(strTest,“-”,“-”)
strArray=拆分(strTest,“”)
'通过数组查找以使文本成为foll
对于i=LBound(strArray)到UBound(strArray)
如果strArray(i)=“-”,则
lCaseOn=True
后藤下滴度
如果结束
如果是这样的话
strArray(i)=LCase(strArray(i))
lCaseOn=False
下一次滴定:
如果结束
'循环遍历数组并建立文本字符串以输出到消息框
txt=txt&strArray(一)和“
“删除空间
txt=修剪(替换(txt,“-”,“-”)
偏移量(0,2)。选择:ActiveCell.Value=txt
接下来我
'MsgBox txt
端函数
'====================================================================
子数据范围()
附页(“名称”)。列(“B”)
如果工作表function.CountA(.Cells)=0,则“而不是:

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

另外,不要依赖函数体中的
ActiveCell
,而是改为:

strTest = WorksheetFunction.Proper(thisCell.Value)
然后调用您的函数:

Call arrayManip(thisCell)
如果您的函数没有向调用者返回值,那么它可能应该是一个子函数。将其更改为
Sub
,上述
Call
语句仍应有效

另见:


我最终使用了。选择使用范围作为活动单元格,因此不需要传递值。@m4sterbunny在这里可能会起作用,以及其他非常简单的情况,但通常不赞成依赖
ActiveCell
select
。请继续阅读:
Call arrayManip(thisCell)