fork download
  1. Option Explicit
  2.  
  3. Public Sub 検索結果クリア()
  4. Dim sh As Worksheet
  5. Dim maxrow As Long
  6. Set sh = Worksheets("検索結果")
  7. maxrow = sh.Cells(Rows.Count, 1).End(xlUp).Row 'A列の最大行取得
  8. If maxrow < 3 Then Exit Sub
  9. sh.Range("A3:F" & maxrow).ClearContents
  10. MsgBox ("クリア完了")
  11. End Sub
  12.  
  13.  
  14. Public Sub 検索実行()
  15. Dim sh As Worksheet
  16. Dim ws As Worksheet
  17. Dim maxrow1 As Long '検索対象シート最大行
  18. Dim maxrow2 As Long '検索結果シート最大行
  19. Dim wrow As Long
  20. Dim frow As Long '検索した文字列のある商品の1行目の行番号
  21. Dim krow As Long '検索結果シートの行番号
  22. Dim sheet_name As String '検索対象シート名
  23. Dim find_str As String '検索対象文字列
  24. Dim result As Long
  25. Set sh = Worksheets("検索結果")
  26. sheet_name = sh.Range("H2").Value
  27. If sheet_name = "" Then
  28. MsgBox ("検索シート名未設定")
  29. Exit Sub
  30. End If
  31. find_str = sh.Range("I2").Value
  32. If find_str = "" Then
  33. MsgBox ("検索文字未設定")
  34. Exit Sub
  35. End If
  36. If isExistSheet(sheet_name) = False Then Exit Sub
  37. Set ws = Worksheets(sheet_name)
  38. maxrow1 = ws.Cells(Rows.Count, 1).End(xlUp).Row '検索対象シート A列の最大行取得
  39. maxrow2 = sh.Cells(Rows.Count, 1).End(xlUp).Row '検索結果シート A列の最大行取得
  40. frow = 0
  41. For wrow = 3 To maxrow1
  42. '該当文字が存在すれば、設定を行う
  43. If InStr(1, ws.Cells(wrow, "A").Value, find_str, vbTextCompare) > 0 Then
  44. If ws.Cells(wrow, "C").Value <> "" And ws.Cells(wrow, "F").Value <> "" Then
  45. frow = wrow - 1
  46. ElseIf ws.Cells(wrow + 1, "C").Value <> "" And ws.Cells(wrow + 1, "F").Value <> "" Then
  47. frow = wrow
  48. ElseIf ws.Cells(wrow - 1, "C").Value <> "" And ws.Cells(wrow - 1, "F").Value <> "" Then
  49. frow = wrow - 2
  50. Else
  51. frow = 1
  52. End If
  53. Exit For
  54. End If
  55. Next
  56. If frow = 0 Then
  57. MsgBox ("該当文字列なし")
  58. Exit Sub
  59. End If
  60. If frow = 1 Then
  61. ws.Activate
  62. ws.Cells(wrow, "A").Select
  63. MsgBox ("該当文字はあるが、数量、金額なし")
  64. Exit Sub
  65. End If
  66. sh.Range("A" & maxrow2 + 1 & ":F" & maxrow2 + 3).Value = ws.Range("A" & frow & ":F" & frow + 2).Value
  67. MsgBox ("検索完了")
  68. End Sub
  69. 'シートの存在チェック
  70. Private Function isExistSheet(ByVal sheet_name As String) As Boolean
  71. isExistSheet = False
  72. On Error GoTo ERROR99
  73. Dim ws As Worksheet
  74. Set ws = Worksheets(sheet_name)
  75. isExistSheet = True
  76. Exit Function
  77. ERROR99:
  78. MsgBox (sheet_name & "は存在しません")
  79. End Function
  80.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty