简体   繁体   中英

VB.Net pulling dates from Excel into Array

I've been having loads of trouble pulling dates from excel and loading them into an array. I have code that should, for all intents and purposes work, but nothing is being loaded into the array. When I do a search in the array for a value, no matter what it is, it says it has been found at Index -1 in the array. Code is as follows.

Dim d As String
Dim strDate(0 To 35) As String
Dim dCell As Object
Dim tDate As String = Now.ToShortDateString

    Dim dCount = WFMBook.Range("G2:AO2").Cells.Count
        For y = 1 To dCount Step +1
            For Each dCell In WFMBook.Range("G2:AO2").Cells(y).Value.ToString
                d = WFMBook.Range("G2:AO2").Cells.Value.ToString
            Next
            strDate(y) = d
            TextBox1.Text = strDate(0)
        Next

And then after all the data is supposedly loaded into the array (I have the textbox function to check whether things are in the array -- no results are printed into the textbox.) I perform this function:

Dim dindex As Integer = Array.FindIndex(strDate, Function(s2) s2 = tDate)
MsgBox("Found Date " & tDate & " at index " & dindex)

As I said previously, though, the MsgBox shows that it was found at index -1, and no array results are ever printed to the textbox. I believe it has something to do with Excel Date/Time somehow. How can I get this to properly load the dates into a string format ex "12/3/2015" into an array? Thanks!

It appears your problem is you are not iterating across your range correctly. You just need to reference the Cells property respective to your test range of WFMBook.Range("G2:AO2") .

Dim checkRange = WFMBook.Range("G2:AO2")
Dim dCount = checkRange.Cells.Count

For y = 1 To dCount
    ' Row postion (1) remains the same, but the column is incremented with y.
    d = checkRange.Cells(1, y).Value.ToString
    strDate(y) = d
    TextBox1.Text = strDate(0)
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM