lists and arrays in VBA

Wolves picture Wolves · Jun 10, 2013 · Viewed 121.9k times · Source

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!

Answer

Ripster picture Ripster · Jun 10, 2013

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