0

I've written a pretty useful macro within Excel in VBA and am attempting to transfer it to a stand-alone windows application written in VB.net. I've found all the referencing pretty confusing and am now facing trouble in converting the general syntax of my code.

I'm pasting below the relevant code:

  Dim ElevenSheets As Excel.Worksheet
    Dim TwelveSheets As Excel.Worksheet
    Dim ThirteenSheets As Excel.Worksheet
    Dim WorkingSheet As Excel.Worksheet
    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Open("FILENAME.xls") 'Removed file name for company confidentiality purposes.
    ElevenSheets = xlWorkBook.Worksheets("2011")
    TwelveSheets = xlWorkBook.Worksheets("2012")
    ThirteenSheets = xlWorkBook.Worksheets("2013")
    WorkingSheet = xlWorkBook.Worksheets("WorkingSheet")
...
    Cell = WorkingSheet.Range("B3")  '<--- This line causes the error.
            CurrentCell = (Cell.Row & "," & Cell.Column)
            CurrentRow = Cell.Row
            MyColumn = (Cell.Column)
            CurrentCell = (CurrentRow & "," & MyColumn)

So, as you can see I've pointed out the line that gives me an error. I'm trying to set a range names "Cell" and the error "MissingMemberException unhandled No default member found for type 'DBNull'" presents itself.

Does anyone know what I've done wrong? I'm sure it's something very simple with my syntax but am finding this whole process difficult and also finding it difficult to understand other reasonably similar topics on the internet.

Thanks for bothering to ready this and let me know if you need more context,

Josh

4

2 回答 2

0

我没有看到 Cell 的任何构造函数。此外,您不必显式声明所有这些工作表。您可以使用 xlWorkBook("WorkingSheet").Range("B3") 之类的方法简单地调用它们

于 2013-07-24T15:56:47.920 回答
0

我已经通过反复试验解决了这个问题。我这样做的方式只是更改这些声明:

ElevenSheets = xlWorkBook.Worksheets("2011")
TwelveSheets = xlWorkBook.Worksheets("2012")
ThirteenSheets = xlWorkBook.Worksheets("2013")
WorkingSheet = xlWorkBook.Worksheets("WorkingSheet")

对此:

 ElevenSheets = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)
 TwelveSheets = CType(xlWorkBook.Worksheets(2), Excel.Worksheet)
 ThirteenSheets = CType(xlWorkBook.Worksheets(3), Excel.Worksheet)
 WorkingSheet = CType(xlWorkBook.Worksheets(4), Excel.Worksheet)

我不知道将工作表名称更改为工作表编号是固定它还是CType固定它。但其中一位做到了。

感谢您的贡献!

于 2013-07-25T09:15:40.223 回答