Excel提取vba

我正在做一个Excel摘要,并有一些关于格式化的问题。 所以基本上我正在做的是进入SQL SERVER并select一堆数据。 然后我写它到Excel(在VB6应用程序)。

xlrow=6 xlcol=1 While Not g_RS3.EOF For i = 0 To RCOUNT - 1 Cells(xlRow, xlCol).Value = g_RS3("School") xlCol = xlCol + 1 Cells(xlRow, xlCol).Value = g_RS3("LastName") & " ," & g_RS3("FirstName") xlCol = xlCol + 1 Cells(xlRow, xlCol).Value = g_RS3("Q01") xlCol = xlCol + 1 Cells(xlRow, xlCol).Value = g_RS3("Q02") xlCol = xlCol + 1 Cells(xlRow, xlCol).Value = g_RS3("Q03") xlCol = xlCol + 1 Cells(xlRow, xlCol).Value = g_RS3("q04") xlCol = xlCol + 1 Cells(xlRow, xlCol).Value = g_RS3("q05") xlcol=1 xlrow=xlrow+1 next i wend 

所以我把这些数据写入Excel,一切都很好。 但是我很难格式化。

我想使字体= 9,并在单元格周围放置边框。 因为这是dynamic的(我不知道会有多less条logging,我试图在写入时将其格式化)。 每次我做xlcol=xlcol+1 ,我都xlcol=xlcol+1这一行放在这里,所以我做了类似的事情:

 xlcol=xlcol+1 cells(xlrow, xlcol).font.size=9 

但是我必须在每一行之后这样做,也要有边界。 有没有更简单的方法来做到这一点? 谢谢!

编辑:包括这个代码不为我做这项工作,它只给了我左右边界,而不是顶部和底部,因为它遍及整个范围。 也只能改变第一列的字体….“学校”

 Dim rng As Range Set rng = Range(Cells(6, 1), Cells(xlRow, xlCol)) rng.Font.Size = 9 rng.Borders(xlEdgeBottom).LineStyle = xlContinuous rng.Borders(xlEdgeTop).LineStyle = xlContinuous rng.Borders(xlEdgeRight).LineStyle = xlContinuous rng.Borders(xlEdgeLeft).LineStyle = xlContinuous 

我需要在每行数据后面写一些边框

你的循环之后为什么不是这样的:

 Dim rng As Range Set rng = Range(Cells(6,1), Cells(xlrow, xlcol)) rng.font.size = 9 rng.Borders.Color = RGB(0,0,0) rng.Borders(xlEdgeTop).Weight = xlThick etc...