【问题标题】:Defined number of rows and columns in ExcelExcel中定义的行数和列数
【发布时间】:2017-09-07 15:00:56
【问题描述】:

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

【问题讨论】:

  • 选择整行或整列,右键,隐藏。
  • 选中所有未使用的行或列,右键隐藏

标签: excel vba


【解决方案1】:

如果Excel Worksheet Format 具有固定的工作表大小(1,048,576 行 x 16,384 列),您将需要执行以下技巧之一:

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

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

【讨论】:

    【解决方案2】:

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

    (只需更新 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多