在Excel工作表中查找数据,如果存在数据,则向列H添加“是”

嘿,伙计们,我需要你的帮助。 我目前有以下代码,通过名为“数据”的工作表进行search。 如果符合所有条件的行存在,我想在列“H”中添加“是”,并提供一个消息框,指出“数据组合存在”,如果没有提供“数据不存在”的消息。 提前致谢。

Private Sub Cmdnext_Click() 'With Sheets("data") 'we initialize the row number where we will start comparing the data because the worksheet has headers in row 1 'Sheet = Data Row = 2 'Now we start the looping process Do While Sheets("DATA").Cells(Row, 1).Value <> ”” 'We compare the entered data with the worksheet data If Sheets("DATA").Cells(Row, 1).Value = Txtscan.Text And Sheets("DATA").Cells(Row, 2).Value = TxtsourceID.Text And Sheets("Data").Cells(Row, 3).Value = txtcardbin.Text And Sheets("DATA").Cells(Row, 6).Value = txtcardstock.Text Then MsgBox ("Data combination exists") End Else 'we go to the next row to compare data Row = Row + 1 End If Loop 'If the user is not found based on his inputs in the login form, we give out a message MsgBox ("Data Does not Exist") 'End With End Sub 

您的邮件格式不是很好,所以很难理解您的代码的结构。您应该使用codefunction来使其更清晰。

此外,这看起来像是一个功课题,对我来说,所以我不会为你编写代码(我觉得在SO上的一个问题应该提供自己的代码….你真正提供的是伪代码)。

我将如何构build我的代码:

我会声明一个布尔值,说:

 data_exist: True 

然后,我会循环通过我的细胞。 如果任何标准不匹配,我会然后将我的布尔值设置为“False”。

你可以使用for循环,从第一个单元循环到最后一个代码。 在for循环的开始,插入一个if来检查你的布尔值是否为True(因为如果它等于false,那么没有进一步迭代的点)。

然后,你可以有一个IF检查布尔值== True,如果它确实然后运行你的msgbox&一切。

===================编辑后格式================

谢谢先生,这样更清楚些。 基于你在这里提供了什么,我会做什么,我会添加另一个工作表,“check_data”。 在那里,我将input所有必须出现在每一行我想检查的值。 好处是,你可以很容易地编辑你的支票(即他们没有硬编码)的价值,和另一个用户可以编辑它,甚至不知道VBA是用于这一点。 比在你的代码中硬编码的答案好得多。 我决定把数据放在我的新表第2行,“check_data”

那么让我们假设我有这张表,“check_data”。 我们假设我把数据检查(你的Txtscan.txt,Source.txt等),使得它们的列号与你正在检查数据的表格完全一致。 这样你可以通过同一个variables来遍历这两个variables,并简单地处理你的代码。

  Private Sub Cmdnext_Click() dim data_exist As Bolean, dim row as integer, dim columns As integer, dim max_row As integer, dim i as integer 'You should declare variable here row = 2, columns = 1 max_row = ???????? 'Not sure what your situation for max_row, but you can figure out a way to determine 'that I am sure. If fixed number it's easy, otherwise there are command to find last used row of a worksheet. for i=row to max_row: 'This will iterate over all your rows columns = 2, data_exist = True 'Each time you are about to check a new row, you must reset the column number so it increments from 1 to 6 and start by assuming that the data will match While data_exist == True And columns < 7 'Check each column in current row ideally again I wouldn't hard-code it but you can just adapt that later If Sheets("Data").Cells(row, columns) != Sheets("check_data").Cells(2, columns) Then data_exist = False Else columns = columns + 1 End If End While If data_exist == True Then 'Put your msgbox or whatever code you want to run here in case data are identical Else 'Put any code you want to run if the data aren't identical for that row End if next i End sub 

一般来说应该是这样的。 我实际上没有安装MS Excel,所以我不能真正testing代码,它的内存和你所知道的(或者如果你在VBA中编写代码很快就会知道),这件事情是充满了令人沮丧的怪癖和黑客,但除了应该工作的小细节。 我认为它是一个更灵活的结构,你开始…基本上是想尽可能硬编码,使用函数来计算/find一个值。 由于上面详细说明的原因,从长远来看,在单独的工作表上添加答案将会更好。