Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Clipboard clearing itself after sub call inside button event

$
0
0

So, I have a worksheet that contains a table, and the worksheet is protected, so to allow users to paste data from the clipboard into the table I have a button that calls a sub which unprotects then pastes the data into the table then protects the sheet again.

Now, the code works fine if I link it to a keyboard shortcut, or if I place the code into the button click procedure, but I would much prefer to only have calls to subs in all the "Sheet" code and all my actual code in modules, but it seems that when the event is fired and then my sub is called I lose clipboard data. Is there a way around this? I want to have it structured as below and have it work on button click and on shortcut combo, but it will only work for the button click if I place the code from PasteToTable() into btnPasteData_Click()

Private Sub btnPasteData_Click()
    Call PasteToTable
End Sub

Sub PasteToTable()

    Call Init

    dataSheet.Unprotect

    Dim Data As MSForms.DataObject
    Set Data = New MSForms.DataObject
    Data.GetFromClipboard

    On Error GoTo failed
    Range("A3").PasteSpecial

    Dim tableData As Range
    Set tableData = table.DataBodyRange
    tableData.Locked = False

    dataSheet.Protect

    Exit Sub

failed:

    If Not tableData Is Nothing Then
        tableData.Locked = False
    Else
        Range("A3", "B3").Locked = False
    End If

    dataSheet.Protect

    MsgBox "There is no data in the clipboard, please copy the required data"& _
            " to the clipboard before trying again.", vbCritical, "Data required!"

Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>