我有一个自定义Sub。从列中删除数字。它非常简单,它要求用户(通过输入框)从列中删除数字。用户可以输入“B C D E F”,它将遍历B,C,D,E,F列并删除数字。
但是,我想从另一个sub调用这个sub,但不使用inputbox。我宁愿将我的专栏传递给子,即
Call removeNumbers(B,C,D,E,F)
并使用这些列运行我的removeNumbers宏。
如何设置我的removeNumbers
子以允许无限值传递(抱歉,如果这不是术语!)。因为我本来也可以完成B列。或B,C,D,E,F,G,H,I,...,ZZ。
我在想这样的事情:
Sub removeNumbers(Optional ByVal x as String, optional ByVal y as String, optional ByVal z as String)
但这只留下三个“传递变量”的空间。我怎样才能创建一个开放式的?像Sub removeNumbers(optional ByVal cols() as Array)
?
如果我能说清楚的话,请告诉我!
对于它的价值,这是我的removeNumbers
子:
Sub removeNumbers()
Dim ChosenColumn As String
Dim LastRowCol As String, i As Integer, cel, columnArray() As String
ChosenColumn = InputBox("What columns (A,B,C, etc.) would you like to remove numbers from? Use SPACES, to separate columns")
columnArray() = Split(ChosenColumn)
LastRowCol = InputBox("What column do you want to use to determine the last cell?")
Application.ScreenUpdating = False
With ActiveSheet
lastRow = .Cells(.Rows.Count, LastRowCol).End(xlUp).row
End With
Dim startCell As Range
For i = LBound(columnArray) To UBound(columnArray)
Debug.Print "Now going through column " & columnArray(i)
Column2Copy = columnArray(i)
For Each cel In Range(Cells(1, columnArray(i)), Cells(lastRow, columnArray(i)))
If IsNumeric(cel.Value) And Not IsEmpty(cel.Value) Then
cel.Value = ""
End If
Next
Next i
Application.ScreenUpdating = True
Dim runAgain
runAgain = MsgBox("Run again?", vbYesNo)
If runAgain = vbYes Then
Call removeNumbers
Else
MsgBox ("Done!")
End If
End Sub
Edit3:对于解决方案,@ Findwindow的链接是学习如何以编程方式执行此操作的绝佳资源。 @Rory的答案是一个简短而甜蜜的解决方案,所以两者都有效。谢谢大家!!