对象必需的错误VBA函数

时间:2017-01-16 00:04:28

标签: excel vba excel-vba

我本周末开始使用Macros(我倾向于在计算机方面快速使用)。到目前为止,当我有疑问时,我已经能够找到答案,但我的理解是如此有限,我已经到了不再理解答案的地步。我正在使用VBA for Excel编写一个函数。我希望函数能够产生一个范围,然后可以将其用作另一个函数的变量。这是我的代码:

Function StartingCell() As Range

Dim cNum As Integer
Dim R As Integer
Dim C As Variant

C = InputBox("Starting Column:")
R = InputBox("Starting Row:")

cNum = Range(C & 1).Column

Cells(R, cNum).Select

此处的代码有效。它选择了细胞,一切都在世界上很好。

Set StartingCell = Range(Cell.Address)
End Function

我想我不知道如何将此位置保存为StartingCell()。我使用了与在“= Range(Cell.Address)”的另一个非常相似的情况下看到的相同的代码。但那不是在这里工作。有任何想法吗?我是否需要提供更多信息以获得帮助?感谢您的投入!

编辑:我忘了添加我正在使用InputBox选择起始单元格,因为我将重复使用此代码与多个数据集,并且需要将每个数据集放在不同的位置,每次都会跟随相同的人口模式。

谢谢A.S.H& Shai Rado

我已将代码更新为:

Function selectQuadrant() As Range

Dim myRange As Range
Set myRange = Application.InputBox(Prompt:="Enter a range: ", Type:=8)

Set selectQuadrant = myRange


End Function

这很好用。 (似乎文本应该显示“输入范围:”但它只显示InputBox的“输入”。可能这可能是因为我在Mac上?

总之。我能够调用该函数并将其设置为我的其他代码中的新变量。但我正在做类似于设置一个长(一个颜色)的东西,所以我可以在一个范围内选择某种颜色的单元格,但我也在这里得到各种对象错误。我真的不明白。 (而且我认为我正在处理更多问题,因为在Mac上,我没有编辑宏的典型窗口。只是我,基本上是一个文本框和互联网。

因此。这里也是Color的功能和使用这些功能的Sub。 (我编辑了这么多,我不确定我从哪里开始或错误在哪里。)

我正在使用这些函数并将变量设置为等于函数结果。

Sub SelectQuadrantAndPlanets()

Dim quadrant As Range
Dim planetColor As Long

Set quadrant = selectQuadrant()
Set planetColor = selectPlanetColor() '<This is the row that highlights as an error

Call selectAllPlanets(quadrant, planetColor)

End Sub

这是我用来选择我想要在我的范围内突出显示的颜色的功能
我可以选择使用我选择的范围内部颜色,但我不知道如何将内部颜色设置为变量,所以我选择输入框中的1,2或3。

Function selectPlanetColor() As Long

Dim Color As Integer

Color = InputBox("What Color" _
& vbNewLine & "1 = Large Planets" _
& vbNewLine & "2 = Medium Planets" _
& vbNewLine & "3 = Small Planets")

Dim LargePlanet As Long
Dim MediumPLanet As Long
Dim smallPlanet As Long
    LargePlanet = 5475797
    MediumPlanet = 9620956
    smallPlanet = 12893591


If Color = 1 Then
    selectPlanetColor = LargePlanet
Else
    If Color = 2 Then
        selectPlanetColor = MediumPlanet
    Else
        If Color = 3 Then
            selectPlanetColor = smallPlanet
        End If
    End If
End If


End Function

任何帮助都会很棒。我已经能够单独完成各个部分,但现在将它们全部绘制成一个调用它们的子部件对我来说效果不佳。谢谢VBA社区:)

2 个答案:

答案 0 :(得分:0)

它简单得多。刚

Set StartingCell = Cells(R, C)
获得输入后

,然后End Function

Cells方法的神奇之处在于它接受第二个参数 一个数字或一个字符。那就是:

Cells(3, 4)&lt; =&gt; Cells(3, "D")

Cells(1, 28)&lt; =&gt; Cells(3, "AB")

还有一件事,你可以通过一个输入框直接提示用户输入一个范围,如下所示:

Dim myRange as Range
Set myRange = Application.InputBox(Prompt:="Enter a range: ", Type:=8)

Type:=8指定提示的输入是Range。

最后,因为您正处于VBA的学习过程中,所以尽可能避免:

  • 使用SelectActivate内容

  • 使用非限定范围。这指的是方法Cells(..)Range(..)在其前面没有点.的任何地方。这通常会导致一些随机问题,因为它们引用ActiveSheet,这意味着例程的行为将取决于它们运行时活动工作表的内容。避免这种情况,并始终从您定义范围的工作表中明确引用

答案 1 :(得分:0)

继续选择范围的想法使用InputBox选择列和行,使用Application.InputBox并在末尾添加Type以限制选项用户到您想要的类型(Type:= 1&gt;&gt;字符串,Type:= 2&gt;&gt;数字)。

功能 StartingCell 代码

Function StartingCell() As Range

Dim cNum As Integer
Dim R As Integer
Dim C As Variant

C = Application.InputBox(prompt:="Starting Column:", Type:=2) '<-- type 2 inidcates a String
R = Application.InputBox(prompt:="Starting Row:", Type:=1)  '<-- type 1 inidcates a Number

Set StartingCell = Range(Cells(R, C), Cells(R, C))

End Function

Sub TestFunc Code (测试功能)

Sub TestFunc()

Dim StartCell As Range
Dim StartCellAddress As String

Set StartCell = StartingCell '<-- set the Range address to a variable (using the function)
StartCellAddress = StartCell.Address '<-- read the Range address to a String

End Sub