lists and arrays in VBA

拈花ヽ惹草 提交于 2020-01-02 02:21:10

问题


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)

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

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!


回答1:


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

Another great site to help you get started is 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!

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


来源:https://stackoverflow.com/questions/17028592/lists-and-arrays-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!