简体   繁体   English

循环并比较数组中的值

[英]Loop and compare values from array

In Sheet1 I have a single column with populated cells from 1-20 row with regular numbers. 在Sheet1中,我有一列包含1-20行中具有常规编号的填充单元格。 Look at picture bellow: 看下面的图片:

在此处输入图片说明

In Sheet2 I have also a single column, cells are starting from 5-25. 在Sheet2中,我还有一列,单元格从5-25开始。 If I enter some values into these cells, in Sheet1 from cell with that same value to column "D" background color is changed. 如果我在这些单元格中输入了一些值,则在Sheet1中,将具有相同值的单元格更改为“ D”列的背景色。 Look at pictures bellow to see how it works: 看下面的图片,看看它是如何工作的:

在此处输入图片说明在此处输入图片说明

I'm doing it with this piece of code: 我正在用这段代码来做到这一点:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 1 And Target.Row >= 5 Then
    If Target.Offset(0, 0).Text <> "" Then
    Dim n As Long
        For n = 5 To 25
            If Target.Offset(0, 0).Value = Worksheets("Sheet1").Range("A" & n).Value Then
                Worksheets("Sheet1").Range("A" & n & ":D" & n).Interior.ColorIndex = 3
            End If
        Next n
    End If
End If

End Sub

Now, I'd like to make some kind of check every time Sheet1 is activated, in a way if there are no values in cells Interior.ColorIndex = xlNone for all the cells in Range("A1:A20"), if there is a value in some cells Interior.ColorIndex = 3 for those cells. 现在,我想每次激活Sheet1时都要进行某种检查,以某种方式检查是否在Range(“ A1:A20”)中所有单元格的Interior.ColorIndex = xlNone都没有值。这些单元格在某些单元格中的值Interior.ColorIndex = 3 I was thinking about to put these values into an array and then loop through it to compare values but I'm new to VBA so help would be welcome. 我当时正在考虑将这些值放入一个数组中,然后循环遍历以比较值,但是我对VBA还是陌生的,因此欢迎您提供帮助。 If there is a better solution, just bring it on. 如果有更好的解决方案,那就加油吧。

Also, I'd like to make a piece of code for a situation if I replace value 12 with 17 that Interior.ColorIndex of cell that contains 12 goes to xlNone and of 17 Interior.ColorIndex goes to " 3 ". 另外,我想使一段代码的情况下,如果我有17个替换值12 Interior.ColorIndex细胞含有12去xlNone和17 Interior.ColorIndex变为“ 3 ”。

So, every suggestion is welcome. 因此,欢迎提出任何建议。

No VBA is needed, as pnuts says. 正如Pnuts所说,不需要VBA。

The easiest way to do this with the colours is with Conditonal Formatting using Formula to check the value of the cells in column A on one sheet against the values in the rows where you want the colour to change and apply the formatting. 最简单的颜色处理方法是使用“ 公式”进行“ 条件格式化 ”,将一张纸上A列中的单元格的值与要更改颜色并应用格式的行中的值进行比较。

See: 看到:

BUT if you want VBA, then you can do this in the workbook and worksheet event handlers. 但是,如果要使用VBA,则可以在工作簿和工作表事件处理程序中执行此操作。

Worksheet Selection/Activation: 工作表选择/激活:

in Excel on Sheet1, right-click on the sheetname at the bottom, and click View Code . 在Excel在Sheet1上,右键单击底部的工作表名称,然后单击查看代码 This will open the class module for Sheet1 in the VB Editor. 这将在VB编辑器中打开Sheet1的类模块。

At the top of the code module on the left, select Worksheet from the drop-down, and on the right drop-down, click the Activate event-handler. 在左侧代码模块的顶部,从下拉列表中选择“ 工作表 ”,然后在右侧下拉列表中单击“ 激活”事件处理程序。

This will create an empty sub-routine that will be executed by Excel everytime that you select the Sheet1 worksheet. 这将创建一个空的子例程,该例程将在您每次选择Sheet1工作表时由Excel执行。

In that code you can make your checks. 在该代码中,您可以进行检查。

Cell changes on sheet2: sheet2上的单元格更改:

To get code to run everytime you make a cell change on sheet2, you need to open the class module for sheet2, select Worksheet from the drop-down on the left, and Change for the event. 要使代码每次在Sheet2上进行单元更改时都运行,您需要打开sheet2的类模块,从左侧的下拉菜单中选择“ 工作表 ”,然后为事件进行更改

This code will run everytime you change a cell on sheet2 and in here, you would write code that first checks if the Target argument is in your range like this: 每当您更改sheet2上的单元格时,该代码就会运行,在这里,您将编写代码来首先检查Target参数是否在您的范围内,如下所示:

If not Application.Intersect(Target, "A5:25") Then Exit Sub

Next you want to write your code to check if the value is no longer matching, and reset the colours. 接下来,您要编写代码以检查该值是否不再匹配,然后重置颜色。

HTH HTH

Philip 菲利普

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

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