簡體   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