Consulting

Results 1 to 20 of 20

Thread: VBA to force paste special

  1. #1

    VBA to force paste special

    dear colleagues,
    i created a data collection sheet with loads of conditional formattings in order to support the users and to save time for feedback loops. the problem is that copied values overwrite conditional formattings and thus make them useless. i found a script online with which i can force to paste only values by using strg+v and right mouse button + paste. but unfortunately there are a few paste options which are not affected by the script and could cause trouble. the relevant rows in the code are:

    Excel.Application.OnKey "^v", m_sPasteProcedure_c
    Excel.Application.OnKey "+{INSERT}", m_sPasteProcedure_c

    do you have an idea, how i could force paste value for every paste option?

    thanks for your help!

    best
    daniel

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    to 'copy' only values:

    Sub M_snb()
      sheet2.range("A5")=sheet1.range("A2").value
    End Sub

  4. #4
    hey,
    thanks for the links. the solution of the second one (EDIT: I mean the solution from Siddharth Rout) seems to fit for my problem, but I always get an error saying "Argument not optional". It seems like I did not declare one or more variables, but I couldn't see it. Here the code:

    Option Explicit
     
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim UndoList As String
     
        Application.ScreenUpdating = False
        Application.EnableEvents = False
     
        On Error GoTo Whoa
     
        '~~> Get the undo List to capture the last action performed by user
        UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
     
        '~~> Check if the last action was not a paste nor an autofill
        If Left(UndoList, 5) <> "Paste" And UndoList <> "Auto Fill" _
        Then GoTo LetsContinue
     
        '~~> Undo the paste that the user did but we are not clearing
        '~~> the clipboard so the copied data is still in memory
        Application.Undo
     
        If UndoList = "Auto Fill" Then Selection.Copy
     
        '~~> Do a pastespecial to preserve formats
        On Error Resume Next
        '~~> Handle text data copied from a website
        Target.Select
        ActiveSheet.PasteSpecial Format:="Text", _
        Link:=False, DisplayAsIcon:=False
     
        Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        On Error GoTo 0
     
        '~~> Retain selection of the pasted data
        Union(Target, Selection).Select
     
    LetsContinue:
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Exit Sub
    Whoa:
        MsgBox Err.Description
        Resume LetsContinue
    End Sub
    Any ideas where the problem is?

  5. #5
    You overlooked the simplest solution in #3
    Last edited by snb; 08-18-2014 at 05:15 AM.

  6. #6
    You mean your solution #5 in the link? Which gives me the same error, meaning that I have a general knowledge problem...

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    @snb

    the users sometimes create miracles when filling in pre-formatted forms, tables, etc. they simply open a blank sheet, create their own table and directly paste it to formatted area.

    the OP wants to disable all paste options the users can use and, it seems, chooses an undo operation.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    it worked for me...
    can you upload a workbook with sample data and formatting.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    all the users must "enable" macros as well.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  10. #10
    I bet the code is working, but the way I use it is the problem. I created an empty workbook with several sheets for a data collection. To guide the user and to reduce the workload, the file includes a lot of conditional formattings. Now I want to force pasting values only with the code of Siddharth Rout above (or any other working code). I copied the code in a vba module in excel, but when I run it I get the error "Argument not optional". Did I forget to determine a variable in the code above?

  11. #11
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    since it's a workbook "event" code, copy-paste the code to ThisWorkbook code module.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  12. #12
    Thanks. The code is not working for me though, since it gives a warning message every time I do anything (copy, write, paste), but it overwrites my formats anyway. Shouldn`t the code prevent my formats?

  13. #13
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    Quote Originally Posted by mancubus View Post
    it worked for me...
    can you upload a workbook with sample data and formatting.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  14. #14
    I referred to the wrong post:

    You overlooked the simplest solution in #3 ?

    @macubus

    If you use a userform and keep all the worksheet that matter very hidden, and if closing the userform will close the werkbook automatically too....
    The last thing I would do is change the UI.
    Last edited by snb; 08-18-2014 at 06:37 AM.

  15. #15

    testfile

    please find a testfile attached. this is a very small part of a huge file for data collection, where user have to insert their data, some of them mandatory, some of them if needed. the mandatory cells have conditional formattings, which should not be overwritten.
    thanks for all your efforts!
    Attached Files Attached Files

  16. #16
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    if i was assigned this task when i had very little experience of excel and VBA, i would use a protected sheet, where data validation is the only way to enter data. (tx God, i have a bit more experience today )


    @danielw7
    snb's last suggestion sounds good. but i dont know if you ever tried to customize the ribbon.
    we have a dedicated forum here:
    http://www.vbaexpress.com/forum/foru...2007-Ribbon-UI
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  17. #17
    Thanks for having a look at the file. To get more knowledge though: do you know where the problem is in the file?

  18. #18
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    i was writing my message after snb's post and didn't see your file.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  19. #19
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    it also works for your file.

    when i copy paste from an unformatted cell, the cell in column G keeps its formatting.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  20. #20
    I'd suggest to study the CF implementation first.

    in cell Q4

    The CF rule that will format the cell as yellow should read as:

    =($A4<>"")*(G4="")
    the use of IF(condition,1,0) in CF rules is redundant.

    I'd prefer to make an inventory of CF rules.
    When closing the worksheet (or at any other desired moment) the CF rules can be re-implemented.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •