Results 1 to 3 of 3

Thread: Cant copy/paste from workbook when using shortcut/hot key

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    2

    Question Cant copy/paste from workbook when using shortcut/hot key

    I have encountered a very strange problem. I have some code that opens a second workbook, copies a sheet from the second workbook into the first workbook (where the VBA is), then closes the second workbook. It works fine if I run it from the code window, or by using View Macros. However, if I try to run it using an assigned Shortcut/Hot Key (CTRL+J or CTRK+M), it will open the second workbook then hang. It won't do the copy or close the workbook. I have tried 4 different ways of doing the copy/paste (cells.select, selection.copy, ActiveSheet.copy, and the following code). All of them behave the same way - they work but not with the shortcut/hot key

    SourceFile = “Filename.csv”

    Set SourceBook = Workbooks.Open(SourceFile)
    SourceBook.Sheets(1).Copy After:=ThisWorkbook.Sheets(1)
    SourceBook.Close SaveChanges:=False

    Why is there a difference when running this from a shortcut/hot key???

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    2

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Cant copy/paste from workbook when using shortcut/hot key

    i pasted your code into a procedure and assigned a hotkey ctrl+m, the code worked as expected either using the hot key or from the ide

    did you try specifying the full path to the file?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width