2013-08-26 62 views
0

我想使用字符串FindString的每個實例的值來填充UserForm1中的文本框。在每個打開的工作表中搜索字符串

我正在爲每個文本框獲取唯一的WorkSheet。但其餘的值來自運行該模塊時激活的工作表。

這意味着字符串Rng不循環通過WorkSheets,但保留在初始WorkSheet中。我該如何補救?

Public Sub FindString() 
Dim FindString As Variant 
Dim Rng As Range 
Dim SheetName As String 
Dim ws As Worksheet 
Dim i As Integer 

Application.ScreenUpdating = False 
SheetName = ActiveSheet.Name 

FindString = Cells(ActiveCell.Row, 1).Value 

FindString = InputBox("Enter the case number to search for:", "Case ID", FindString) 
If FindString = "" Then Exit Sub 
If FindString = False Then Exit Sub 


i = 1 

For Each ws In Worksheets 
    If ws.Name Like "Lang*" Then 
     With ws 

      If Trim(FindString) <> "" Then 
       With Range("A:A") 
        Set Rng = .Find(What:=FindString, _ 
            After:=.Cells(.Cells.Count), _ 
            LookIn:=xlValues, _ 
            LookAt:=xlWhole, _ 
            SearchOrder:=xlByRows, _ 
            SearchDirection:=xlNext, _ 
            MatchCase:=False) 
        If Not Rng Is Nothing Then 

      UserForm1.Controls("TextBox" & i) = ws.Name & vbTab & _ 
      Rng.Offset(0, 2).Value & vbTab & _ 
      Rng.Offset(0, 5).Value & vbTab & _ 
      Rng.Offset(0, 6).Value & vbTab & _ 
      Rng.Offset(0, 7).Value & vbTab & _ 
      Rng.Offset(0, 8).Value 

      i = i + 1 

        Else: GoTo NotFound 
        End If 
       End With 
      End If 

     End With 
    End If 
Next ws 

Sheets(SheetName).Activate 
Application.ScreenUpdating = True 
UserForm1.Show 

Exit Sub 

NotFound: 
Sheets(SheetName).Activate 
Application.ScreenUpdating = True 
MsgBox "Case ID not found" 
Exit Sub 

End Sub 

回答

0

知道了!

只是需要加入

ws.Activate

If ws.Name Like "Lang*" Then