返回列表 上一主題 發帖

[發問] 如何載入一檔案並判斷特定欄位做運算後產生一新檔案(內含舊檔與運算後之結果)

本帖最後由 GBKEE 於 2014-12-22 16:51 編輯

回復 2# happycoccolin
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim AR, Sh As Worksheet
  4.     Dim i As Long, Msg As Variant, W As Single, M As Single, s As String, filein As String, fileout As String
  5.      filein = Application.GetOpenFilename(FileFilter:="Excel 活頁簿 (*.xlsx),*.xlsx", Title:="請選擇要比對的檔案")
  6.      If Not TypeName(filein) = "String" Then Exit Sub '取消則結束
  7.      With Workbooks.Open(filein)
  8.         Set Sh = .Sheets(1)
  9.         AR = Sh.UsedRange.Columns("S")
  10.      End With
  11.     AR(1, 1) = "PASS/FAIL"
  12.     For i = 2 To UBound(AR)
  13.         Msg = ""
  14.         Select Case Cells(i, "O")
  15.             
  16.             Case ""
  17.                 '**********************************************************************
  18.                 '2.若是O欄(Implementation)為空,請在S欄位(結果欄位)顯示"無工作電壓/電流"
  19.                 Msg = "無工作電壓/電流"
  20.             Case "C"
  21.                 'IF(O1="C",IF(『擷取M1欄"/"後字元』*0.6>Q1,PASS,FAIL))
  22.                 Msg = Val(Split(Cells(i, "M"), "/")(1)) * 0.6 > Cells(i, "Q")
  23.             Case "R"
  24.                 Msg = Split(Cells(i, "P"), "_")   'Msg = P欄中用 "_" 字串分割傳回的陣列
  25.                 If UBound(Msg) = 0 Then           '陣列元素只有一個,P欄中沒有"_"的字元
  26.                     Msg = Split(Cells(i, "P"), "_")(0)
  27.                 ElseIf UBound(Msg) > 0 Then        'P欄中有"_"的字元
  28.                     Msg = Split(Cells(i, "P"), "_")(1)
  29.                     If Mid(UCase(Msg), 1, 1) = "H" Then
  30.                       '"r0603_hxx"(xx是變數) : r+後四碼字串_hxx
  31.                         Msg = Split(Cells(i, "P"), "_")(0)
  32.                     End If
  33.                 End If
  34.                 W = 0
  35.                 Select Case Right(Trim(Msg), 4)  ' P欄後4碼字串
  36.                     Case "0402"           '零件大小
  37.                         W = 0.0625        '功率(W)
  38.                     Case "0603"
  39.                         W = 0.1
  40.                     Case "0805"
  41.                         W = 0.125
  42.                     Case "1206"
  43.                         W = 0.25
  44.                     Case "1210"
  45.                         W = 0.3333
  46.                     Case "1812"
  47.                         W = 0.5
  48.                     Case "2010"
  49.                         W = 0.75
  50.                     Case "2512"
  51.                         W = 1
  52.                 End Select
  53.                 '**********************************************************************
  54.                 '4.以下這段,若是Kohm & Mohm中間帶一個空格(K ohm & M ohm),一樣可以判斷出來嗎?
  55.                 '**************************************************************************
  56.                 Msg = UCase(Right(Trim(Cells(i, "M")), 5))
  57.                
  58.                 If Msg = "K OHM" Or Msg = "M OHM" Then   '讀取5個字元,取得歐姆單位
  59.                     'M1需判斷值:以歐姆值計算.若為Kohm:其值×1000、若為Mohm:其值×1000000)
  60.                     '例如:M1=2.64Kohm=2.64*1000=2640
  61.                      M = Val(Cells(i, "M")) * 10000        'Kohm
  62.                     If Msg = "M OHM" Then M = Val(Cells(i, "M")) * 1000000
  63.                     'IF(O1="R",IF(OR((M1<>"0ohm"),(M1<>"0 ohm")),IF(Q1平方/2640-2640*N1<0.0625*0.6,PASS,FAIL)))
  64.                     Msg = Cells(i, "Q") ^ 2 / M - M * Cells(i, "N") < W * 0.6
  65.                 End If
  66.                
  67.                 If Not IsNumeric(Msg) Then                      '5個字元,非歐姆單位
  68.                     Msg = UCase(Right(Trim(Cells(i, "M")), 4))  '讀取4個字元,取得歐姆單位
  69.                     If Msg = "0OHM" Or Msg = " OHM" Then  '『非0 ohm』:ohm類的字元前面可能帶空格,部分未帶空格
  70.                         'IF(O1="R",IF(OR((M1="0ohm"),(M1="0 ohm")),IF(Q1平方*N1< W參閱項目2*0.6,PASS,FAIL)))
  71.                         M = Val(Cells(i, "M"))
  72.                         Msg = Cells(i, "Q") ^ 2 * Cells(i, "N") < W * 0.6
  73.                     ElseIf Msg = "KOHM" Or Msg = "MOHM" Then
  74.                         'M1需判斷值:以歐姆值計算.若為Kohm:其值×1000、若為Mohm:其值×1000000)
  75.                         '例如:M1=2.64Kohm=2.64*1000=2640
  76.                         M = Val(Cells(i, "M")) * 10000        'Kohm
  77.                         If Msg = "MOHM" Then M = Val(Cells(i, "M")) * 1000000
  78.                         'IF(O1="R",IF(OR((M1<>"0ohm"),(M1<>"0 ohm")),IF(Q1平方/2640-2640*N1<0.0625*0.6,PASS,FAIL)))
  79.                         Msg = Cells(i, "Q") ^ 2 / M - M * Cells(i, "N") < W * 0.6
  80.                     End If
  81.                 End If
  82.                 If Not IsNumeric(Msg) Then                  '4個字元,非歐姆單位
  83.                     Msg = UCase(Right(Trim(Cells(i, "M")), 3))  '剩下3個字元,最小的歐姆單位
  84.                     M = Val(Cells(i, "M"))
  85.                     Msg = Cells(i, "Q") ^ 2 * Cells(i, "N") < W * 0.6
  86.                
  87.                 End If
  88.             Case "BEAD"
  89.                 'IF(O1="Bead",IF(Q1平方<『判斷F1欄”/”後字元,若出現mA字元,其值要÷1000反之則否』平方*0.6,PASS,FAIL)))
  90.                 '例如:F1值=FERRITE BEAD(0402)600OHM/300mA,則執行”/”後字元=300mA=300/1000
  91.                 'IF(O1="Bead",IF(Q1平方<0.3平方*0.6,PASS,FAIL)))
  92.                 If InStr(Cells(i, "f"), "/") Then   '判斷F1欄 有”/”此字元
  93.                     M = Val(Split(Cells(i, "f"), "/")(1))
  94.                     Msg = InStr(UCase(Split(Cells(i, "f"), "/")(1)), "MA")
  95.                     If Msg Then M = Val(Split(Cells(i, "f"), "/")(1)) / 1000
  96.                     Msg = Cells(i, "Q") ^ 2 > M
  97.                 End If
  98.         End Select
  99.         If Msg <> "" Then
  100.             If Msg = "無工作電壓/電流" Then
  101.                 AR(i, 1) = Msg
  102.             Else
  103.                 AR(i, 1) = IIf(Msg, "PASS", "FAIL")
  104.             End If
  105.         End If
  106.     Next
  107.    
  108.     With Sh.UsedRange.Columns("S")
  109.         .Cells = AR
  110.         Msg = Array("PASS", "FAIL")
  111.         For i = 0 To UBound(Msg)
  112.             .Replace Msg(i), "=EX", xlWhole
  113.              With .SpecialCells(xlCellTypeFormulas, xlErrors)
  114.                 .Cells = Msg(i)
  115.                 'PASS顯示綠底黑字 FAIL顯示紅底白字
  116.                 .Font.Color = IIf(i = 0, vbBlack, vbWhite)
  117.                 .Interior.Color = IIf(i = 0, vbGreen, vbRed)
  118.              End With
  119.         
  120.         Next
  121.         .SpecialCells(xlCellTypeConstants).EntireRow.Copy
  122.     End With
  123.      '**********************************************************
  124.     '1.我目前是想做成我可以執行此程式後跳出一個視窗->讓我挑選要載入的檔案->載入後執行運算->跳出視窗讓我可另存新檔~
  125.    If MsgBox("請問是否要儲存檔案?", vbYesNo) = vbYes Then
  126.         fileout = Application.GetSaveAsFilename(FileFilter:="Excel 活頁簿 (*.xlsx),*.xlsx", Title:="另存為新檔")
  127.         If Not TypeName(fileout) = "String" Then Exit Sub '取消則結束
  128.         With Workbooks.Add(1)
  129.             .Sheets(1).Paste
  130.             .SaveAs fileout ', FileFormat:=xlWorkbookDefault
  131.             .Close True
  132.         End With
  133.     Else
  134.          Application.CutCopyMode = False
  135.       
  136.     End If

  137.     '******************************
  138.     '1不要將結果顯示在原檔案
  139.     Sh.Parent.Close False
  140.    
  141. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 5# happycoccolin
