Excel VBA自动创建行号

vyswwuz2  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(247)

我上网搜索一个excel VBA代码,以自动输入列中的行号,我找到了下面的代码。它工作得很漂亮,只是它会检测工作表中单击的每个单元格。我想改变它,使它只检测表中的单元格,或者,如果不可能,它只检测A列中的单元格。我怎么能改变它?我的表有8列A:H
下面是代码:

Option Explicit 
 
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) 
    Dim RowOffset As Long 
    Dim IndexCol As String 
     'Set values
    RowOffset = 0 
     'Change the C to the column where you want the numbers to show
    IndexCol = "C" 
     
    Intersect(ActiveCell.EntireRow, Columns(IndexCol)).Value = ActiveCell.Row + RowOffset 
End Sub
0dxa2lsx

0dxa2lsx1#

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)提供了一个名为Target的范围变量,它表示已选择的范围。在代码中使用该变量来检查该列是否是要对其进行操作的列。

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Dim RowOffset As Long
    Dim IndexCol As String
    
    'Check if clicked cell is in specified columns
    If Target.Column >= 1 And Target.Column <= 8 Then
         
         'Set values
        RowOffset = 0
         'Change the C to the column where you want the numbers to show
        IndexCol = "C"
        
        Intersect(ActiveCell.EntireRow, Columns(IndexCol)).Value = ActiveCell.Row + RowOffset
    
    End If
    
End Sub

相关问题