我正绞尽脑汁想弄清楚这件事。我有3个领域的一个样本。
我可以让宏显示一组样本的结果(3)。但是我如何让它循环下面的3个样本,以此类推?

Sub Data()
If Range("H33").Value <= 40 And Range("H34").Value > 42 And Range("H35").Value > 42 Then
Range("J33").Value = "Present"
Range("K33").Value = "SARS-CoV-2 DETECTED"
Else:
Range("K33").Value = "SARS-CoV-2 not detected"
Range("J33").Value = "Absent"
End If
End Sub发布于 2021-02-22 15:56:21
你可以这样做,让它变得简单:
Sub Data()
Dim cell As Range, i As Long
For Each cell In Range("G1:G20") 'Your column of the sample names
If cell.Value = "Sample" Then 'If equals sample. Otherwise you could do: If cell.Value <> "" Then
i = cell.Row
If Range("H" & i).Value <= 40 And Range("H" & i + 1).Value > 42 And Range("H" & i + 2).Value > 42 Then
Range("J" & i).Value = "Present"
Range("K" & i).Value = "SARS-CoV-2 DETECTED"
Else
Range("K" & i).Value = "SARS-CoV-2 not detected"
Range("J" & i).Value = "Absent"
End If
End If
Next
End Sub发布于 2021-02-22 16:32:43
请尝试下一个代码。它使用数组,只在内存中工作,在需要处理的范围很大的情况下会非常快。它独立于字符串'Sample‘工作,这个字符串可能是其他东西(也是变量):
Sub DataCovidResults()
Dim sh As Worksheet, lastR As Long, arrH, arrFin, i As Long
Set sh = ActiveSheet 'use here the sheet you need
lastR = sh.Range("H" & sh.Rows.count).End(xlUp).row
arrH = sh.Range("H2:H" & lastR).value 'put the H:H range in an array
ReDim arrFin(1 To UBound(arrH), 1 To 2) 'redim the final array (to collect the result)
For i = 1 To UBound(arrH) Step 3
If arrH(i, 1) <= 40 And arrH(i + 1, 1) > 42 And arrH(i + 1, 1) > 42 Then
arrFin(i, 1) = "Present": arrFin(i, 2) = "SARS-CoV-2 DETECTED"
Else
arrFin(i, 1) = "Absent": arrFin(i, 2) = "SARS-CoV-2 not detected"
End If
Next i
'drop the processing result array at once:
sh.Range("I2").Resize(UBound(arrFin), UBound(arrFin, 2)).value = arrFin
End Sub发布于 2021-02-23 16:23:47
就我个人而言,我认为这将是一个更好的公式。
单元格J33中
=IF(G33="Sample",IF(AND(H33<=40,H34>42,H35>42),"Present","Absent"),"")在单元格K33中
=IF(G33="Sample",IF(AND(H33<=40,H34>42,H35>42),"SARS-CoV-2 DETECTED","SARS-CoV-2 not detected"),"")您可以将其复制下来,它将只在具有Sample的那些行上显示值。如果你愿意,你可以把那部分去掉。
作为一个在真正进入excel可用的功能之前基本学会了VBA的人,我发现自己经常尝试用VBA创建解决方案,这些解决方案使用excel函数要简单得多,至少要快得多。因此,如果你还没有这样做,我建议你做一些研究,看看excel函数能为你做些什么。如果你经常使用excel,你会惊讶于你可以在没有VBA的情况下做的事情。
https://stackoverflow.com/questions/66311793
复制相似问题