Results 1 to 3 of 3

Thread: VB.NET Updating Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Question VB.NET Updating Excel

    I would like to have a simple program which would update a spreadsheet from a simple VB.NET GUI.

    I would like to submit a value from my VB.NET GUI, to the spreadsheet. THe spreadsheet would then recalculate any associated fields/records.

    SO A B C
    2 2 =sum(A+B) 4

    Updated column A via VB.NET and then C would be recalculated by teh spreadsheet to = 5.

    to A B C
    3 2 5

    Not the most technically demanding challenge ever, but I have not programmed for a while and am starting off again! So if anyone could help me with an example of the source code to update Excel Spreadsheets that would be most appreciated. Thanks a lot.

  2. #2
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: VB.NET Updating Excel

    First, include 'Excel 9.0' in your References. Then...

    'Declare objects

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet

    xlApp = CreateObject("Excel.Application")

    'Open file

    xlBook = xlApp.Workbooks.Open(spreadsheetfilename)
    xlSheet = xlBook.Worksheets(worksheetnumber)

    'Then you can refer to any cell using...

    myvalue = xlSheet.Cells(rowindex, columnindex).Value

    '...and read/write cells like that

    'If you want to save changes to the workbook use:

    xlBook.Save()

    'When you've finished:

    xlApp.Quit()

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB.NET Updating Excel

    Here is an example on how to write to the cells and also enter a formula. Then update the cell with "3" and recalculate the formula.

    If your running Excel 2000 or earlier then you can not use the .Interop.

    VB Code:
    1. 'Excel 2002 (XP) or 2003
    2. Option Explicit On
    3. Option Strict On
    4. 'Add a reference to MS Excel xx.0 Object Library
    5. Imports Microsoft.Office.Interop
    6.  
    7. Public Class Form1
    8.  
    9.     Inherits System.Windows.Forms.Form
    10.  
    11. #Region " Windows Form Designer generated code "
    12.  
    13.     Public Sub New()
    14.         MyBase.New()
    15.  
    16.         'This call is required by the Windows Form Designer.
    17.         InitializeComponent()
    18.  
    19.         'Add any initialization after the InitializeComponent() call
    20.  
    21.     End Sub
    22.  
    23.     'Form overrides dispose to clean up the component list.
    24.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    25.         If disposing Then
    26.             If Not (components Is Nothing) Then
    27.                 components.Dispose()
    28.             End If
    29.         End If
    30.         MyBase.Dispose(disposing)
    31.     End Sub
    32.  
    33.     'Required by the Windows Form Designer
    34.     Private components As System.ComponentModel.IContainer
    35.  
    36.     'NOTE: The following procedure is required by the Windows Form Designer
    37.     'It can be modified using the Windows Form Designer.  
    38.     'Do not modify it using the code editor.
    39.     Friend WithEvents Button1 As System.Windows.Forms.Button
    40.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    41.         Me.Button1 = New System.Windows.Forms.Button
    42.         Me.SuspendLayout()
    43.         '
    44.         'Button1
    45.         '
    46.         Me.Button1.Location = New System.Drawing.Point(192, 88)
    47.         Me.Button1.Name = "Button1"
    48.         Me.Button1.TabIndex = 0
    49.         Me.Button1.Text = "Button1"
    50.         '
    51.         'Form1
    52.         '
    53.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    54.         Me.ClientSize = New System.Drawing.Size(284, 122)
    55.         Me.Controls.Add(Me.Button1)
    56.         Me.Name = "Form1"
    57.         Me.Text = "Form1"
    58.         Me.ResumeLayout(False)
    59.  
    60.     End Sub
    61.  
    62. #End Region
    63.  
    64.     Private moApp As Excel.Application
    65.  
    66.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    67.         moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    68.         moApp.Visible = False
    69.     End Sub
    70.  
    71.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    72.         If Not moApp Is Nothing Then
    73.             moApp.Quit()
    74.         End If
    75.         moApp = Nothing
    76.     End Sub
    77.  
    78.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    79.         Dim oWB As Excel.Workbook = moApp.Workbooks.Add
    80.         Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Sheet1"), Excel.Worksheet)
    81.         oSht.Cells(1, 1) = "2"
    82.         oSht.Cells(1, 2) = "2"
    83.         oSht.Cells(1, 3) = "=SUM(A1, B1)"
    84.         moApp.Visible = True
    85.         MessageBox.Show("Before Calculate()")
    86.         oSht.Cells(1, 1) = "3"
    87.         oSht.Calculate()
    88.         MessageBox.Show("After Calculate()")
    89.         oWB.Close(True, "C:\Test.xls")
    90.         oSht = Nothing
    91.         oWB = Nothing
    92.     End Sub
    93.  
    94. End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width