返回列表 上一主題 發帖

[發問] 請教如何尋找遠端磁碟中excel檔案工作表結果

回復 3# chengtoo
沒搜尋出結果 會一直出現錯誤在 最後面的 End If  執行後沒這錯誤
試試看 修改是你所需嗎?
  1. Sub QuickSearch()
  2.     Dim wks As Excel.Worksheet
  3.     Dim rCell As Excel.Range
  4.     Dim szLookupVal As String
  5.     szLookupVal = InputBox("key in", "key in msg", "")
  6.     If szLookupVal = "" Then Exit Sub
  7.     Application.ScreenUpdating = False
  8.     'Application.DisplayAlerts = False
  9.     On Error GoTo Sheet_add        '程式碼有錯誤:GoTo(到) Sheet_add 繼續執行程式碼
  10.     With Sheets("Result").Cells    '工作表不存在會有錯誤
  11.         .Clear
  12.         .Cells(1) = "下面也有所需要資訊" & szLookupVal
  13.         .EntireColumn.AutoFit
  14.         .HorizontalAlignment = xlCenter
  15.     End With
  16.     On Error GoTo 0                 '程式碼有錯誤:不處裡->預防有不知的錯誤可再修正程式碼
  17.     i = 2
  18.     For Each wks In ActiveWorkbook.Worksheets
  19.         If wks.Name = "Result" Then Exit For
  20.         With wks.Cells
  21.             Set rCell = .Find(szLookupVal, , , xlWhole, xlByColumns, xlNext, False)
  22.             If Not rCell Is Nothing Then
  23.                 szFirst = rCell.Address
  24.                 Do
  25.                     With Sheets("Result")
  26.                         .Cells(i, 1) = wks.Name & "'!" & rCell.Address
  27.                         .Cells(i, 2) = rCell
  28.                     End With
  29.                     rCell.Interior.ColorIndex = 19
  30.                     Set rCell = .FindNext(rCell)
  31.                     i = i + 1
  32.                 Loop While Not rCell Is Nothing And rCell.Address <> szFirst
  33.             End If
  34.         End With
  35.     Next wks
  36.     Set rCell = Nothing
  37.     Sheets("Result").Activate
  38.     If i = 2 Then MsgBox "所要查找的值{" & szLookupVal & "}在工作表中沒有", 64, "找不到喔"
  39.     Application.ScreenUpdating = True
  40.    ' Application.DisplayAlerts = True
  41.    Exit Sub    '離開程序-> 不再執行  Sheet_add:下的程式碼
  42. Sheet_add:  'Result不存在時新增工作表
  43.     Sheets.Add ActiveSheet
  44.     ActiveSheet.Name = "Result"
  45.     Resume       '回到程式碼錯誤行繼續執行程式
  46. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 5# chengtoo
對於不了解的VBA 函數,方法屬性 ,多看說明會進步的
  1.       '.....程式碼     
  2.      Set rCell = Nothing
  3.     Sheets("Result").Activate
  4.     If i = 2 Then
  5.         Application.DisplayAlerts = False    '停止:系的統詢問視窗
  6.         MsgBox "所要查找的值{" & szLookupVal & "}在工作表中沒有", 64, "找不到喔"
  7.         Sheets("Result").Delete
  8.         Application.DisplayAlerts = True   '恢復:系統的詢問視窗
  9.     End If
  10.     Application.ScreenUpdating = True
  11.    '.....程式碼
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 地上種了菜,就不易長草;心中有善,就不易生惡。
返回列表 上一主題