vba 如何在 Excel 电子表格中创建 Tab 键顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8426403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:39:34  来源:igfitidea点击:

How to create tab order in Excel Spreadsheet

excel-vbavbaexcel

提问by user1049518

I have a spreadsheet, it contains 18 columns. What I want to do is when doing data entry the tab will move till end of the record, i.e. 18col. Then it goes to the next row 1st col. If user wants to go at middle of the record, it won`t allow.

我有一个电子表格,它包含 18 列。我想要做的是在进行数据输入时,选项卡将移动到记录的末尾,即18col. 然后它转到下一行第一列。如果用户想在记录的中间去,它不会允许。

If it is possible to do through VBA, please help me.

如果可以通过VBA完成,请帮助我。

Thanks

谢谢

采纳答案by Rachel Hettinger

If just need to keep the user from selecting outside the desired range (18 columns), you don't even need VBA. Use sheet protection and don't allow users to select locked cells. Here's a quick review of how sheet protection works: http://www.dummies.com/how-to/content/how-to-protect-cell-data-in-excel-2010.html

如果只需要防止用户选择超出所需范围(18 列),您甚至不需要 VBA。使用工作表保护并且不允许用户选择锁定的单元格。以下是对工作表保护工作原理的快速回顾:http: //www.dummies.com/how-to/content/how-to-protect-cell-data-in-excel-2010.html

If you also need to enforce that cells are populated in order, you'll need to add this VBA code. It uses an event procedure to unlock the "next" cell if the "previous" cell was populated; if it wasn't, the current cell is moved back to the previous cell so the user can fill it in.

如果您还需要强制按顺序填充单元格,则需要添加此 VBA 代码。如果填充了“上一个”单元格,则它使用事件过程来解锁“下一个”单元格;如果不是,则将当前单元格移回上一个单元格,以便用户可以填充它。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rngPrev As Range
    Dim rngNext As Range
    Const LAST_COL As Long = 18

    Select Case Target.Column
        Case Is = 1
            If Target.Row > 1 Then
                Set rngPrev = Target.Offset(-1, LAST_COL - 1)
            Else
                Set rngPrev = Target
            End If
            Set rngNext = Target.Offset(0, 1)
        Case 2 To LAST_COL - 1
            Set rngPrev = Target.Offset(0, -1)
            Set rngNext = Target.Offset(0, 1)
        Case 18
            Set rngPrev = Target.Offset(0, -1)
            Set rngNext = Target.Offset(1, 1 - LAST_COL)
    End Select
    If Not IsEmpty(rngPrev.Value) > 0 Then
        Me.Unprotect
        rngNext.Locked = False
        ' note: this line is for testing purposes to show which cells get unlocked
        rngNext.Interior.ColorIndex = 6
        Me.Protect
    Else
        rngPrev.Activate
    End If
End Sub