简体   繁体   English

Excel VBA范围返回空

[英]Excel VBA Range returns empty

I'm trying to do what I thought was going to be simple is turning out to be more difficult. 我正在尝试做我认为会变得简单的事情,结果却变得更加困难。 Basically what I am trying to do is populate a material column based upon the selection change of the cell next to it. 基本上我想做的是根据旁边的单元格的选择更改填充材料列。 The material column is a drop down menu which gets it's value from a LookUpRange. 材质列是一个下拉菜单,可从LookUpRange获取其值。

Here is Part 1 这是第1部分

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim MaterialCellAddress As String
Dim MaterialCell As Range
Dim LookUpRangeName As String
Dim LookUpRange As Range

Select Case Target.Column
    Case 2
        ThisRow = Target.Row
        MsgBox ("Changing the width")
    Case 3
        MsgBox ("Changing the Height")
    Case 11

        MaterialCellAddress = "L" & Target.Row
        Set MaterialCell = Range(MaterialCellAddress)

        LookUpRangeName = "LookUpRange_" & Target.Value & "Materials"
        Set LookUpRange = Range(LookUpRangeName)

        Call LM.InitMaterialDropDownList(MaterialCell, LookUpRange)
    Case Else
        MsgBox ("Something else is going on")
 End Select
End Sub

And here is Part II inside the my LM Class 这是我的LM课程的第二部分

Public Sub InitMaterialDropDownList(ByVal MaterialCell As Range, ByVal LookUpRange As Range)
With Sheets("Entry Form").Range(MaterialCell).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=LookUpRange"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "Select Edge Type"
.ErrorTitle = "Invalid Edge Type"
.InputMessage = "Select Edge Type"
.ErrorMessage = "You must select a valid edge type from the drop down list"
.ShowInput = True
.ShowError = True
End With
End Sub

A couple things that I am noticing. 我注意到了几件事。 When the code hits Set MaterialCell = Range(MaterialCellAddress) or this Set LookUpRange = Range(LookUpRangeName) both of these return empty for some reason. 当代码到达Set MaterialCell = Range(MaterialCellAddress)或此Set LookUpRange = Range(LookUpRangeName) ,由于某种原因,这两个都返回空。

And of course when I hit this... Call LM.InitMaterialDropDownList(MaterialCell, LookUpRange) it throws an object required error. 当然,当我遇到这个问题时... Call LM.InitMaterialDropDownList(MaterialCell, LookUpRange)它将引发一个对象必需的错误。

I'm not a vba veteran so I'm sure this is something stupid simple. 我不是vba的资深人士,所以我敢肯定这很简单。 So I could use some help. 因此,我可以使用一些帮助。

LM is a Class, Classes dont have subs or functions, instances of a Class have the functions/subs you define. LM是一个类,类没有子类或函数,类的实例具有您定义的函数/子类。 so 所以

Call LM.InitMaterialDropDownList(MaterialCell, LookUpRange)

should be 应该

Call (new LM).InitMaterialDropDownList(MaterialCell, LookUpRange)

or shorter (Call is not neccessary) 或更短(不必要致电)

(new LM).InitMaterialDropDownList MaterialCell, LookUpRange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM