Results 1 to 9 of 9

Thread: [RESOLVED] Cell Formatting in an Excel Object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Resolved [RESOLVED] Cell Formatting in an Excel Object

    hi, does anyone knows how to change the cell formatting of a cell in an excel object? like change the font, background color, etc ...

    can this be done?

    hope you guys can help.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Cell Formatting in an Excel Object

    Have a look at this, I think I've got it from this forum and modified it to suit my needs...

    VB Code:
    1. Public Sub ExportRstToExcel(ByVal Rst As ADODB.Recordset, ByVal sName As String)
    2.        
    3.     Dim i           As Long
    4.     Dim vLimit      As Long
    5.     Dim vCount1     As Long
    6.     Dim vCount2     As Long
    7.     Dim exl         As Object
    8.     Dim wkb         As Object
    9.     Dim rng         As Object
    10.        
    11.     'Use late-binding so that the app
    12.     'is not tied to a specific version of Excel;
    13.     'drawback is there is no intellisense and speed is slower
    14.    
    15.     'Set exl = New Excel.Application --> Binded version
    16.    
    17.     Set exl = CreateObject("Excel.Application") '--> initialize
    18.     'create workbook object
    19.     Set wkb = exl.Workbooks.Add
    20.     'set active worksheet
    21.     wkb.ActiveSheet.Name = sName
    22.            
    23.     With Rst
    24.         'lblStatus.Caption = "Setting columns..."
    25.         vLimit = .Fields.Count - 1
    26.         'set column headers
    27.         For i = 0 To vLimit
    28.             wkb.ActiveSheet.Cells(1, i + 1).Font.Bold = True
    29.             wkb.ActiveSheet.Cells(1, i + 1).Font.Color = vbBlue
    30.             wkb.ActiveSheet.Cells(1, i + 1) = .Fields(i).Name
    31.         Next i
    32.         'lblStatus.Caption = "Loading Records..."
    33.        
    34.         'load records
    35.         wkb.ActiveSheet.Cells(2, 1).CopyFromRecordset Rst
    36.         vCount1 = Rst.RecordCount + 2
    37.        
    38.         'set background color for the top row (headers)
    39.         Set rng = exl.Range(exl.Cells(1, 1), exl.Cells(1, .Fields.Count))
    40.         rng.Select
    41.         With exl.Selection.Interior
    42.             .ColorIndex = 6 'yellow
    43.             .Pattern = 1    'xlSolid
    44.         End With
    45.        
    46.         'autofit all values
    47.         exl.Range(exl.Cells(1, 1), exl.Cells(i, .Fields.Count + 1)).Select
    48.         exl.Selection.Columns.AutoFit
    49.         exl.Cells(2, 1).Select
    50.     End With
    51.    
    52.     'freeze first row
    53.     '-------------------------------------------------------------------
    54.     'cell("A2") is already selected so we may freeze row right above it
    55.     '-------------------------------------------------------------------
    56.     exl.ActiveWindow.FreezePanes = True
    57.    
    58.     exl.Visible = True
    59.  
    60.     'clean up and exit
    61.     Set exl = Nothing
    62.     Set wkb = Nothing
    63.     Set rng = Nothing
    64.     Rst.Close
    65.     Set Rst = Nothing
    66. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Cell Formatting in an Excel Object

    Code:
        With Range("A1").Interior
            .ColorIndex = 41
            .Pattern = xlSolid
        End With
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

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

    Re: Cell Formatting in an Excel Object

    Easiest way to determine how to do something is to record a macro and view the generated code.

    Moved from Classic VB.
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: Cell Formatting in an Excel Object

    thanks dee-u and Liquid Metal.


    Quote Originally Posted by RobDog888
    Easiest way to determine how to do something is to record a macro and view the generated code.

    Moved from Classic VB.
    i learned something new from what you said thanks. by the way, from the codes generated when you record a macro, are those codes also applicable in VB? like can i just copy & paste the codes w/o changes?

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

    Re: Cell Formatting in an Excel Object

    Yes and No. Most of it will be ok but some of it will require complete object references.
    VB Code:
    1. 'VBA:
    2. Sheet1.Cells(1, 1).Value = "Test"
    3.  
    4. 'VB:
    5. Dim oApp As Excel.Application
    6. Set oApp = New Excel.Application
    7. oApp.Workbooks("Book1.xls").Sheets("Sheet1").Cells(1, 1).Value = "Test"
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: Cell Formatting in an Excel Object

    ok, i'll keep that in mind. i was playing with excel macro recording and i noticed that it uses "Selection" object. when i looked it up in the object browser (VBA), it belongs to Excel.Window object. how come i can't find it in VB? i already made a reference to Microsoft Excel 11.0 Object Library and there is no Excel.Window object

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

    Re: Cell Formatting in an Excel Object

    Its parent object is the Application object class.
    VB Code:
    1. Application.Selection
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Location
    Philippines
    Posts
    285

    Re: Cell Formatting in an Excel Object

    thanks ....

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