无法让用户表单按钮代码正常工作

时间:2011-10-19 20:33:24

标签: excel vba

有一张包含两张纸的工作簿。第一个是数据所在的位置,第二个是“校正”页面。此工作簿将发送给要审核它的用户,并注意不一致/不一致。现在,它设置为通过双击突出显示单元格,然后将活动单元格转发到同一行末尾的单元格。事实证明人们需要更多的评论空间,所以我决定使用第二张作为评论表。除了“提交”按钮,我已经完成了用户表单和所有操作。当用户双击时,单元格仍然会突出显示,但不会转发到行尾,而是打开用户表单以进行注释。我正在尝试让提交按钮做两件事: 首先,我希望它将突出显示的单元格的行#放入第一列;第二,我想要用户放入教科书中放入第二栏的内容。

我可以让它在文本框的第一行输入一个值,但我不知道从哪一行开始#(可能是ActiveCell.Row?);另外,如果第一行已经有评论,我不知道怎么去设置它下移到下一行(需要一个排+1的东西我猜?这只是这个最后一个按钮让我放慢了速度完成其余部分,但我可以在userform编码的这一部分使用一些建议。谢谢!

2 个答案:

答案 0 :(得分:1)

以下是我的表现(草稿):

Private Sub Worksheet_Beforedoubleclick(ByVal Target As Range, Cancel As Boolean)

    Const CLR_INDX As Integer = 6

    If Target.Interior.ColorIndex = xlNone Then 'If cell is clear

        With frmCorrections
            Set .CellRange = Target
            .HiliteColorIndex = CLR_INDX
            .Show
        End With

    'Or Else if cell is already yellow
    ElseIf Target.Interior.ColorIndex = CLR_INDX Then
        Target.Interior.ColorIndex = xlNone 'Then clear the background
    End If

    Cancel = True

End Sub

和用户表单代码:

Dim m_rng As Range
Dim m_index As Integer

Public Property Set CellRange(rng As Range)
    Set m_rng = rng
End Property

Public Property Let HiliteColorIndex(indx As Integer)
    m_index = indx
End Property

Private Sub cmdCancel_Click()
    Me.Hide
End Sub

Private Sub cmdOK_Click()

    Dim cmt As String, NextCell As Range

    cmt = Me.txtComment.Text
    If Len(cmt) > 0 Then
        Set NextCell = ThisWorkbook.Sheets("Corrections").Cells( _
                        Rows.Count, 1).End(xlUp).Offset(1, 0)
        With NextCell
            .Parent.Hyperlinks.Add Anchor:=NextCell, Address:="", _
               SubAddress:=m_rng.Address(False, False, , True), _
               TextToDisplay:=m_rng.Address(False, False)
            .Offset(0, 1).Value = cmt
        End With
        m_rng.Interior.ColorIndex = m_index
    End If
    Me.Hide

End Sub

Private Sub UserForm_Activate()
    Me.txtComment.Text = ""
    Me.lblHeader.Caption = "Enter comment for cell: " & _
                          m_rng.Address(False, False)
End Sub

答案 1 :(得分:0)

编辑:

这是我最终想出来的,让它以我想要的方式运作。在第一个工作表上,用户可以双击单元格,然后突出显示单元格并提示用户表单。如果用户取消,则删除突出显示并且用户可以继续工作;如果他们在框中输入任何内容并提交,则单元格地址放在“评论”页面的一行中,文本在与原始单元格地址对应的行中的一列中输入,这样我就可以看到修正的位置和内容他们的理由是。无论如何,代码如下。 我使用以下内容突出显示并调用表单:

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

    Application.EnableEvents = False

    Dim TargRow As Variant
    Dim TargCol As Variant

    TargRow = Target.Row
    TargCol = Target.Column

    Header = 8
    FirstCol = 0
    LastCol = 13
    CommentCol = 13

    If TargRow > Header And TargCol > FirstCol And TargCol < LastCol Then
        'If the cell is clear
        If Target.Interior.ColorIndex = xlNone Then
                Cancel = True

            'Then change the background to yellow
            Target.Interior.ColorIndex = 6
            Corrections.Show

            'Else if the cell background color is already yellow
            ElseIf Target.Interior.ColorIndex = 6 Then

            'Then clear the background
            Target.Interior.ColorIndex = xlNone
        End If
    End If

    'This is to prevent the cell from being edited when double-clicked
    Cancel = True

    Application.EnableEvents = True
End Sub

我将此用于用户表单:

Private Sub UserForm_Initialize()
    TextBox.Value = ""
End Sub

Private Sub CommandButton2_Click()

    Unload Corrections

    ActiveCell.Interior.ColorIndex = xlNone

End Sub

Private Sub CommandButton1_Click()

    Dim PrevCell As Range

    Set PrevCell = ActiveCell

    ActiveWorkbook.Sheets("Comments").Activate
    Range("A6").Select

    Do

    If IsEmpty(ActiveCell) = False Then

        ActiveCell.Offset(1, 0).Select

    End If

    Loop Until IsEmpty(ActiveCell) = True
        ActiveCell.Value = PrevCell.Address
        ActiveCell.Offset(0, 1) = TextBox.Value

    Unload Corrections

    ActiveWorkbook.Sheets("DataPage").Activate

End Sub