VBA循环来自另一张图纸
我的循环无法在整个工作表1中运行,这使我遇到了问题。如果第1页“测试”中的值存在于第2页“癌症”中。然后我想把表2“癌症”中的值放入表1“测试”中。除了循环之外,代码也可以工作。目前它只适用于我的第一张工作表中的第一条记录,然后停止VBA循环来自另一张图纸,vba,excel,loops,Vba,Excel,Loops,我的循环无法在整个工作表1中运行,这使我遇到了问题。如果第1页“测试”中的值存在于第2页“癌症”中。然后我想把表2“癌症”中的值放入表1“测试”中。除了循环之外,代码也可以工作。目前它只适用于我的第一张工作表中的第一条记录,然后停止 Sub Testing() Dim x As Long Dim y As Long x = 2 y = 2 Do While Sheets("Cancer").Cells(y, 1).Value <> "" If LCase(Trim(S
Sub Testing()
Dim x As Long
Dim y As Long
x = 2
y = 2
Do While Sheets("Cancer").Cells(y, 1).Value <> ""
If LCase(Trim(Sheets("Cancer").Cells(y, 1).Text)) = LCase(Trim(Sheets("Tests").Cells(x, 3).Text)) Then
If Sheets("Tests").Cells(x, 4).Value = "" Then
Cells(x, 4) = (Trim(Sheets("Cancer").Cells(y, 3).Text))
x = x + 1
End If
End If
y = y + 1
Loop
End Sub
子测试()
暗x等长
长得一样暗
x=2
y=2
Do While床单(“癌症”)。单元格(y,1)。值“”
如果LCase(Trim(Sheets(“Cancer”).Cells(y,1.Text))=LCase(Trim(Sheets(“Tests”).Cells(x,3.Text)),则
如果表格(“测试”).单元格(x,4).Value=“”,则
细胞(x,4)=(修剪(薄片(“癌症”)。细胞(y,3)。文本))
x=x+1
如果结束
如果结束
y=y+1
环
端接头
您几乎已经对所有范围进行了鉴定。你错过了一个。尝试更改行:
Cells(x, 4) = (Trim(Sheets("Cancer").Cells(y, 3).Text))
到
我会使用两个循环
for y = 2 to 10000 'the range your values are found
if Sheets("Cancer").Cells(y, 1).Value <> "" then
for x = 2 to 10000 'the range your values are in
If LCase(Trim(Sheets("Cancer").Cells(y, 1).Text)) = LCase(Trim(Sheets("Tests").Cells(x, 3).Text)) and Sheets("Tests").Cells(x, 4).Value = "" Then
Cells(x, 4) = (Trim(Sheets("Cancer").Cells(y, 3).Text))
End If
next
end if
next
对于y=2到10000'可以找到您的值的范围
如果是“癌”细胞(y,1)。值“”,则
对于x=2到10000’,您的值所处的范围
如果LCase(Trim(Sheets(“Cancer”).Cells(y,1.Text))=LCase(Trim(Sheets(“Tests”).Cells(x,3.Text))和Sheets(“Tests”).Cells(x,4.Value=”“
细胞(x,4)=(修剪(薄片(“癌症”)。细胞(y,3)。文本))
如果结束
下一个
如果结束
下一个
循环未贯穿整个活页1的原因在于以下两条线:
如果LCase(修饰(表(“癌症”).细胞(y,1.Text))=LCase(修饰(表(“测试”).细胞(x,3.Text))和表(“测试”).细胞(x,4.Value=”“
如果这些条件都不是真的,那么x将永远不会循环到它的下一个迭代,并且您将通过Sheet2“Cancer”的每个值进行循环,同时只检查Sheet1“Tests”的相同记录 你有没有通过代码检查发生了什么?请学习(好的地方,但不确定如果代码第一次工作,这可能是原始问题。你是对的,我假设它正在某个地方更改活动工作表,但它不是。是我,还是这只是一个vba模拟的查找?不确定tbh!诸如此类。
for y = 2 to 10000 'the range your values are found
if Sheets("Cancer").Cells(y, 1).Value <> "" then
for x = 2 to 10000 'the range your values are in
If LCase(Trim(Sheets("Cancer").Cells(y, 1).Text)) = LCase(Trim(Sheets("Tests").Cells(x, 3).Text)) and Sheets("Tests").Cells(x, 4).Value = "" Then
Cells(x, 4) = (Trim(Sheets("Cancer").Cells(y, 3).Text))
End If
next
end if
next