Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 创建用户输入以使用一组标准操作数据_Vba_Excel - Fatal编程技术网

Vba 创建用户输入以使用一组标准操作数据

Vba 创建用户输入以使用一组标准操作数据,vba,excel,Vba,Excel,我在VBA Excel 2010程序代码方面遇到问题 我正在尝试从excel 2010上的电子表格中读取数据。我拥有的是一组数据(见下文),我正试图编写一个代码,让我使用消息框,让我从列中的名称列表中写下我要查找的名称,例如“名称1”,然后我想设置一个条件,如果列中的数字等于零,则另一列的数字再次等于0(“名称5”),然后突出显示“名称8和名称9”列中大于30的任何数字(只是一个随机示例)。重要的是,只有当行“Name 1”和“Name 5”等于零时,列“Name 8/9”的红色突出显示才会出现

我在VBA Excel 2010程序代码方面遇到问题

我正在尝试从excel 2010上的电子表格中读取数据。我拥有的是一组数据(见下文),我正试图编写一个代码,让我使用消息框,让我从列中的名称列表中写下我要查找的名称,例如“名称1”,然后我想设置一个条件,如果列中的数字等于零,则另一列的数字再次等于0(“名称5”),然后突出显示“名称8和名称9”列中大于30的任何数字(只是一个随机示例)。重要的是,只有当行“Name 1”和“Name 5”等于零时,列“Name 8/9”的红色突出显示才会出现

我已经这样做了,但我只使用了列号,例如A1:A5。现在我需要使用列的名称,因为我想为不同的excel电子表格使用代码,但是每个excel表格的列名称位于不同的位置,但是如果我使用名称,无论它们是excel中的哪一列,我都会找到我要找的正确列并设置标准

“名称1/5”的条件始终为=0或=1,但程序在搜索时必须要求我选择该条件

如果您查看下面的示例,您可以看到当名称1和名称5满足=0的条件且名称8/9中的数字大于30时,红色突出显示。当它不大于30且仍符合标准时,在excel电子表格示例中,它将以蓝色突出显示。必须忽略所有其他名称

见下面的例子

Name 1  Name 2  Name 3  Name 4  Name 5  Name 6  Name 7  Name 8  Name 9  Name 10
0       0       1       0       0       1       58      35      14      19
0       0       0       0       0       1       41      45      68      74
1       0       1       0       1       0       23      18      98      87
0       0       1       0       0       1       65      36      52      89
0       0       0       0       1       1       24      95      47      75
1       1       1       0       1       0       58      87      59      14
0       1       0       0       0       0       74      41      84      32
1       1       0       0       1       0       96      25      74      96
0       0       0       0       0       0       87      35      15      53
0       0       1       0       0       1       57      49      48      47
1       0       1       0       1       1       63      84      23      65
0       1       0       0       0       0       21      54      69      12
0       0       1       0       0       0       54      23      54      54
1       1       0       0       1       1       88      34      77      88
0       0       1       0       0       0       78      48      68      69
1       0       1       0       0       1       96      87      14      65
1       0       0       0       1       0       21      96      54      25
0       1       0       0       0       0       54      72      78      29
0       1       1       0       0       1       62      38      22      78
0       0       0       0       0       0       21      49      65      54
1       0       1       0       1       1       17      65      98      99
0       0       0       0       0       0       59      15      56      70
0       1       1       0       0       0       36      12      29      54
1       0       0       0       1       0       29      49      55      54
代码:


如果我读对了你想要的,你可以试试这个。这将要求您输入名称,然后在该特定列上进行运动,作为循环的范围。这就是你想要的吗

还有,我变了

If Application.Sum(rw.Resize(, 4)) = 0 Then 
        cll.Interior.ColorIndex = 3 
到rw.Interior.Colorindex=3-我猜这是一个错误(因为不能在其循环之外使用变量)

Private Sub CommandButton21_Click()

searchstring = InputBox("Input name?")

Set coll = Rows(1).Find(What:=searchstring, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

If coll Is Nothing Then
    MsgBox "Name not found"
    Exit Sub
Else
    coll = coll.Column
    Lrow = Range(Cells(2, coll), Cells(2, coll)).CurrentRegion.Rows.Count
End If

    Cells.Interior.ColorIndex = 0
    For Each rw In Range(Cells(2, coll), Cells(Lrow, coll))

        If Application.Sum(rw.Resize(, 4)) = 0 Then
            rw.Interior.ColorIndex = 3
            For Each cll In rw.offset(, 4).Resize(, 18).Cells
                If cll.Value > 50 Then cll.Interior.ColorIndex = 3
            Next cll
        End If
    Next rw
End Sub

这就是我到目前为止所知道的。数字和单元格有点不同。这段代码使用了单元格位置而不是列名。这很好,但我希望能够搜索三个不同的名称并设置条件(一个消息框,询问我希望它是1还是0).因此,搜索名称1向我询问条件,1或0,然后名称2,然后名称3。记住,为什么不看看创建自定义表单?您可以有3个文本框和3个切换框来选择您的条件。如果有很多东西一个接一个地出现,这不是很好的编码实践-当一件事情发生时,我不知道如何才能做到这一点,很抱歉,这是一个非常新的问题。首先,请按Alt+F11进入VBA编辑器,然后右键单击项目并添加用户表单。联机提供了大量教程,帮助您走上正确的道路。
Private Sub CommandButton21_Click()

searchstring = InputBox("Input name?")

Set coll = Rows(1).Find(What:=searchstring, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

If coll Is Nothing Then
    MsgBox "Name not found"
    Exit Sub
Else
    coll = coll.Column
    Lrow = Range(Cells(2, coll), Cells(2, coll)).CurrentRegion.Rows.Count
End If

    Cells.Interior.ColorIndex = 0
    For Each rw In Range(Cells(2, coll), Cells(Lrow, coll))

        If Application.Sum(rw.Resize(, 4)) = 0 Then
            rw.Interior.ColorIndex = 3
            For Each cll In rw.offset(, 4).Resize(, 18).Cells
                If cll.Value > 50 Then cll.Interior.ColorIndex = 3
            Next cll
        End If
    Next rw
End Sub