Solved

Reading Excel Column's data

Posted on 2004-09-30
12
986 Views
Last Modified: 2012-06-22
I have a column in Excel called "DATE" which starts at B:6 and holds a month's worth of dates in there.

1) Is there a way to loop through Column B starting at B:6 and down, then checking each date?
2) Once I find today's date like using something like Date.Now() how would I write data starting at that specific cell?

Thanks!

Chris
0
Comment
Question by:Trancedified
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 250 total points
ID: 12194609
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12200275
That'll do it Anirban.

Bob
0
 
LVL 1

Author Comment

by:Trancedified
ID: 12200569
-Baan,

Thanks for the quick post, but I got an error:

"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll"

It is pointing to:
cmd.ExecuteNonQuery()

Based on that web page's code, I got this:

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private m_sConn1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=C:\Documents and Settings\CChang\My Documents\blah.xls;" & _
               "Extended Properties=""Excel 8.0;HDR=NO"""

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'http://support.microsoft.com/default.aspx?scid=kb;EN-US;316934#11

        Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
        conn1.Open()
        Dim cmd As New System.Data.OleDb.OleDbCommand
        cmd.Connection = conn1
        cmd.CommandText = "INSERT INTO [sheet1$C6] values ('CCC')"
        cmd.ExecuteNonQuery()   '<----- Here
        conn1.Close()

      End Sub

Any ideas?

Chris

End Class
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 12200610
Is sheet1$C6 the name of the worksheet?

Bob
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12201728
Put Try-Catch to get the exact error..

        Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
        conn1.Open()
        Dim cmd As New System.Data.OleDb.OleDbCommand
        cmd.Connection = conn1
        cmd.CommandText = "INSERT INTO [sheet1$C6] values ('CCC')"
        Try
             cmd.ExecuteNonQuery()   '<----- Here
        Catch Ex As Exception
             MsgBox(Ex.ToString)
        Finally
             conn1.Close()
        End Try

-Baan
0
 
LVL 1

Author Comment

by:Trancedified
ID: 12203650
I reworked it alittle:


        Try
            Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
            conn1.Open()
            Dim cmd As New System.Data.OleDb.OleDbCommand
            cmd.Connection = conn1

            cmd.CommandText = "INSERT INTO [Sheet1$] (A1) values ('111')"
            cmd.ExecuteNonQuery()

            conn1.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

The error message is: "The INSERT INTO statement contains the following unknown field name: 'A1'. Make sure y ou have typed the name correctly, and try the operation again.

I thought A1 would be Column A, row 1??
Chris
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 250 total points
ID: 12203679
What is the connection string like?  Are you marking the first row as headers?  If you don't then your columns are going to be F1, F2, F3...

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MySpreadsheet.XLS;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

HDR=Yes

Bob
0
 
LVL 1

Author Comment

by:Trancedified
ID: 12204291
Bob,

    Private m_sConn1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=C:\Documents and Settings\CChang\My Documents\blah.xls;" & _
               "Extended Properties=""Excel 8.0;HDR=NO;"""

I tried flipping YES or NO but I get the same error. blah.xls is just a blank excel file.


Chris
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12216401
What are the field names for the data table?

Bob
0
 
LVL 1

Author Comment

by:Trancedified
ID: 12217514
Bob,

There are no field names like "Date" or something. I'm hoping to just write to specific columns like A, B, C, D etc. but starting from a specific row.


Chris
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12217844
No, you would have to use the same column names that the OleDb provider would use, which is gets from either the header row, or uses F1, F2, F3, F4... when HDR = NO (as is in your connection string).

Bob
0
 
LVL 1

Author Comment

by:Trancedified
ID: 12264889
Bob, Baan,

I found the solution, instead of getting from a datasource, I just checked for a blank (a gray bar in a row in Excel) within a specific column, and if there's a blank BELOW that bar, add the data I need. It's alittle better than checking for Date.Now()... Thanks for your comments!

Chris
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question