使用Excel VBA将以下数据集从SQL Server数据库转储到Excel电子表格(请原谅格式化,但无法粘贴图像):
CLC_Yr IND_Period NMR_Company NMR_TourType CLC_AccountType CLC_RecCategory REC_3TableCategory Total
2014 2 00010 FIT Sales 1.2 327,964.71
2014 2 00010 FIT Sales 1.3 3.1.1 395,062.92
2014 2 00010 FIT Sales 1.3 3.1.2.1 30,783,438.87
2014 2 00010 FIT Sales 1.3 3.1.2.2 397,434.81
2014 2 00010 FIT Sales 1.3 3.1.3 354,248.45
2014 2 00010 FIT COS 1.5 2,752,042.61
2014 2 00010 FIT COS 1.6 3.2.1 1,145,008.75
2014 2 00010 FIT COS 1.6 3.2.2.1 25,376,372.13
2014 2 00010 FIT COS 1.6 3.2.3 2,121,651.62
2014 2 00010 FIT Total 63,653,224.87
2014 2 00010 GRP Sales 1.2 340,682.10
2014 2 00010 GRP COS 1.5 695,162.13
2014 2 00010 GRP Total 1,035,844.23
2014 2 00010 COMP Total 64,689,069.10
2014 2 00070 GRP Sales 1.2 268,522.54
2014 2 00070 GRP COS 1.5 250,493.17
2014 2 00070 GRP Total 519,015.71
2014 2 00070 COMP Total 519,015.71
对于每个公司,我们有各种细节线(NMR_TourType FIT / GRP和CLC_AccountType Sales / COS),其中包括FIT Total和GRP Total的小计,后跟整体公司总COMP COMP。 并非每个公司都会同时拥有FIT和GRP细节线 - 因此在上面的示例中,公司00010确实有FIT和GRP细节线,因此将包含“FIT Total' 和#GRP Total',但公司00070只有GRP细节线,所以只有一个' GRP Total'。
正在正确填充数据库表,并且当前正使用一些简单的Excel VBA代码将其转储到Excel电子表格
stSQL = "SELECT CLC_Yr, IND_Period, NMR_Company, " & _
"CASE WHEN NMR_TourType = '' THEN 'COMP' ELSE NMR_TourType END AS NMR_TourType, " & _
"CASE WHEN CLC_AccountType = '' THEN 'Total' ELSE CLC_AccountType END AS CLC_AccountType, " & _
"CLC_RecCategory, REC_3TableCategory, " & _
"[CLC_Company_Value] AS Total " & _
"FROM [ReportDB].dbo.[ray_report_totals] "
Set cnt = New ADODB.Connection
With cnt
.CursorLocation = adUseClient
.Open stADO
.CommandTimeout = 0
Set rst = .Execute(stSQL)
End With
' Set up a reference to the Report Figures Worksheet
Set wsSheet = wbBook.Worksheets("Report Figures")
' Clear out our data area as this is refreshed on each load of spreadsheet
wsSheet.Range("a10:q600").ClearContents
' On the Report Figures Worksheet we will dump the data starting from Cell A10
With wsSheet
Set rnStart = .Range("A10")
End With
' Now dump the Recordset data to the sheet from A10
rnStart.CopyFromRecordset rst
一切正常,但使用Excel VBA,我想对电子表格进行更多格式化。
因此,根据数据集,电子表格中的数据将为每个公司提供各种详细信息行(NMR_TourType FIT / GRP和CLC_AccountType Sales / COS),其中包含FIT Total和GRP Total的小计,然后是整个公司总计COMP总计。 并非每个公司都会同时拥有FIT和GRP细节线 - 因此在上面的示例中,公司00010确实有FIT和GRP细节线,因此将包含“FIT Total' 和#GRP Total',但公司00070只有GRP细节线,所以只有一个' GRP Total'。
我想在Excel VBA中应用的格式为:
a) Blank out the Company number when the same number is repeated across multiple entries, so it only appears on the first entry for that Company;
b) For the 'FIT Total', 'GRP Total' and 'COMP Total' lines have the text appear as Bold, and possibly even changes the background colour of the row to green;
c) Have a blank line after each 'COMP Total' line before the next set of entries for another Company
我希望我能够在Excel VBA中实现这种格式化,并希望在此提供一些特定的Excel VBA指南,因为这超出了我目前对Excel VBA的基本知识。
由于
雷
答案 0 :(得分:0)
学习宏格式的最简单方法是使用宏录制工具。也就是说,此代码可能适合您。
Sub FormatSheet1()
Dim BlnkLn As String, Cmpn As String, LastRow As Long
Dim RCnt As Long, MovCnt As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
MovCnt = 2
RCnt = 2
BlnkLn = "COMP"
Range("D2").Select
CName = ActiveCell.Address
Cmpn = Range(CName).Value
Do Until RCnt > LastRow 'run thru all the companies
Do Until Cmpn = BlnkLn 'find all the same company
ActiveCell.Offset(1, 0).Select
CName = ActiveCell.Address
Cmpn = Range(CName).Value
RCnt = RCnt + 1
Loop
Range(Cells(MovCnt, "C"), Cells(RCnt - 1, "C")).Select
Selection.ClearContents
Range(Cells(RCnt, "A"), Cells(RCnt, "H")).Select
Selection.Font.Bold = True ' format the COMP line
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range(Cells(RCnt + 1, "A"), Cells(RCnt + 1, "H")).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
With Selection.Interior ' insert and format line between companies
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
LastRow = LastRow + 1
RCnt = RCnt + 2
MovCnt = RCnt
Range(Cells(RCnt, "D"), Cells(RCnt, "D")).Select
CName = ActiveCell.Address
Cmpn = Range(CName).Value
Loop
End Sub