Results 1 to 6 of 6

Thread: [Excel, C#2005] Getting Cell From CellName

  1. #1

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    [Excel, C#2005] Getting Cell From CellName

    I'm using the Excel Interop to input a number of strings into cells. The cells can change depending on the excel "template" I open.

    The cell names are constant however: the template file will always have a number of input cells with specific names.

    What I need to do (In Visual C#, 2005) is get the specific cell in the worksheet from a name I specify in the code, and change it's value.

    So far I have the following set up:
    Code:
    Excel.Application oXLApp = new Excel.Application();
                oXLApp.Visible = true;
                Excel.Workbook oXLBook = oXLApp.Workbooks.Open(
                  Application.StartupPath + "\\Template1.xls", 
                  openFileDialog1.FileName, 0, true, 5,
                  "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
                  0, true);
                Excel.Worksheet oSht = (Excel.Worksheet)oXLBook.Worksheets.get_Item(1);
    From there I'm not sure where to go. Any help would be appreciated.
    Qu.
    Last edited by Quasar6; Aug 27th, 2008 at 08:56 PM. Reason: Add more info to title
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  2. #2
    Lively Member
    Join Date
    May 2007
    Posts
    70

    Re: [Excel, C#2005] Getting Cell From CellName

    It's probably easier if you give us an example of what you mean by

    What I need to do (In Visual C#, 2005) is get the specific cell in the worksheet from a name I specify in the code, and change it's value.
    what's the name you specify in the code? how is the name related to the worksheet?

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel, C#2005] Getting Cell From CellName

    i guess this should work, assuming the cell name is a defined named range
    osht.Range("myname").Value = 123
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: [Excel, C#2005] Getting Cell From CellName

    Sorry guys: what I meant was something like this (see attachment).

    Rather than specifying (in code) that I'd like to input a string into Cell[2,2], I'd like to specify Cell["INPUT1"].
    "INPUT1" could be any cell in the file.
    Attached Images Attached Images  
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel, C#2005] Getting Cell From CellName

    did you try the code i posted, it should do what you want, change "myname" to "input1"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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

    Re: [Excel, C#2005] Getting Cell From CellName

    See post #3 of my Office/C# FAQ:
    http://www.vbforums.com/showthread.php?t=406639

    I show how to access a specific cell (1, 1) in the example procedure "btnOpen_Click"
    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