Excel 我可以循环这个吗?
有一个很好的时间循环这个;D2是列表的起点。我想让它运行d3,d4,d5,d6。。。。直到一片空白 此外,我将把数据放在E列中,这也需要像D列一样增加;e2,e3,e4,e5,e6Excel 我可以循环这个吗?,excel,while-loop,vba,Excel,While Loop,Vba,有一个很好的时间循环这个;D2是列表的起点。我想让它运行d3,d4,d5,d6。。。。直到一片空白 此外,我将把数据放在E列中,这也需要像D列一样增加;e2,e3,e4,e5,e6 Sub james() 'Main Program Dim celltxt As String celltxt = ActiveSheet.Range("D2").Value DELETE_EJ If InStr(1, celltxt, "Christy", vbTextCompare) Then Ran
Sub james() 'Main Program
Dim celltxt As String
celltxt = ActiveSheet.Range("D2").Value
DELETE_EJ
If InStr(1, celltxt, "Christy", vbTextCompare) Then
Range("E2").Value = "Christy"
ElseIf InStr(1, celltxt, "Kari", vbTextCompare) Then
Range("E2").Value = "Kari"
ElseIf InStr(1, celltxt, "Sue", vbTextCompare) Then
Range("E2").Value = "Sue"
ElseIf InStr(1, celltxt, "Clayton", vbTextCompare) Then
Range("E2").Value = "Clayton"
是的,您可以将名称放入数组中,然后在数组中循环:
Sub james() 'Main Program
Dim celltxt As String
Dim nmArr()
nmArr = Array("Christy", "Kari", "Sue", "Clayton")
celltxt = ActiveSheet.Range("D2").Value
DELETE_EJ
For i = LBound(nmArr) To UBound(nmArr)
If InStr(1, celltxt, nmArr(i), vbTextCompare) Then
Range("E2").Value = nmArr(i)
Exit For
End If
Next i
End Sub
是的,您可以将名称放入数组中,然后在数组中循环:
Sub james() 'Main Program
Dim celltxt As String
Dim nmArr()
nmArr = Array("Christy", "Kari", "Sue", "Clayton")
celltxt = ActiveSheet.Range("D2").Value
DELETE_EJ
For i = LBound(nmArr) To UBound(nmArr)
If InStr(1, celltxt, nmArr(i), vbTextCompare) Then
Range("E2").Value = nmArr(i)
Exit For
End If
Next i
End Sub
是的,定义一个要循环的范围,然后可以使用名称列表上的内部循环这样做:
Sub foo() 'Main Program
Dim nmArr()
Dim i as Long
Dim loopRange as Range
Dim cl As Range
'## This is the range you will loop over
Set loopRange = ActiveSheet.Range("D2:D6") '## Modify as needed
'## This is the list of names built as an array
nmArr = Array("Christy", "Kari", "Sue", "Clayton")
DELETE_EJ
For Each cl in loopRange.Cells
For i = LBound(nmArr) to Ubound(nmArr)
If Instr(1, cl.Value, nmArr(i), vbTextCompare) Then
cl.Offset(0,1).Value = nmArr(i)
Exit For
End If
Next
Next
End Sub
上面要求对范围进行硬编码,但如果需要在找到空白单元格之前进行硬编码,请按如下方式修改:
Option Explicit
Sub foo() 'Main Program
Dim nmArr()
Dim i As Long
Dim cl As Range
Set cl = ActiveSheet.Range("D2") '## This is the STARTING cell
'## This is the list of names built as an array
nmArr = Array("Christy", "Kari", "Sue", "Clayton")
DELETE_EJ
Do
For i = LBound(nmArr) To UBound(nmArr)
If InStr(1, cl.Value, nmArr(i), vbTextCompare) Then
cl.Offset(0, 1).Value = nmArr(i)
Exit For
End If
Next
'## Get a handle on the NEXT cell
Set cl = cl.Offset(1, 0)
Loop Until Trim(cl.Text) = vbNullString
End Sub
第二种方法已经过测试,并致力于产生如下输出:
是的,定义一个要循环的范围,然后您可以使用名称列表上的内部循环这样做:
Sub foo() 'Main Program
Dim nmArr()
Dim i as Long
Dim loopRange as Range
Dim cl As Range
'## This is the range you will loop over
Set loopRange = ActiveSheet.Range("D2:D6") '## Modify as needed
'## This is the list of names built as an array
nmArr = Array("Christy", "Kari", "Sue", "Clayton")
DELETE_EJ
For Each cl in loopRange.Cells
For i = LBound(nmArr) to Ubound(nmArr)
If Instr(1, cl.Value, nmArr(i), vbTextCompare) Then
cl.Offset(0,1).Value = nmArr(i)
Exit For
End If
Next
Next
End Sub
上面要求对范围进行硬编码,但如果需要在找到空白单元格之前进行硬编码,请按如下方式修改:
Option Explicit
Sub foo() 'Main Program
Dim nmArr()
Dim i As Long
Dim cl As Range
Set cl = ActiveSheet.Range("D2") '## This is the STARTING cell
'## This is the list of names built as an array
nmArr = Array("Christy", "Kari", "Sue", "Clayton")
DELETE_EJ
Do
For i = LBound(nmArr) To UBound(nmArr)
If InStr(1, cl.Value, nmArr(i), vbTextCompare) Then
cl.Offset(0, 1).Value = nmArr(i)
Exit For
End If
Next
'## Get a handle on the NEXT cell
Set cl = cl.Offset(1, 0)
Loop Until Trim(cl.Text) = vbNullString
End Sub
第二种方法已经过测试,并致力于产生如下输出:
因为根本没有循环,所以循环的时间很长?@findwindow你今天很顺利;)@Bungle lol先生只说了一个明显的XD循环时间很长
,因为你根本就没有循环?@findwindow你今天玩得很开心;)@Bungle lol先生只是说了一个明显的XDWow,这比所有代码都单独列出名称要好得多。如何使D2和E2循环运行该行?D3信息到E3???@JSt见大卫的答案。我错过了第二圈。谢谢@scottcramer!哇,这比所有代码都单独列出名称要好得多。如何使D2和E2循环运行该行?D3信息到E3???@JSt见大卫的答案。我错过了第二圈。谢谢@scottcramer!现在运行时错误“91”对象变量或未设置块变量?我在哪里能读到这样的东西??我真的很感谢你的帮助@davidzemens@JSt我在哪里可以读到一些东西
试试。找到它,然后修复它<代码>cl
未分配任何内容。修改为在第二种方法中根本不使用loopRange
变量,它现在应该可以工作了。希望我能不止一次地升级投票,因为你刚刚完成了工作。@ScottCraner如果我在发布答案之前通过编译器运行它,对我来说工作就会少一些。哈哈哈:DNow RunTime Error'91'Object variable或block variable not set?我在哪里能读到这样的东西??我真的很感谢你的帮助@davidzemens@JSt我在哪里可以读到一些东西
试试。找到它,然后修复它<代码>cl未分配任何内容。在第二种方法中,修改为根本不使用loopRange
变量,它现在应该可以工作了。希望我能因为你刚才所做的工作而多次投票。@ScottCraner如果我在发布答案之前只运行编译器,对我来说就不会那么麻烦了哈哈哈:D