VBA中的列表和数组 [英] lists and arrays in VBA

查看:2367
本文介绍了VBA中的列表和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VB.NET编写方面非常陌生,甚至没有意识到VB.NET和VBA之间存在显着差异.我已经在Visual Studio中编写我的应用程序,但是我意识到我需要将其移植到Outlook中的VBA,并且需要处理一些语法问题.我已经搜索过,但是找不到VBA甚至VB6的任何确定性引用(例如msdn),据我所知,它比VB.NET更接近VBA.

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.

我将在此处包括代码的相关部分.如果有人需要更多背景信息,请告诉我-我可以发布整件事,时间不长.不过,我想使这篇文章尽可能简单.

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)

新列表和排序后的列表在列表后面的括号中给出了预期的:语句结尾".数组在Array处提供Expected:标识符.字符串DateToday在等号处给出预期的语句结尾.尝试添加到已排序列表中的结果为Expected:=.

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: =.

我已经在VB.NET上工作了两三天,而我之前从未使用过VBA或VB6,所以我只是没有必要的经验来知道从这里开始.如果你们中有人愿意帮助我,我将不胜感激!

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!

推荐答案

您将不得不更改某些数据类型,但是鉴于我使用的数据类型,您刚发布的内容的基础可能会转换为与此类似的内容.不准确.

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.

此处是指向Outlook开发人员参考的链接. http://msdn.microsoft. com/en-us/library/office/ff866465%28v = office.14%29.aspx

Here is a link to the outlook developer reference. http://msdn.microsoft.com/en-us/library/office/ff866465%28v=office.14%29.aspx

另一个可以帮助您入门的好网站是 http://www.cpearson.com/Excel/Topic.aspx

Another great site to help you get started is http://www.cpearson.com/Excel/Topic.aspx

将所有内容从VB.Net移到VBA并不是一件容易的事,因为并非所有数据类型都是相同的,并且您没有.Net框架.如果您卡住了,只需发布​​要转换的代码,您肯定会得到帮助!

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!

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

这篇关于VBA中的列表和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