vba VBA中的列表和数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17028592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
lists and arrays in VBA
提问by Wolves
I am extremely new at writing in VB.NET, and I did not even realise that there was a significant difference between VB.NET and VBA. I have been writing my application in Visual Studio, but I realised that I will need to port it over to VBA in Outlook, and there are some syntax issues that I need to deal with. I have already searched, but I cannot find any sort of definitive reference (like the msdn) for VBA or even VB6, which from what I hear is much closer to VBA than VB.NET.
我对用 VB.NET 编写代码非常陌生,我什至没有意识到 VB.NET 和 VBA 之间存在显着差异。我一直在 Visual Studio 中编写我的应用程序,但我意识到我需要将它移植到 Outlook 中的 VBA,并且我需要处理一些语法问题。我已经搜索过,但我找不到任何关于 VBA 甚至 VB6 的明确参考(如 msdn),据我所知,它比 VB.NET 更接近 VBA。
I will include the relevant sections of code here. If anyone needs more context, please let me know--I can post the whole thing, it's not that long. I would like to keep this post as simple as possible, though.
我将在此处包含相关的代码部分。如果有人需要更多背景信息,请告诉我——我可以发布整个内容,不会那么长。不过,我想让这篇文章尽可能简单。
Dim DateToday As String = String.Format("0:{yyyy/MM/dd}", DateTime.Now)
Dim Computers As New SortedList()
Dim disabledList As New List(Of String)
'\ four additional lists
Dim compArray As Array
...
Computers.Add(ComputerName, ErrorState)
The new lists and sorted list give Expected: End of Statement at the parenthesis after List. The array gives Expected: identifier at Array. The string DateToday gives an expected end of statement at the equals sign. The attempt to add to the sorted list gives an Expected: =.
新列表和排序列表在 List 之后的括号中给出了 Expected: End of Statement。该数组在 Array 处给出了 Expected: 标识符。字符串 DateToday 在等号处给出预期的语句结束。添加到排序列表的尝试给出了一个 Expected: =。
I have been working with VB.NET for maybe two or three days, and I have never worked with VBA or VB6 before, so I just do not have the experience required to know where to go from here. If any of you would be willing to help me out, I would really appreciate it!
我已经使用 VB.NET 工作了大约两三天,我以前从未使用过 VBA 或 VB6,所以我只是没有所需的经验来知道从哪里开始。如果你们中有人愿意帮助我,我将不胜感激!
回答by Ripster
You will have to change some of your data types but the basics of what you just posted could be converted to something similar to this given the data types I used may not be accurate.
您将不得不更改某些数据类型,但鉴于我使用的数据类型可能不准确,您刚刚发布的内容的基础知识可以转换为类似的内容。
Dim DateToday As String: DateToday = Format(Date, "yyyy/MM/dd")
Dim Computers As New Collection
Dim disabledList As New Collection
Dim compArray(1 To 1) As String
'Assign data to first item in array
compArray(1) = "asdf"
'Format = Item, Key
Computers.Add "ErrorState", "Computer Name"
'Prints "ErrorState"
Debug.Print Computers("Computer Name")
Collections cannot be sorted so if you need to sort data you will probably want to use an array.
集合无法排序,因此如果您需要对数据进行排序,您可能需要使用数组。
Here is a link to the outlook developer reference. http://msdn.microsoft.com/en-us/library/office/ff866465%28v=office.14%29.aspx
这是 Outlook 开发人员参考的链接。 http://msdn.microsoft.com/en-us/library/office/ff866465%28v=office.14%29.aspx
Another great site to help you get started is http://www.cpearson.com/Excel/Topic.aspx
另一个帮助您入门的好网站是 http://www.cpearson.com/Excel/Topic.aspx
Moving everything over to VBA from VB.Net is not going to be simple since not all the data types are the same and you do not have the .Net framework. If you get stuck just post the code you're stuck converting and you will surely get some help!
将所有内容从 VB.Net 转移到 VBA 并不容易,因为并非所有数据类型都相同,而且您没有 .Net 框架。如果您遇到困难,只需发布您在转换时遇到问题的代码,您一定会得到一些帮助!
Edit:
编辑:
Sub ArrayExample()
Dim subject As String
Dim TestArray() As String
Dim counter As Long
subject = "Example"
counter = Len(subject)
ReDim TestArray(1 To counter) As String
For counter = 1 To Len(subject)
TestArray(counter) = Right(Left(subject, counter), 1)
Next
End Sub