【问题标题】:Converting VBA Arrays to VB.NET将 VBA 数组转换为 VB.NET
【发布时间】:2015-10-26 21:10:56
【问题描述】:

我一直在尝试创建一个 VB.NET VTSO 插件,其中 excel 列根据列标题值重新排列。

我发现(在线)VBA 代码可以做到这一点,但 Visual Basic 无法识别“Dim v = ...”行。

有谁知道我该如何解决这个问题。

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

【问题讨论】:

标签: arrays vb.net excel vba


【解决方案1】:

蒂姆用他对修复的评论把它钉在了头上。

    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"}

Array Class 是一个抽象类,这意味着您不能直接初始化它,而必须通过它的一个子类(从它继承的另一个类)来初始化它。在 .Net 中,您必须指定数组的基础类型(对象也很重要),通常通过在变量声明中的类型之后添加括号。

【讨论】:

  • 谢谢各位!完美运行!
  • `新字符串(9)`不需要。
猜你喜欢
  • 2014-10-10
  • 1970-01-01
  • 2015-07-28
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多