Autofit VBA excel提取

我在Excel中创build一个提取。 我从SQL Server中提取数据 – 然后写入excel。 起初我通过指定行来创build标题,见下面….

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 

然后我写数据,使用logging集

 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。 实际上因为有很多logging,所以我想根据最长评论的长度自动调整或自动resize。 我不知道我应该在哪里包括这一点,使其自动适应。 我将如何解决这个问题?

使用AutoFit方法:

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