|
-
Jan 18th, 2006, 11:58 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 19th, 2006, 12:20 AM
#2
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:
Public Sub ExportRstToExcel(ByVal Rst As ADODB.Recordset, ByVal sName As String)
Dim i As Long
Dim vLimit As Long
Dim vCount1 As Long
Dim vCount2 As Long
Dim exl As Object
Dim wkb As Object
Dim rng As Object
'Use late-binding so that the app
'is not tied to a specific version of Excel;
'drawback is there is no intellisense and speed is slower
'Set exl = New Excel.Application --> Binded version
Set exl = CreateObject("Excel.Application") '--> initialize
'create workbook object
Set wkb = exl.Workbooks.Add
'set active worksheet
wkb.ActiveSheet.Name = sName
With Rst
'lblStatus.Caption = "Setting columns..."
vLimit = .Fields.Count - 1
'set column headers
For i = 0 To vLimit
wkb.ActiveSheet.Cells(1, i + 1).Font.Bold = True
wkb.ActiveSheet.Cells(1, i + 1).Font.Color = vbBlue
wkb.ActiveSheet.Cells(1, i + 1) = .Fields(i).Name
Next i
'lblStatus.Caption = "Loading Records..."
'load records
wkb.ActiveSheet.Cells(2, 1).CopyFromRecordset Rst
vCount1 = Rst.RecordCount + 2
'set background color for the top row (headers)
Set rng = exl.Range(exl.Cells(1, 1), exl.Cells(1, .Fields.Count))
rng.Select
With exl.Selection.Interior
.ColorIndex = 6 'yellow
.Pattern = 1 'xlSolid
End With
'autofit all values
exl.Range(exl.Cells(1, 1), exl.Cells(i, .Fields.Count + 1)).Select
exl.Selection.Columns.AutoFit
exl.Cells(2, 1).Select
End With
'freeze first row
'-------------------------------------------------------------------
'cell("A2") is already selected so we may freeze row right above it
'-------------------------------------------------------------------
exl.ActiveWindow.FreezePanes = True
exl.Visible = True
'clean up and exit
Set exl = Nothing
Set wkb = Nothing
Set rng = Nothing
Rst.Close
Set Rst = Nothing
End Sub
-
Jan 19th, 2006, 12:23 AM
#3
Frenzied Member
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
-
Jan 19th, 2006, 05:42 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 19th, 2006, 11:26 PM
#5
Thread Starter
Hyperactive Member
Re: Cell Formatting in an Excel Object
thanks dee-u and Liquid Metal.
 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?
-
Jan 19th, 2006, 11:35 PM
#6
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:
'VBA:
Sheet1.Cells(1, 1).Value = "Test"
'VB:
Dim oApp As Excel.Application
Set oApp = New Excel.Application
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 19th, 2006, 11:56 PM
#7
Thread Starter
Hyperactive Member
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
-
Jan 20th, 2006, 12:25 AM
#8
Re: Cell Formatting in an Excel Object
Its parent object is the Application object 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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 20th, 2006, 12:34 AM
#9
Thread Starter
Hyperactive Member
Re: Cell Formatting in an Excel Object
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|