繁体   English   中英

Autofit VBA Excel摘录

[英]Autofit VBA excel extract

我在Excel中创建摘录。 我正在从SQL Server中提取数据-然后将其写入Excel。 首先,我通过指定行来创建标题,请参见下文。

With xlSheetInfo
.Cells(5, 1).ColumnWidth = 50
.Cells(5, 1).Value = "School"
.Cells(5, 2).ColumnWidth = 25
.Cells(5, 2).Value = "Name"
.Cells(5, 3).Value = "Q1"
.Cells(5, 4).Value = "Comments"
end with

然后我使用记录集写入数据

While Not g_RS3.EOF
For i = xlCol To rCount
    Cells(xlRow, xlCol).Value = g_RS3("Location")
    Cells(xlRow, xlCol).Font.Bold = True
    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("Comments")
next i
wend

我遇到的问题是“注释”,因为此字段最多可以包含500个字符。 因为这是我的摘录中的最后一列,所以我希望它是AUTOFIT。 实际上,因为有很多记录,我希望它根据最长注释的长度自动调整或自动调整大小。 我不确定应该在哪里添加它以使其自动适应。 我该如何解决?

使用AutoFit方法:

Cells(xlRow, xlCol).Value = g_RS3("Comments")
Cells(xlRow, xlCol).EntireColumn.AutoFit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM