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是Long
类型的变量(在我的`FindEmptyColumn'函数中找到),它只是我数据中第一个空列的列号。我在设置数字格式的部分遇到问题。代码不是选择单列,而是选择该列以及每个列都大于它。
我认为它刚开始这样做......不确定原因。
答案 0 :(得分:2)
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