Results 1 to 2 of 2

Thread: excel cells

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    excel cells

    when saving data to an excel worksheet, you reference the cell by coordinates: .cells(1,1) for ex.

    IF you can't use a variable to increment because you are in a recursive loop, how do you save to the next cell?


    VB Code:
    1. Public Sub PrintName(ByVal cont As Control)
    2.  
    3.         Dim xl As New Excel.Application
    4.         Dim wb As Excel.Workbook
    5.         Dim ws As Excel.Worksheet
    6.  
    7.         For Each ctl As Control In cont.Controls
    8.             Application.DoEvents()
    9.  
    10.             wb = xl.Workbooks.Open("C:\Documents and Settings\arichardson\Desktop\controlNames.xls")
    11.             ws = wb.Worksheets.Item(1)
    12.             ws.Cells(1, 1).Value = ctl.Name
    13.             ws.Cells(2, 1).Value = ctl.Parent.Name
    14.             wb.Save()
    15.             wb.Close()
    16.  
    17.             Try
    18.                 If TypeOf ctl Is TabControl Then
    19.  
    20.                     PrintName(ctl)
    21.                 End If
    22.  
    23.                 If TypeOf ctl Is TabPage Then
    24.                     PrintName(ctl)
    25.                 End If
    26.  
    27.                 If TypeOf ctl Is GroupBox Then
    28.                     PrintName(ctl)
    29.                 End If
    30.  
    31.             Catch
    32.  
    33.             End Try
    34.  
    35.         Next
    36.  
    37.  
    38.     End Sub

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045
    Andy ...

    Did you ever get this solved? Why do you bang the workbook for every control? Why not just open it BEFORE iterating the controls, load the cells using index variables, and then save the workbook after all the controls are processed. I can help with this one ... I iterate through Excel sheets all the time. Let me know if you are still working on it.

    By the way, Thank you for the concise example of getting into an Excel sheet ... I'm a VB.Net Newbie.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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