| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1043 人关注过本帖
标题:请教一下ExcelVBA写的代码用Python怎么实现
只看楼主 加入收藏
大惺惺
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-9-2
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
请教一下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


怎么论坛没办法上传附件呀!
搜索更多相关主题的帖子: Process InStr If and End 
2021-09-02 15:44
_xXx_
Rank: 2
等 级:论坛游民
威 望:1
帖 子:4
专家分:40
注 册:2021-9-5
收藏
得分:20 
能回答你这个问题的至少得两个都会啊……
2021-09-05 19:52
快速回复:请教一下ExcelVBA写的代码用Python怎么实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019719 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved