0

I've been trying to create a VB.NET VTSO addin, where excel columns are rearranged depending on the column header value.

I found (online) VBA code that does exactly that, but Visual Basic does not recognise the "Dim v = ..." line.

Does anyone know how I can remedy this.

Dim v As Object, x As Object, findfield As Object
    Dim oCell As Excel.Range
    Dim iNum As Long

    Dim v = Array("First Name", "Middle Name", "Last Name", "Date of Birth", "Phone Number", "Address", "City", "State", "Postal (ZIP) Code", "Country")
    For x = LBound(v) To UBound(v)
        findfield = v(x)
        iNum = iNum + 1

        oCell = ActiveSheet.Rows(1).Find(What:=findfield, LookIn:=Excel.XlFindLookIn.xlValues, LookAt:=Excel.XlLookAt.xlWhole, SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext, MatchCase:=False, SearchFormat:=False)

        If Not oCell.Column = iNum Then
            ThisApplication.ThisWorkbook.Columns(oCell.Column).Cut
            ThisApplication.ThisWorkbook.Columns(iNum).Insert(Shift:=Excel.XlInsertShiftDirection.xlShiftToRight)
        End If
    Next x
1
  • Dim v As String() = { "First Name", "Middle Name", "Last Name", "Date of Birth", "Phone Number", "Address", "City", "State", "Postal (ZIP) Code", "Country" }. How to: Initialize Array in Variable in Visual Basic Commented Oct 26, 2015 at 21:12

1 Answer 1

1

Tim nailed it on the head with his comment on the fix.

    Dim v As String() = New String(9) {"First Name", "Middle Name", "Last Name", "Date of Birth", "Phone Number", "Address", "City", "State", "Postal (ZIP) Code", "Country"}

The Array Class is an Abstract Class, meaning you can't initialize it directly, but rather must do so through one of its children (another Class that inherits from it). In .Net you must specify the underlying Type of an Array (Object counts too), typically by adding parenthesis after the Type in the variable declaration.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Guys! Worked perfectly!
` New String(9) ` not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.