请教一下ExcelVBA写的代码用Python怎么实现
我的目的是在Results这个目录里查找和Sheet1 A列相同数字,并且想返还最终结果为以下的四个选项1 - "Yes - Store and Process",
2- ”YES - Only Process和
3- “Yes - Only Store",
4 - "Yes - Unspecified"。
查找的具体内容是如果在Results B列下的单元格内有“Use”和“Storage”则返回"Yes - Store and Process"如果只有“Use”则返回YES - Only Process如果只有“Storage”则返回Yes - Only Store"如果“Use”和“Storage”都没找到则返回"Yes - Unspecified"。
用ExcelVBA能实现我的目的,我想问问用python是怎么写。
程序代码:
Sub test() Dim dic, arr, i&, brr, s$ Set dic = CreateObject("scripting.dictionary") arr = Sheets("Results").Range("a4").CurrentRegion For i = 1 To UBound(arr) dic(arr(i, 1)) = arr(i, 2) Next With Sheets("Sheet1") arr = .Range("a5").CurrentRegion ReDim brr(1 To UBound(arr), 1 To 1) For i = 1 To UBound(arr) If dic.exists(arr(i, 1)) Then s = dic(arr(i, 1)) If InStr(s, "Use") > 0 And InStr(s, "Storage") > 0 Then brr(i, 1) = "Yes - Store and Process" ElseIf InStr(s, "Use") > 0 And InStr(s, "Storage") = 0 Then brr(i, 1) = "YES - Only Process" ElseIf InStr(s, "Use") = 0 And InStr(s, "Storage") > 0 Then brr(i, 1) = "Yes - Only Store" Else brr(i, 1) = "Yes - Unspecified" End If End If Next End With [b5].Resize(UBound(brr)) = brr End Sub
程序代码:
Function bb(r As Range) Dim dic, arr, i&, s$, irow& Set dic = CreateObject("scripting.dictionary") Application.Volatile With Sheet2 irow = .Cells(Rows.Count, 1).End(3).Row arr = .Range("a4:b" & irow) End With For i = 1 To UBound(arr) dic(arr(i, 1)) = arr(i, 2) Next If dic.exists(r.Value) Then s = dic(r.Value) If InStr(s, "Use") > 0 And InStr(s, "Storage") > 0 Then bb = "Yes - Store and Process" ElseIf InStr(s, "Use") > 0 And InStr(s, "Storage") = 0 Then bb = "YES - Only Process" ElseIf InStr(s, "Use") = 0 And InStr(s, "Storage") > 0 Then bb = "Yes - Only Store" Else bb = "Yes - Unspecified" End If End If Set dic = Nothing End Function
怎么论坛没办法上传附件呀!