Rename Worksheet in excel through codes

Discussion in 'Programming General' started by WalangAlam, May 14, 2007.

Rename Worksheet in excel through codes
  1. Unread #1 - May 14, 2007 at 9:33 PM
  2. WalangAlam
    Referrals:
    0

    WalangAlam Guest

    Rename Worksheet in excel through codes

    How to Rename Worksheet in excel through code?
    Example Sheet1 to MyWorksheet using a code in VB.Net
     
  3. Unread #2 - May 14, 2007 at 10:52 PM
  4. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Rename Worksheet in excel through codes

    the actual file name or the excel speadsheet. or rename one of the sheets inside that file?
     
  5. Unread #3 - May 15, 2007 at 12:38 AM
  6. WalangAlam
    Referrals:
    0

    WalangAlam Guest

    Rename Worksheet in excel through codes

    yes rename one of the sheets inside that file. We have only one sheet in the excel file and i want to rename it using code.
     
  7. Unread #4 - May 15, 2007 at 3:04 AM
  8. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Rename Worksheet in excel through codes

    I don't think its possible unless Excel can handle "Macro Scripting" or you do something shitty like getting the handle of its window and using the mouse to click on it etc.
     
  9. Unread #5 - May 15, 2007 at 10:12 AM
  10. dodge
    Joined:
    Mar 26, 2007
    Posts:
    125
    Referrals:
    1
    Sythe Gold:
    5

    dodge Active Member
    Banned

    Rename Worksheet in excel through codes

    Here are two ways of doing this

    OLD VB6 way and still works on .net

    Code:
    Dim xlApp As Object = CreateObject("Excel.Application")
    Dim xlBook As Object = xlApp.Workbooks.Open("c:\Test.xls")
    xlBook.Worksheets(1).Name = xlBook.Name 'or any name you want
    xlBook.Close(1)
    xlApp.Quit()
    
    .net way (still using COM Excel object) i like this one better since you can do more with this

    Code:
           Dim exc As New Application
            Dim workbooks As Workbooks = exc.Workbooks
            Dim workbook As Workbook = workbooks.Open("c:\Test.xls")
            Dim sheets As Sheets = workbook.Worksheets
            Dim worksheet As Worksheet = CType(sheets.Item(1), Worksheet)
            worksheet.Name = "WOWThis is Cool"
    
    note for this last one you will need the follwing

    add the follwing references to your project
    [​IMG]

    add this import to your class file
    Code:
    Imports Microsoft.Office.Interop.Excel
     
< need help with a jeopardy c++ game | Binding Combobox to a dataset >

Users viewing this thread
1 guest


 
 
Adblock breaks this site