将代码更改为根据行内容自动调整列宽

我有一些代码在这里工作很好,但我想微调。 现在macros将三行值复制到一个新的工作簿。 这些值粘贴没有任何格式。 不是所有单元格的内容都是可见的,因为列宽有时不够大。 我的问题是以下几点:如何更改下面提供的代码,以使列变大,取决于粘贴的第一行中的文本。
如何根据第一行的内容自动调整列的大小?

码:

Public Sub pub_sub_ExportRows() '# '# declare private variables '# Dim pvt_xls_Current As Excel.Worksheet Dim pvt_wbk_New As Excel.Workbook Dim pvt_lng_SelectedSourceRow As Long Dim pvt_flg_ValidRow As Boolean Dim pvt_lng_RowNumber As Long Dim pvt_lng_FirstColumn As Long Dim pvt_lng_LastColumn As Long Dim pvt_lng_ColumnNumber As Long Dim pvt_lng_TargetColumn As Long '# '# record the current row based on the active cell '# Set pvt_xls_Current = ThisWorkbook.ActiveSheet pvt_lng_SelectedSourceRow = ActiveCell.Row '# '# the maximum number of columns to be considered is not dependent on the columns defined in the '# worksheet, but rather by a limit imposed by the user - ie column HG is the last column to be copied, '# and column Q is the first column to be considered '# pvt_lng_FirstColumn = Columns("Q").Column pvt_lng_LastColumn = Columns("HG").Column pvt_lng_TargetColumn = 0 '# '# check if the selected row is valid by examining the values in the columns on that '# row - any non-blank value implies that the selected row is valid - when looking at '# the values the search starts in column Q as requested by the user '# With pvt_xls_Current pvt_flg_ValidRow = False For pvt_lng_ColumnNumber = pvt_lng_FirstColumn To pvt_lng_LastColumn If LenB(.Cells(pvt_lng_SelectedSourceRow, pvt_lng_ColumnNumber).Value) > 0 Then pvt_flg_ValidRow = True Exit For End If Next pvt_lng_ColumnNumber End With If Not pvt_flg_ValidRow Then MsgBox "You must select a valid - ie non empty - row" Exit Sub End If If pvt_lng_SelectSourceRow > 10000 Then MsgBox "You may not select a row > 10000" Exit Sub End If '# '# create a new workbook to hold the copied values and copy & paste the information to the '# newly created workbook '# Set pvt_wbk_New = Application.Workbooks.Add With pvt_xls_Current For pvt_lng_ColumnNumber = pvt_lng_FirstColumn To pvt_lng_LastColumn If LenB(.Cells(pvt_lng_SelectedSourceRow, pvt_lng_ColumnNumber).Value) > 0 And _ InStr(1, "$AF,$BF,$CG,$DH,$ES,$FV,$HD,$HF", Split(Columns(pvt_lng_ColumnNumber).Address, ":")(0)) = 0 Then pvt_lng_TargetColumn = pvt_lng_TargetColumn + 1 pvt_wbk_New.Worksheets("Sheet1").Cells(1, pvt_lng_TargetColumn).Value = .Cells(4, pvt_lng_ColumnNumber).Value pvt_wbk_New.Worksheets("Sheet1").Cells(2, pvt_lng_TargetColumn).Value = .Cells(5, pvt_lng_ColumnNumber).Value pvt_wbk_New.Worksheets("Sheet1").Cells(3, pvt_lng_TargetColumn).Value = .Cells(pvt_lng_SelectedSourceRow, pvt_lng_ColumnNumber).Value End If Next pvt_lng_ColumnNumber End With '# '# activate the new workbook '# pvt_wbk_New.Activate End Sub 

这将自动将第一个单元格的整行设置为一个值。 它显然可以简化为只是表示第1行,但我试图复制你使用的代码:

  pvt_wbk_New.Worksheets("Sheet1").Cells(1,pvt_lng_TargetColumn).EntireRow.Columns.Autofit