简体   繁体   English

VBA 中的 Object 参考问题

[英]Issue with Object Reference in VBA

I am trying to write some code that cycles through a range of values and return either a 1 or 0. The code will only run if I select the sheet the calculation takes place in, even though I am telling the procedure that the range it is dealing with is in that sheet.我正在尝试编写一些代码,循环遍历一系列值并返回 1 或 0。代码仅在我 select 发生计算的工作表时运行,即使我告诉程序它的范围是处理是在那张纸上。 I want this sheet to be very hidden as other people will use this document and I don't want them messing with the formatting.我希望这张表非常隐藏,因为其他人会使用这个文档,我不希望他们弄乱格式。 I have named the sheet in the VB as 'Binary' as well.我也将 VB 中的工作表命名为“二进制”。 I am still very new to VBA and am trying to learn new things about this language.我对 VBA 仍然很陌生,并且正在尝试学习有关这种语言的新知识。

I have tried several things to fix the issue, but it breaks when I don't explicitly say to select the sheet.我已经尝试了几件事来解决这个问题,但是当我没有明确地对 select 说这张表时,它就会中断。 I've commented below in the code where it is the code breaks and I can't find a solution.我在下面的代码中评论了代码中断,我找不到解决方案。 Printing my debug statements give me the correct values, and the entire project runs correctly as long as I explicitly tell the procedure to select the sheet.打印我的调试语句会给我正确的值,只要我明确地将程序告诉 select 工作表,整个项目就会正确运行。 I would prefer if there is a solution that does not require me to tell the code to hide/unhide sheets, and have the sheet remain veryhidden.如果有一个解决方案不需要我告诉代码隐藏/取消隐藏工作表,并且让工作表保持非常隐藏,我会更喜欢。

Sub Binary_Check()

    Dim binaryWS As Worksheet
    Dim summaryLastRow As Long
    Dim summaryLastColumn As Long
    Dim BinaryRng As Range
    
    'binaryWS.Visible = xlSheetVisible
   
    Set binaryWS = Binary

    'Taking away this next line will break where I set BinaryRng
    Binary.Select
    
    summaryLastRow = binaryWS.Range("A" & Rows.Count).End(xlUp).Row
    summaryLastColumn = binaryWS.Cells(1, Columns.Count).End(xlToLeft).Column
    'Debug.Print summaryLastColumn
    'Debug.Print summaryLastRow
    
    'This is what breaks and gives me an error saying Method Range of object _worksheet failed
    Set BinaryRng = binaryWS.Range("B2", Cells(summaryLastRow, summaryLastColumn))
    
    'Debug.Print BinaryRng.Address
        
    For Each cell In BinaryRng
        If InStr(cell, "(") > 0 Then
            cell.Value = 1
        Else
            cell.Value = 0
        End If
    Next cell
    
    'binaryWS.Visible = xlSheetHidden
       
End Sub

If you use the.Cells, .Rows, .Columns method, you have to add the Worksheet reference.如果使用 .Cells、.Rows、.Columns 方法,则必须添加 Worksheet 引用。 Otherwise it will assign an Active Worksheet to that method.否则,它将为该方法分配一个活动工作表。

Set BinaryRng = binaryWS.Range("B2", binaryWS.Cells(summaryLastRow, summaryLastColumn))

Usually I would use the With...End With statement:通常我会使用With...End With语句:

With binaryWS 
    summaryLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    summaryLastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    'Debug.Print summaryLastColumn
    'Debug.Print summaryLastRow

    'This is what breaks and gives me an error saying Method Range of object _worksheet failed
    Set BinaryRng = .Range("B2", .Cells(summaryLastRow, summaryLastColumn))
End With

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

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