2013-10-20 106 views
0

我工作的一个CommandButton宏搜索在另一个工作表中的列文本字符串,如果找到,并将文字“发现”在原来的工作表中的列。搜索文本字符串由原始工作表中两个特定单元格中的文本定义。遍历列,搜索文本

我已经构建了认定上的另一个工作表的范围内对该文本进行一些工作的代码,但它与行1000的处理时速度很慢。如何将我的代码转换为在这种情况下使用循环(我认为这是最快的方法)?

我当前的代码:

Private Sub CommandButton1_Click() 
On Error Resume Next 
Application.ScreenUpdating = False 
Dim artist As String 
artist = ActiveSheet.Range("C4").Text 

Dim title As String 
title = ActiveSheet.Range("C5").Text 

Dim tick As String 
tick = "found" 

Dim c As Range 
Dim d As Range 
For Each c In Sheets("Repertoire").Range("F1:F2000") 
For Each d In Sheets("Repertoire").Range("G1:G2000") 

If c.Value = artist And d.Value = title Then 

Sheets("Dashboard").Range("F4").Value = artist 
Sheets("Dashboard").Range("G4").Value = title 
Sheets("Dashboard").Range("H4").Value = tick 

End If 
Next 
Next 

End Sub 
+0

你有没有考虑过使用VLOOKUP? –

+0

我有,但我需要使用CommandButton来完成此任务。 VLOOKUP可以集成到VBA中吗? – Byate

+1

如果你想VBA,那么你可能要考虑使用[.Find而.FindNext(http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/) –

回答

1

Try this using Find method

Private Sub CommandButton1_Click() 
    Dim artistFound As Range, titleFound As Range, artist As String, title As String, c As Range, d As Range 

    artist = ActiveSheet.Range("C4") 
    title = ActiveSheet.Range("C5") 

    Set c = Sheets("Repertoire").Range("F1:F2000") 
    Set d = Sheets("Repertoire").Range("G1:G2000") 

    Set artistFound = c.Find(What:=artist, After:=c.Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 
    Set titleFound = d.Find(What:=title, After:=d.Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 

    If Not artistFound Is Nothing And Not titleFound Is Nothing Then 
     With Sheets("Dashboard") 
      .Range("F4").Value = artist 
      .Range("G4").Value = title 
      .Range("H4").Value = "found" 
     End With 
    End If 
End Sub 
+0

这真是太棒了 - 但是如果在同一行上找到艺术家和标题,是否有办法返回以下值? '.Range(“F4”)。Value = artist .Range(“G4”)。Value = title 。Range(“H4”)。Value =“found”' – Byate

+0

您的意思是您的匹配条件是如果艺术家和标题在“Repertoire”表单中的同一行上? –

+0

删除所有对'd'和'titleFound'的引用,并将'If ...'行替换为:'If not artistFound is Nothing and artistFound.Offset(0,1)= title Then'。这是否做到了? –

0

你真正的问题是嵌套循环。

如果需要,这两个艺术家和标题中的“剧目”在同一行上出现,那么你就需要扫描下来的列只有一次找对。

如果您需要艺术家F栏出现在任何地方和任何地方的标题出现在G列,那么你就需要先扫描下来列女性寻找艺术家,然后扫描下来G列中寻找称号。

在这两种情况下,你不需要嵌套循环。

编辑#1

基于您的评论:

Sub Button1_Click() 
    artist = ActiveSheet.Range("C4") 
    title = ActiveSheet.Range("C5") 
    tick = "Found" 
    Set c = Sheets("Repertoire").Range("F1:F2000") 
    Set d = Sheets("Repertoire").Range("G1:G2000") 
    For Each cc In c 
     If cc.Value = artist And cc.Offset(0, 1).Value = title Then 
      Sheets("Dashboard").Range("F4").Value = artist 
      Sheets("Dashboard").Range("G4").Value = title 
      Sheets("Dashboard").Range("H4").Value = tick 
      Exit Sub 
     End If 
    Next cc 
End Sub 

我,因为我是使用去虫一个表单按钮使用的button1_Click

+0

是的,刚刚意识到这是一个问题。你如何用VBA指定两个搜索字符串存在于同一行上? – Byate

+0

真的很有用,谢谢。只要我获得更多的声望点,我会尽快回复这个答案。 – Byate

+0

感谢您的反馈。 –