VBA – 试图select一个列,列()是select该列,每一个比它大?

Private Sub FormatSheet() ' ' This fucntion is called to correctly format the sheet headings, given the ' input worksheet (as a Boolean True/False). ' ' 'Find the first empty column; Populate Column titles iColumn = FindEmptyColumn(3) Cells(3, iColumn).Value = "Outer Surface" Cells(3, iColumn + 1).Value = "Inner Surface" Cells(3, iColumn + 2).Value = "Average" 'Set Column Formatting Columns(iColumn).ColumnWidth = 12 Columns(iColumn + 1).ColumnWidth = 12.57 Columns(iColumn + 2).ColumnWidth = 8.43 Columns(iColumn).Select Selection.NumberFormat = "0.0000" Columns(iColumn + 1).Select Selection.NumberFormat = "0.0000" Columns(iColumn + 2).Select Selection.NumberFormat = "0.0000" End Sub Public Function FindEmptyColumn(ByVal iRow As Long) As Long ' ' This function finds the first empty cell in a column given the input row ' ' Dim dCounter As Double Dim iColumnLocal As Long dCounter = 1 Do Until Cells(iRow, dCounter) = "" iColumnLocal = dCounter dCounter = dCounter + 1 Loop iColumnLocal = iColumnLocal + 1 FindEmptyColumn = iColumnLocal End Function 

在这段代码中,iColumn是一个Longtypes的variables(在我的FindEmptyColumn函数中),它只是我的数据中第一个空列的列号。 我在设置数字格式的部分有问题。 而不是select单列,代码是select该列,每一个都比它大。

我认为这只是开始这样做…不知道为什么。

 Private Sub FormatSheet() iColumn = FindEmptyColumn() Cells(3, iColumn).Value = "Outer Surface" Cells(3, iColumn + 1).Value = "Inner Surface" Cells(3, iColumn + 2).Value = "Average" Columns(iColumn).ColumnWidth = 12 Columns(iColumn + 1).ColumnWidth = 12.57 Columns(iColumn + 2).ColumnWidth = 8.43 Union(Columns(iColumn), Columns(iColumn + 1), Columns(iColumn + 2)).NumberFormat = "0.0000" End Sub Function FindEmptyColumn() As Long On Error Resume Next With Worksheets("Sheet1") FindEmptyColumn = .Cells.Find("*", .Cells(1), xlFormulas, xlWhole, xlByColumns, xlPrevious).Column + 1 If Err <> 0 Then FindEmptyColumn = 0 End If End With End Function