【问题标题】:Excel find rows between two strings and change value in different column for rows between stringsExcel查找两个字符串之间的行并更改字符串之间行的不同列中的值
【发布时间】:2019-12-13 01:15:16
【问题描述】:

不太确定如何查找 [Column B] 中的字符串 [801 C] 和字符串 [Nr] 在 [Column A] 中,然后将 [Column C] 中的值从 [3] 更改为 [27]。我必须在另一张纸上引用这张纸上的数据,以便能够绘制每种颜色标准的 L* a* b* 数据以及标准在光谱仪上的存储方式,标准 ID 号之间存在重叠700 和 800 颜色编号,所以我必须弄清楚如何更改 800 系列颜色的标准 ID 编号的值,以便图表仅包含该数据。标准 ID 号是日志中每一行上唯一可用的东西,它将每一行(行)数据与每种颜色联系起来。还需要能够找到多个实例,这两个字符串可能多次出现在同一张表中。任何帮助、想法和建议将不胜感激。

我确实在 stackoveverflow 的帮助下获得了一个批处理脚本来提取数据,但无法找到它,但是这两个字符串的第一个实例并不是一个理想的解决方案。宏将是理想的,但我对 vb 脚本非常陌生,这超出了我的知识范围。再次感谢任何回复此问题的人,您是圣人。

【问题讨论】:

  • 您显示的内容有两个“Nr”实例,但第二次 B 列字符串不是“801 C”。那里应该做什么?
  • 在程序中将标准更改为不同颜色或系列停止并重新启动时,每个数据块顶部的日志中都会出现“Nr”,它将开始一个新条目与块顶部的 Nr 相同的颜色。我需要的是“801 C”到“706 C”数据开始的“Nr”上方,这让我感到困惑。
  • 范围的.Find 方法允许您指定起始单元格。如果有多个潜在的 801 C 出现,您基本上希望继续循环遍历 .Columns(2) 直到它没有找到任何东西并使用结果的起始行(.Find 返回一个范围对象)来确保它找到正确的编号如果您需要显示的实际实现,我可以稍后完成。
  • 一个实施示例将非常有帮助。谢谢!
  • 需要做几件事给我一个小时。

标签: excel vba


【解决方案1】:

没有对此进行测试,因此可能存在拼写错误,但您应该能够使其正常工作:

Dim valCell As Range 'store the cell where you find "801 C"
Dim i As Long, first As Long, last As Long 'counter, store rows as you iterate

first = 1 'start at the top
Set valCell = Sheet1.Columns(2).Find("801 C", Sheet1.Cells(first, 2)) 'get the first time you have "801 C", if there are none then this will be Nothing and the While loop condition will fail right away

While (Not valCell Is Nothing)

    first = valCells.Row 'set the first row to the row you found "801 C"
    last = Sheet1.Columns(1).Find("Nr", Sheet1.Cells(first, 1)).Row 'set the last row to the row you found "Nr"

    For i = first To last 'loop through the cells and change the values
        Sheet1.Cells(i, 3).Value = 27
    Next

Wend

不要忘记将工作表引用从 Sheet1 更改为您正在使用的工作表的 .CodeName;即编辑器中括号前出现的名称。

【讨论】:

    【解决方案2】:
    Dim valCell As Range 'store the cell where you find "801 C"
    Dim i As Long, first As Long, last As Long 'counter, store rows as you iterate
    
    first = 1 'start at the top
    Set valCell = Worksheets("export1").Columns(2).Find("801 C", Worksheets("export1").Cells(first, 2)) 'get the first time you have "801 C", if there are none then this will be Nothing and the While loop condition will fail right away
    While (Not valCell Is Nothing)
        If valCell.Row > first Then
            first = valCell.Row 'set the first row to the row you found "801 C"
            last = Worksheets("export1").Columns(1).Find("Nr", Worksheets("export1").Cells(first, 1)).Row 'set the last row to the row you found "Nr"
    
            For i = first To last 'loop through the cells and change the values
                Worksheets("export1").Cells(i, 3).Value = 27
            Next
            first = last
            Set valCell = Worksheets("export1").Columns(2).Find("801 C", Worksheets("export1").Cells(first, 2)) 'get the next time you have "801 C", if there are none then this will be Nothing and the While loop condition will fail right away8
        Else
            Set valCell = Nothing
        End If
    Wend
    End Sub
    

    所有内容都按“应该”排序,找到“801 C”和“Nr”的每个实例都会将其留在此处,以防它帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-07
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2017-07-07
      • 2020-05-28
      相关资源
      最近更新 更多