我想编写一个脚本来获取表中使用的列总数,并将数字保存为数字。我不想选择数据。
Sub AddHeaders()
Dim NumColumns As Number
NumberColumns = ???
答案 0 :(得分:1)
使用此:
{{1}}
这将为您提供第1行中使用的最后一列,将1更改为您要使用的任何行。
答案 1 :(得分:1)
像
这样的东西intCol = ThisWorkbook.Sheets("Tabelle1").UsedRange.Columns.Count
将为您提供最后一列的索引,其中一个单元格中包含任何内容或特定格式。
答案 2 :(得分:0)
以防万一,以下是其他答案的替代方案:
intCol = ThisWorkbook.Sheets("Tabelle1").Range("a2").CurrentRegion.Columns.Count
这将为您提供列数,而其他答案将返回列号。请注意,CurrentRegion不会包含空列右侧或空行下方的单元格。根据要求,CHoice是你的。
@Martin Dreher响应是一个完美的通用答案,但有一个小缺点:如果清除工作表右侧的范围,在关闭/重新打开工作簿之前,可能不会重置UsedRange。
@Jordan似乎也是一个很好的答案,如果你知道哪一行是最宽的。
答案 3 :(得分:0)
这里有一个完全超出最佳答案 - 将为您提供工作簿中所有单独区域的详细信息(区域由空行和列分隔)。
如果表格上只有一张桌子,你就没有说明 - 所以这就是全部。
Sub Test()
Dim aLists As Variant
Dim x As Long
Dim rng As Range
'//Find lists in the this workbook.
aLists = FindRegionsInWorkbook(ThisWorkbook)
For x = LBound(aLists) To UBound(aLists)
Set rng = Range(aLists(x))
Debug.Print rng.Parent.Name & "!" & rng.Address & _
" | FirstCol: " & rng.Column & _
" | LastCol: " & rng.Column + rng.Columns.Count - 1 & _
" | TopRow: " & rng.Row & _
" | BottomRow: " & rng.Row + rng.Rows.Count - 1 & _
" | TotalRows: " & rng.Rows.Count & _
" | TotalColumns: " & rng.Columns.Count
Next x
Debug.Assert False
End Sub
'---------------------------------------------------------------------------------------
' Procedure : FindRegionsInWorkbook
' Author : Zack Barresse (MVP), Oregon, USA. (http://www.mrexcel.com/forum/showthread.php?t=309052)
' Date : 20/03/2008
' Purpose : Returns each region in each worksheet within the workbook in the 'sRegion' variable.
'---------------------------------------------------------------------------------------
Public Function FindRegionsInWorkbook(wrkBk As Workbook) As Variant
Dim ws As Worksheet, rRegion As Range, sRegion As String, sCheck As String
Dim sAddys As String, arrAddys() As String, aRegions() As Variant
Dim iCnt As Long, i As Long, j As Long
'//Cycle through each worksheet in workbook.
j = 0
For Each ws In wrkBk.Worksheets
sAddys = vbNullString
sRegion = vbNullString
On Error Resume Next
'//Find all ranges of constant & formula valies in worksheet.
sAddys = ws.Cells.SpecialCells(xlCellTypeConstants, 23).Address(0, 0) & ","
sAddys = sAddys & ws.Cells.SpecialCells(xlCellTypeFormulas, 23).Address(0, 0)
If Right(sAddys, 1) = "," Then sAddys = Left(sAddys, Len(sAddys) - 1)
On Error GoTo 0
If sAddys = vbNullString Then GoTo SkipWs
'//Put each seperate range into an array.
If InStr(1, sAddys, ",") = 0 Then
ReDim arrAddys(0 To 0)
arrAddys(0) = "'" & ws.Name & "'!" & sAddys
Else
arrAddys = Split(sAddys, ",")
For i = LBound(arrAddys) To UBound(arrAddys)
arrAddys(i) = "'" & ws.Name & "'!" & arrAddys(i)
Next i
End If
'//Place region that range sits in into sRegion (if not already in there).
For i = LBound(arrAddys) To UBound(arrAddys)
If InStr(1, sRegion, ws.Range(arrAddys(i)).CurrentRegion.Address(0, 0)) = 0 Then
sRegion = sRegion & ws.Range(arrAddys(i)).CurrentRegion.Address(0, 0) & "," '*** no sheet
sCheck = Right(arrAddys(i), Len(arrAddys(i)) - InStr(1, arrAddys(i), "!"))
ReDim Preserve aRegions(0 To j)
aRegions(j) = Left(arrAddys(i), InStr(1, arrAddys(i), "!") - 1) & "!" & ws.Range(sCheck).CurrentRegion.Address(0, 0)
j = j + 1
End If
Next i
SkipWs:
Next ws
On Error GoTo ErrHandle
FindRegionsInWorkbook = aRegions
Exit Function
ErrHandle:
'things you might want done if no lists were found...
End Function