热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

VBA如何使用字典。-VBAhowtouseadictionary

IamgettingissuesinusingadictionaryinVBA.Iwanttoaddvaluesfromasheettoadictionary.

I am getting issues in using a dictionary in VBA. I want to add values from a sheet to a dictionary. If I use simple lists, there is no error in the code. Like this.

我在使用VBA词典时遇到了一些问题。我想从表中添加值到字典中。如果我使用简单的列表,代码中没有错误。像这样。

Function Account(Place As String) As String

Dim cities(500)
Dim accounts(500)

For i = 2 To 500
    cities(i) = Worksheets("Sheet2").Cells(i, 2).Value
    accounts(i) = Worksheets("Sheet2").Cells(i, 3).Value
Next i

placeName = StrConv(Place, vbProperCase)
Account = placeName

End Function

This code does not give an issue but if I add the code for the dictionary, there is some issue.

这段代码没有出现问题,但是如果我为字典添加代码,就会出现问题。

Function Account(Place As String) As String

Dim cities(500)
Dim accounts(500)
Dim dict
Set dict = CreateObject(Scripting.Dictionary)

For i = 2 To 500
    cities(i) = Worksheets("Sheet2").Cells(i, 2).Value
    accounts(i) = Worksheets("Sheet2").Cells(i, 3).Value
    dict(cities(i)) = accounts(i)
Next i

placeName = StrConv(Place, vbProperCase)
Account = placeName
dict = Nothing

End Function

Can someone point out the error. I am new to vba so I dont know much about it.

有人能指出错误吗?我是vba的新手,所以我对它不太了解。

3 个解决方案

#1


3  

The folowing UDF loads a dictionary object with places as keys (unique) and associated accounts as items. After the dictionary has been loaded, it looks up the Place parameter passed into the function and returns the account if found.

folowing UDF装载一个字典对象,它的位置作为键(唯一的)和关联的帐户作为项目。在加载了字典之后,它将查找传递到函数中的Place参数并返回该帐户。

Option Explicit

Function Account(Place As String) As String
    Static d As Long, dict As Object

    If dict Is Nothing Then
        Set dict = CreateObject("Scripting.Dictionary")
        dict.comparemode = vbTextCompare
    Else
        dict.RemoveAll
    End If

    With Worksheets("Sheet2")
        For d = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
            dict.Item(.Cells(d, "B").Value2) = .Cells(d, "C").Value2
        Next d
    End With

    If dict.exists(Place) Then
        Account = dict.Item(Place)
    Else
        Account = "not found"
    End If

End Function

Note that beyond other corrections, the code to instantiate the dictionary object is CreateObject("Scripting.Dictionary") not CreateObject(Scripting.Dictionary).

注意,除了其他更正之外,实例化dictionary对象的代码是CreateObject(“scriptl . dictionary”),而不是CreateObject(scriptl . dictionary)。

#2


0  

One possible area of concern, brought to mind by one of your comments, lies in the use of "Sheet1" and "Sheet2". In Excel VBA, there are two different ways to refer to a worksheet. The is the Name of the worksheet, which is what the user sees on the tabs in Excel, and the user can change at will. Thtese default to names like "Sheet1", "Sheet2", etc.

您的评论中提到的一个可能令人关注的领域是“Sheet1”和“Sheet2”的使用。在Excel VBA中,有两种不同的方法来引用工作表。是工作表的名称,这是用户在Excel中的选项卡上看到的,用户可以随意更改。默认名称为“Sheet1”、“Sheet2”等。

There is also the "Codename" for each worksheet. In the Visual Basic Editor, the project explorer window will list all the worksheets under "Microsoft Excel Objects". There you'll see the Codename for each worksheet, with the Name of the worksheet in parentheses.

每个工作表都有“Codename”。在Visual Basic编辑器中,project explorer窗口将列出“Microsoft Excel对象”下的所有工作表。在这里,您将看到每个工作表的代号,并在圆括号中显示工作表的名称。

When you use Worksheets("Sheet1"), the "Sheet1" refers to the Name, not the Codename. It's possible to end up with a worksheet with the Name "Sheet1" and the codename "Sheet2".

当您使用工作表(“Sheet1”)时,“Sheet1”指的是名称,而不是代号。有可能最终得到一个名为“Sheet1”和代号“Sheet2”的工作表。

As far as your functions are concerned, I note that in both cases you declare local variables -- the arrays 'cities' and 'accounts' in the first, and those two plus the dictionary 'dict' in the second. You have code to fill those local variables, but then do nothing with them. The return value of the function is not dependent on any of those local variables.

就函数而言,我注意到在这两种情况下都声明了局部变量——第一个是数组“cities”和“accounts”,第二个是两个加上字典“dict”。您有代码来填充这些局部变量,但是对它们什么都不做。函数的返回值不依赖于任何这些局部变量。

Once the function code completes, those local variables lose their values. VBA returns the memory it used to store those variables to its pool of available memory, to be reused for other purposes.

一旦函数代码完成,这些局部变量就会丢失它们的值。VBA将用于将这些变量存储到可用内存池中的内存返回,以供其他用途重用。

Try commenting-out the entire for...next loop, and you'll see that the value return from the function is unchanged.

试着为…下一个循环,您将看到函数返回的值没有改变。

I'm not certain what you intend to accomplish in these functions. It would be helpful for you to explain that.

我不确定你打算在这些函数中完成什么。如果你能解释一下,会有帮助的。

#3


-2  

To use CreateObject(Scripting.Dictionary) you have to reference "Microsoft Scripting Runtime". The problem is you probably have to upgrade the .dll file from microsoft website because your current .dll from standart windows installation doesn't support this method (I've been in the same situation myself with a Windows 7 version).

要使用CreateObject(script. dictionary),您必须引用“Microsoft脚本运行时”。问题是,你可能需要从微软网站升级.dll文件,因为你当前的.dll文件不支持这个方法(我自己也有windows 7版本)。

However, I tried to reach microsoft page where I downloaded the update and can't find it. So, I recommend you cross your array contents:

然而,我试图进入微软的页面,在那里我下载了更新,却找不到它。所以,我建议你交叉你的数组内容:

Function Account(Place As String) As String

Dim cities(500)
Dim accounts(500)

For i = 2 To 500
    cities(i) = Worksheets("Sheet2").Cells(i, 2).Value
    accounts(i) = Worksheets("Sheet2").Cells(i, 3).Value
Next i

For i = 2 To 500
    If cities(i) = Place Then
        placeName = accounts(i)
        Exit For
    End If
Next i

Account = placeName

End Function

推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
author-avatar
妩媚别说你LOVE我_383
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有