3#程式碼已更新,請試試看.
3.另外,若是針對"R"的部分,在P欄會出現"r0603_hxx"(xx是變數) or "r0603"的字元,一樣可以判斷出來嗎?(原先是預設為:mx_r0603)
請出示 P欄所有要處裡的字串元範例
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 8# happycoccolin
可以試著套上用,不行可再提問.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-12-22 15:48 編輯

回復 10# happycoccolin
錯誤請參考 http://forum.twbts.com/viewthread.php?tid=6733
  1.   '**********************************************************
  2.     '1.我目前是想做成我可以執行此程式後跳出一個視窗->讓我挑選要載入的檔案->載入後執行運算->跳出視窗讓我可另存新檔~
  3.    If MsgBox("請問是否要儲存檔案?", vbYesNo) = vbYes Then
  4.         fileout = Application.GetSaveAsFilename(FileFilter:="Excel 活頁簿 (*.xlsx),*.xlsx", Title:="另存為新檔")
  5.         If Not TypeName(fileout) = "String" Then Exit Sub '取消則結束
  6.         SH.Copy
  7.         With ActiveWorkbook
  8.             .SaveAs fileout ', FileFormat:=xlWorkbookDefault
  9.             .Close True
  10.         End With
  11.       
  12.     End If

  13.     '******************************
  14.     '1不要將結果顯示在原檔案
  15.     SH.Parent.Close False
  16.   
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 15# happycoccolin
寫不出結果沒關係,PO上妳所寫的,看看如何改.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 17# happycoccolin
請修改 看看
  1. Case "C"
  2.                 'IF(O1="C",IF(『擷取M1欄"/"後字元』*0.6>Q1,PASS,FAIL))
  3.                 If Right(Cells(i, "M"), 2) = "KV" Then Msg = True
  4.                     '在M欄位會出現KV的單位,需要*1000
  5.                 Msg = Val(Split(Cells(i, "M"), "/")(1)) * IIf(Msg = True, 1000, 1) * 0.6 > Cells(i, "Q")
  6.             Case "R"
  7.                 Msg = Split(Cells(i, "P"), "_")   'Msg = P欄中用 "_" 字串分割傳回的陣列
  8.                 If UBound(Msg) = 0 Then           '陣列元素只有一個,P欄中沒有"_"的字元
  9.                     Msg = Split(Cells(i, "P"), "_")(0)
  10.                 ElseIf UBound(Msg) > 0 Then        'P欄中有"_"的字元
  11.                     Msg = Split(Cells(i, "P"), "_")(UBound(Split(Cells(i, "P"), "_")))
  12.                     If Mid(UCase(Msg), 1, 1) = "H" Then
  13.                       '"r0603_hxx"(xx是變數) : r+後四碼字串_hxx
  14.                         Msg = Split(Cells(i, "P"), "_")(UBound(Split(Cells(i, "P"), "_")) - 1)
  15.                     Else
  16.                         Msg = Split(Cells(i, "P"), "_")(UBound(Split(Cells(i, "P"), "_")))
  17.                     End If
  18.                 End If
  19.                 W = 0
  20.                 Select Case Right(Trim(Msg), 4)  ' P欄後4碼字串
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 有智慧才能分辨善惡邪正;有謙虛才能建立美滿人生。
返回列表 上一主題