簡體   English   中英

在單獨的子例程中將子例程的輸出用作變量

[英]Using a subroutine's output as a variable in a separate subroutine

基本上我想在這里做的就是使用這個子:

Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String

sourceCol = ActiveCell.Column   'Uses ActiveCell.Column as reference now, but needs to fit into each Subroutine to select next available
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row

'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
        Cells(currentRow, sourceCol).Select
        Exit For
    End If
Next
End Sub

要查找列中的下一個空單元格,以將該子字符串輸入到該字符串中。

Set selRange = Selection

For i = 0 To ListBox1.ListCount - 1
  If ListBox1.Selected(i) = True Then
     If strApps = "" Then
      strApps = ListBox1.List(i)
      intAppCodeOffset = i
      strAppCodeVal = Worksheets("TestSheet").Range("B31").Offset(i, 0).Value
    Else
      strApps = strApps & ", " & ListBox1.List(i)
      intAppCodeOffset = i
      strAppCodeVal = strAppCodeVal & ", " & Worksheets("TestSheet").Range("B31").Offset(i, 0).Value
    End If
  End If
Next

Set selRange = selRange.Offset(1, 0)

With selRange
  selRange.Value = strAppCodeVal
End With

我嘗試用SelectFirstBlankCell替換selRage.Offset(1, 0) ,但是每次都會收到對象引用錯誤。 任何幫助都將不勝感激,因為我似乎在這里找不到如何做。

如以上注釋中所述,嘗試將Sub更改為Function ,如下所示:

Function SelectFirstBlankCell(sourceCol as Integer) as Range

(刪除舊的sourceCol暗淡和分配)

...
        Set SelectFirstBlankCell = Cells(currentRow, sourceCol)
...
End Function

然后,您可以進行更改:

Set selRange = SelectFirstBlankCell(ActiveCell.Column) 'Or whatever you think should be defined as the sourceCol

您的代碼本身可能不應該選擇任何內容,除非那是宏的最終結果。 嘗試使用代碼代替直接操作單元格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM