简体   繁体   English

使用Interior.Color Excel 2010 VBA ActiveX控件进行While迭代时出现“ 1004”错误

[英]'1004' error on While iteration with Interior.Color Excel 2010 VBA ActiveX Controls

Thanks in advance for any advice. 在此先感谢您的任何建议。

I've got an Active X Check Box with the following code: 我有一个带有以下代码的Active X复选框:

Private Sub CheckBox1_Click()

Dim h As Long, r As Long
h = 1400
r = 1429
If Sheets("SheetName").OLEObjects("CheckBox1").Object.value = True And Sheets("SheetName").CommandButton2.BackColor = RGB(192, 255, 192) Then
    While h <= r
        If Range("O1329") = "Width MM" Then
            If Cells(h, "N").value - Cells(h, "AA").value > Range("Y1317") Then
                Cells(h, "N").Formula = "=((INT(AA" & h & "))-1)+0.25"
                Cells(h, "AA").Interior.Color = RGB(250, 250, 200)
            End If
        Else
            If Cells(h, "N").value - Cells(h, "W").value > Range("Y1317") Then
                Cells(h, "N").Formula = "=((INT(W" & h & "))-1)+0.25"
                Cells(h, "W").Interior.Color = RGB(250, 250, 200)
            End If
        End If
    h = h + 1
    Wend
End If

End Sub

And it's hanging up in a few different places. 它挂在几个不同的地方。 (see EDIT notes at bottom) (请参阅底部的编辑说明)

Here's what I'm trying to do: 这是我想做的事情:

The range 1400-1429 spans several columns which contain relative data, in this case height measurements, so the while statement is for iterating through those cells. 1400-1429范围跨越几列,其中包含相对数据,在这种情况下为高度测量,因此while语句用于遍历这些单元格。 The data is line-specific, with values in columns AA and W coming from user input, and with a formula potentially adjusting those values in column N as a ' height standard '. 数据是特定于行的,AA和W列中的值来自用户输入,并且公式可能将N列中的值调整为“ 高度标准 ”。

The first If statement is meant to check that both the checkbox is checked, AND that a neighboring toggle button is depressed (by checking the toggled color state. The button works very well, and changes color easily). 第一个If语句旨在检查两个复选框是否都被选中,以及相邻的切换按钮是否被按下(通过检查切换的颜色状态。该按钮工作得很好,并且易于更改颜色)。

The If / Else statement is supposed to check to see if we're using Metric or inches, and should choose to pull values from the appropriate column ( W when we're using inches, and AA for metric). If / Else语句应该检查一下我们使用的是公制还是英寸,并且应该选择从适当的列中提取值(当我们使用英寸时为W ,而公制为AA )。 Cell O1329 populates with "MM" when we're using Metric. 当我们使用公制时,单元格O1329会填充“ MM”。 ( NOTE : all factoring on this project takes place in Inches. This particular Sub doesn't execute any of the MM-IN conversion) 注意 :此项目的所有分解均以英寸为单位。此特定的Sub不执行任何MM-IN转换)

The third set of If statements is meant to use the difference between individual cell values in certain columns in the range ( N , minus either W or AA ), and compare that difference to another cell ( Y1317 ). 第三组If语句旨在使用范围( N ,减去WAA )中某些列中各个单元格值之间的差异,并将该差异与另一个单元格进行比较( Y1317 )。 It proceeds only if the value in Y1317 is less than the difference of the values. 仅当Y1317中的值小于这些值的差时,才继续进行。 The value in Y1317 represents an acceptable ' tolerance ' in the height measurements. Y1317中的值表示高度测量中可接受的“ 公差 ”。

Once the code has decided which cell value to adjust, I want a formula to insert itself into the appropriate cell in the N column, and color the cell yellow that it refers to (in the AA or W user input columns as a flag, depending on unit of measurement). 一旦代码确定了要调整的单元格值,我希望一个公式将其自身插入N列中的相应单元格,并将其所引用的单元格涂成黄色(在AA或W用户输入列中作为标志,具体取决于的计量单位)。

The formula is meant to round the appropriate value down to the whole integer, subtract one (1), then add a quarter (0.25). 该公式用于将适当的值四舍五入为整数,然后减去一(1),然后再加上四分之一(0.25)。

So, to recap, this code is meant to apply a standard to height measurements based on a certain tolerance: If the checkbox is checked, AND if the button is depressed, AND if the difference is greater than the allowed tolerance, determine which column to pull a value from (based on unit of measurement) and adjust that value (starting at row 1400) according to the formula, and flag the vertical-corresponding cell in the column where values are pulled from with a light yellow color. 因此,总而言之,该代码旨在基于一定的公差将标准应用于高度测量:如果选中了此复选框,并且如果按下了按钮,并且如果差异大于允许的公差,则确定要从中提取一个值(基于度量单位),然后根据公式调整该值(从行1400开始),并在其中从中提取值的列中以浅黄色标记垂直对应的单元格。 Do all of that 30 times in a row. 连续执行所有30次操作。 (phew!) (phe!)

