Excel

时间:2017-09-07 15:00:56

标签: excel excel-vba vba

我可以在Excel中制作工作簿并指定行数和列数吗?例如20行和5列?我正在制作一个始终具有相同行数和列数的电子表格,我想禁用滚动到无穷大选项。

2 个答案:

答案 0 :(得分:0)

Wether Excel Worksheet Format有一个固定的工作表大小(1,048,576行乘16,384列),你需要做一个这样的技巧:

  
      
  • 在Excel中禁用滚动限制行数和列数
  •   
  • 使用隐藏行和列限制行数和列数
  •   

你有一个link,他们清楚地解释了包含图像的方法和所需的所有步骤。

答案 1 :(得分:0)

下面的代码隐藏了额外的行和列,并限制了单元格的选择/移动

(只需更新Module1中的MAX_COL和MAX_ROW常量)

ThisWorkbook 模块中的代码:

Option Explicit

Public Sub MinimizeAllSheets()
    Dim ws As Worksheet

    Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
        MinimizeSheet ws
    Next
    Application.ScreenUpdating = True
End Sub

Private Sub Workbook_Open()
    MinimizeAllSheets
End Sub

'If ScrollArea is not set
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    RestrictMovement Target
End Sub

Module1 中的代码(通用模块):

Option Explicit

Private Const MAX_COL = 5   'Update default limits (all sheets)
Private Const MAX_ROW = 20

Public Sub MinimizeSheet(Optional ByRef ws As Worksheet, _
                         Optional ByVal maxCol As Long = MAX_COL, _
                         Optional ByVal maxRow As Long = MAX_ROW)

    If maxCol > 0 And maxRow > 0 Then
        Dim actWs As Worksheet
        If ws Is Nothing Then
            Set ws = ActiveSheet
        Else
            If ws.Name <> ActiveSheet.Name Then
                Set actWs = ActiveSheet
                ws.Activate
            End If
        End If
        ActiveWindow.ScrollColumn = 1
        ActiveWindow.ScrollRow = 1
        With ws.Range(ws.Cells(1, maxCol + 1), ws.Cells(1, Columns.Count))
            .EntireColumn.Hidden = True
        End With
        ws.Rows(maxRow + 1 & ":" & Rows.Count).EntireRow.Hidden = True
        'ScrollArea also limits the selection
        ws.ScrollArea = ws.Range(ws.Cells(1), ws.Cells(maxRow, maxCol)).Address
        If Not actWs Is Nothing Then actWs.Activate
    End If
End Sub

Public Sub RestrictMovement(ByVal Target As Range)  'If ScrollArea is not set
    With Target
        If .CountLarge = 1 Then
            If .Column > MAX_COL Then .Parent.Cells(.Row, MAX_COL).Activate
            If .Row > MAX_ROW Then .Parent.Cells(MAX_ROW, .Column).Activate
        Else
            If (.Column - 1) + .Columns.Count > MAX_COL Then
                Set Target = .Resize(.Rows.Count, MAX_COL)
            End If
            If (.Row - 1) + .Rows.Count > MAX_ROW Then
                Set Target = .Resize(MAX_ROW, .Columns.Count)
            End If
        End If
    End With
End Sub