简体   繁体   English

带For循环的Workbook_Open中的对象必需错误

[英]Object required error in Workbook_Open with For loop

***It's working!! ***正在工作! Thanks everyone for the help :D - turns out range was declared and set as "columnnrange" and the for loop was using "columnrange". 谢谢大家的帮助:D-结果是声明了range并将其设置为“ columnnrange”,并且for循环正在使用“ columnrange”。 It's the little things ;) . 这是小事;)。 Appreciate the insight and guidance all! 感谢所有的有识之士和指导!

*Thanks for the help everyone! *感谢大家的帮助! I've updated the code per the responses. 我已经根据响应更新了代码。 The error definitely comes when the For loop runs - if it is commented out there is no error. 错误肯定在For循环运行时出现-如果已注释掉,则没有错误。 I believe the error is with the range "columnrange" object. 我相信错误是与范围“ columnrange”对象有关。 Thanks!! 谢谢!!

I have a simple Workbook_Open() routine that opens a UserForm "SelectData" and sets a ComboBox "DisplayData" value. 我有一个简单的Workbook_Open()例程,该例程打开UserForm“ SelectData”并设置ComboBox“ DisplayData”值。 I have a MsgBox that confirms the value set in the ComboBox "DisplayData". 我有一个MsgBox ,用于确认在ComboBox“ DisplayData”中设置的值。
Once set, the UserForm "SelectData" is hidden. 设置后,将隐藏用户窗体“ SelectData”。 Then, there is a "for" loop to hide all columns where the given cell in range "columnrange" is not equal to the ComboBox value. 然后,存在一个"for"循环来隐藏“ columnrange”范围内给定单元格不等于ComboBox值的所有列。 I'm getting an error "object required" but for the life of me cannot figure out where I'm going wrong. 我遇到了“需要对象”错误,但终生无法弄清楚我要去哪里。 The goal of this spreadsheet is: "on open", allow the user to filter the visible columns on an excel doc exported from a SharePoint list. 该电子表格的目标是:“打开”,允许用户筛选从SharePoint列表导出的excel文档上的可见列。
Thanks in advance!! 提前致谢!!

Private Sub Workbook_Open()

Dim columnrange As Range
Dim cell As Range

Set columnnrange = ActiveWorkbook.Worksheets("owssvr").Range("G1:Z1")

SelectData.Show

MsgBox (SelectData.DisplayData.Value)

For Each cell In columnrange
    If SelectData.DisplayData.Value <> cell Then
        cell.EntireColumn.Hidden = True
   Else
        cell.EntireColumn.Hidden = False
   End If
Next cell


End Sub

You will need to be sure the SelectData form is open. 您将需要确保SelectData表单已打开。 Do you have the code behind for the OK or Close button? 您是否有“确定”或“关闭”按钮的代码? If you use something like Me.Close then you won't have an object. 如果您使用类似Me.Close东西,那么您将没有任何对象。 You can, instead, use Me.Hide . 您可以改为使用Me.Hide

Change the loop condition to verify cell instead of columnrange . 更改循环条件以验证单元格而不是columnrange Here is a simplified code that should work. 这是应该工作的简化代码。

For Each cell In columnrange    
    If SelectData.DisplayData.Value <> cell Then
        cell.EntireColumn.Hidden = True
    Else
        cell.EntireColumn.Hidden = False
    End If    
Next cell

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

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