首页 > 解决方案 > 自动调用 Sub

问题描述

当用户在名为“Schema”的表内单击时,我想运行一个子程序。我正在尝试以下操作,但我在表格内单击,没有任何反应......

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    'If not in the table, exit sub
    If Intersect(Range("Schema"), ActiveCell.EntireRow) Is Nothing Then Exit Sub

    Call MarkRow

End Sub

MarkRow 是这样的:

Sub MarkRow()

    Dim cellno As String: cellno = Str(ActiveCell.row)

    Dim myRowPos As Long
    Dim myRow As Range
    myRowPos = Selection.ListObject.Range.row
    Set myRow = ActiveCell.EntireRow 'I want to select the row in the table ONLY

    'Marking that row in yellow
    Range("Schema").Interior.ColorIndex = 0
    Application.ScreenUpdating = False
    Range("B" & Trim(cellno) & ":I" & Trim(cellno)).Select
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .Color = RGB(0, 255, 0)
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Application.ScreenUpdating = True

    'Show above
    If Not myRow Is Nothing And myRowPos >= 9 Then
        Range("EditCountry").Value2 = ThisWorkbook.ActiveSheet.Range("B" & Trim(cellno)).Value2
        Range("EditNodeName").Value2 = ThisWorkbook.ActiveSheet.Range("C" & Trim(cellno)).Value2
        Range("EditNodeId").Value = ThisWorkbook.ActiveSheet.Range("D" & Trim(cellno)).Value2
        Range("EditParentNode").Value = ThisWorkbook.ActiveSheet.Range("E" & Trim(cellno)).Value2
        Range("EditParentNodeId").Value = ThisWorkbook.ActiveSheet.Range("F" & Trim(cellno)).Value2
        Range("EditActive").Value = ThisWorkbook.ActiveSheet.Range("G" & Trim(cellno)).Value2
        Range("EditFrom").Value = ThisWorkbook.ActiveSheet.Range("H" & Trim(cellno)).Value2
        Range("EditTo").Value = ThisWorkbook.ActiveSheet.Range("I" & Trim(cellno)).Value2
    End If

    'Save row in the table with the modified data when clicking a button

End Sub

代码在同一个模块中......不明白为什么什么都不做。当我用按钮调用 MarkRow 时,它就可以工作了。任何想法?提前致谢!

标签: excelvba

解决方案


推荐阅读