Thanks again to the community for helping here, I'm glad to see that this is generating several responses that will build cumulatively to an answer. 再次感谢社区在这里提供的帮助,我很高兴看到这正在产生一些答案,这些答案将逐渐积累为一个答案。 Feel free to notice that I'm new (to both SO, VBA, and coding in general) and don't hesitate to point me in 'better' directions. 随时注意我是新手(无论是SO,VBA还是一般的编码),都不要犹豫将我指向“更好”的方向。 I'm trying to become a decent programmer. 我试图成为一个体面的程序员。

EDIT: 编辑:

  1. I accidentally left the wrong cell reference on 'line 14'. 我不小心在“第14行”上留下了错误的单元格引用。 Changed Cells(h, "AA").value to Cells(h, "W").value . Cells(h, "AA").value更改为Cells(h, "W").value
  2. Thanks to Portland Runner for not assuming any previous experience. 感谢Portland Runner不承担任何以前的经验。 I don't know how I did w/out the f8 step 'till now. 直到现在,我都不知道如何做f8步。
  3. My very first-ever f8 step-through showed me that validating the checkbox with True does indeed work, where xlOn did not. 我的第一个f8逐步入门告诉我,使用True验证复选框确实可以,而xlOn则不能。 Researching the difference... 研究差异...
  4. Stepping through further gives me a 1004 error (Application-Defined or Object-defined), on lines '11' and '16' (Interior.Color) which I take to mean that VBA doesn't like the way I'm referencing the range to color a particular cell (maybe?) 在“ 11”和“ 16”行(Interior.Color)上,逐步执行操作会给我1004错误(应用程序定义的或对象定义的),这意味着VBA不喜欢我引用的方式范围为特定单元格着色(也许吗?)
  5. Right now I'm trying to fix the 1004 error, and researching Tim Williams' comment. 现在,我正在尝试修复1004错误,并研究蒂姆·威廉姆斯的评论。 The Object.Object is something I saw here , and as one of the posters on that forum noted, the more succint Sheets("SheetName").OLEObjects("CheckBox1").Object.value worked for them, so I'll try that. 我在这里看到 Object.Object ,正如该论坛上的一位张贴者所指出的那样,更Sheets("SheetName").OLEObjects("CheckBox1").Object.value为它们工作,所以我将尝试那。 Qualifying with a sheet reference also seems like something fairly important to know how to do... 确认工作表引用似乎也很重要,知道如何做...

The left part of the main If will return always false, try to replace it by: 如果“ if”将始终返回false,请尝试将其替换为:

If Sheets("SheetName").CheckBox1 AND ...

Or keep it as it is and remove the = xlOn (this is the integer value 1, values for a CheckBox object are TRUE, FALSE) 或者保持原样并删除= xlOn(这是1的整数,CheckBox对象的值为TRUE,FALSE)

If Sheets("SheetName").Shapes("CheckBox1").OLEFormat.Object.Object.value AND ....

Showing how to qualify Range and Cells with a sheet reference (otherwise they will default to the active sheet): 显示如何使用工作表引用限定“范围”和“单元格”(否则它们将默认为活动工作表):

Private Sub CheckBox1_Click()

Dim h As Long, r As Long, rw as Long
Dim sht as WorkSheet
h = 1400
r = 1429

Set sht = Sheets("SheetName")

If sht.OLEObjects("CheckBox1").Object.value = True And _
   sht.CommandButton2.BackColor = RGB(192, 255, 192) Then

   'For.. loop is a better fit here...
   For rw = h to r 
        If sht.Range("O1329") = "Width MM" Then
            If sht.Cells(rw, "N").value - sht.Cells(rw, "AA").value > sht.Range("Y1317") Then
                sht.Cells(rw, "N").Formula = "=((INT(AA" & rw & "))-1)+0.25"
                sht.Cells(rw, "AA").Interior.Color = RGB(250, 250, 200)
            End If
        Else
            If sht.Cells(rw, "N").value - sht.Cells(rw, "W").value > sht.Range("Y1317") Then
                sht.Cells(rw, "N").Formula = "=((INT(W" & rw & "))-1)+0.25"
                sht.Cells(rw, "W").Interior.Color = RGB(250, 250, 200)
            End If
        End If
    Next rw
End If

End Sub

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

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