在Excel表格中查找数据,在列H中添加yes



下面的代码搜索名为"Data"的表。

如果

你的消息不是很好格式化,所以很难理解你的代码结构是如何....您应该使用code函数使其更清晰。

对我来说也很像一个家庭作业问题,所以我不会为你编码(我觉得关于so的问题应该提供自己的代码....)你所提供的只是伪代码)。

我将如何构建我的代码:

声明一个布尔值,比如:

data_exist: True

然后循环遍历细胞。如果任何一个条件不匹配,我将把我的布尔值设置为"False"。

可以使用for循环从代码中的第一个单元格循环到最后一个单元格。在for循环的开始,插入一个if来检查您的bolean == True(因为如果它等于false,则没有进一步迭代的意义)。

然后你可以有一个IF来检查布尔值== True,如果是,那么运行你的msgbox &一切。

=================== 编辑AFTER-FORMAT ================

谢谢你,先生,这样就清楚多了。根据你提供的信息,我会这么做我将添加另一个表"check_data"。在这里,我将输入我想要检查的每一行中必须存在的所有值。这样做的好处是,你可以很容易地编辑你检查的值(即它们不是硬编码的),然后另一个用户可以编辑它,甚至不知道VBA正在被用于此。比在代码中硬编码答案要好得多。我决定把数据放在新表的第2行,"check_data"

假设我有一个名为"check_data"的表单。让我们假设我将要检查的数据(您的Txtscan.txt、Source.txt等)放在这样一种方式中,即它们的列号与您要检查数据的表处于完全相同的位置。这样,您就可以使用相同的变量来遍历这两个变量并简化代码....

    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,所以我不能真正测试代码,它来自记忆,正如你所知道的(或者很快就会知道,如果你在VBA中编写了很多代码)这个东西充满了令人沮丧的小怪癖& &;黑客,但除了一些小细节应该可以工作。我认为这是一个更灵活的结构,比你开始的....基本上,这个想法是尽可能少地硬编码,使用函数来计算/查找值。此外,由于上述原因,从长远来看,将检查的答案添加到另一张纸上会更好…

最新更新