質問

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

役に立ちましたか?

解決 2

I have fixed the problem through trial and error. The way I did it was simply change these declarations:

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

To this:

 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)

I don't know whether changing the sheet names to the sheet number fixed it or the CType fixed it. But one of them did.

Thank-you for your contributions!

他のヒント

I dont see any constructor for Cell. Also you dont have to explicitly declare all those worksheets. You can simply call them using something like xlWorkBook("WorkingSheet").Range("B3")

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top