I have a workbook where I need to be able to click on a single cell of a worksheet and hit my command button. That copies and pastes the cell value to the first blank cell in column E on a different worksheet within the same workbook. When I just run the macro by itself, it works fine. But when I paste the code into a command button, it gives me a couple of runtime error 1004's. The most common error is "Select method of range class failed" and refers to the code line that tells it to select Range (E4). Here is the code:
Private Sub CommandButton1_Click()
' Choose player from Player list and paste to Draft list.
Sheets("Players").Select
Selection.Select
Selection.Copy
Sheets("Draft").Select
Range("E4").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select
Selection.PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
TL;DR, couple options to resolve this, in order of preference:
Selectto access cellsRange("E4")when executing code in aWorksheetobject by usingApplication.Range("E4")orSheets("Draft").Range("E4")orActiveSheet.Range("E4")ThisWorkbookor a code module and call thatSubfrom the event.Here is the lengthy part that attempts to explain why your code does not work.
This all comes down to: where is the code executing? Different execution contexts will behave differently when you use unqualified references to
CellsRangeand a number of other functions.Your original code likely ran inside
ThisWorkbook, a code module, or possibly in the code file for sheetDraft. Why do I guess this? Because in all of those places a call toRange("E4")would be acceptable to get the cellE4on sheetDraft. Cases:ThisWorkbookand a code module will executeRangeon theActiveSheetwhich isDraftsince you just calledSelecton it.Draftwill executeRangein the context ofDraftwhich is acceptable since that is theActiveSheetand the place where you are trying to get cellE4.Now what happens when we add an ActiveX
CommandButtonto the mix? Well that code is added to theWorksheetwhere it lives. This means that the code for the button can possibly execute in a different context than it did before. The only exception to this is if the button and code are both on sheetDrafts, which I assume not since youSelectthat sheet. For demonstrations, let's say the button is located on sheetWHERE_THE_BUTTON_IS.Given that sheet, what is going on now? Your call to
Rangeis now executed in the context of sheetWHERE_THE_BUTTON_ISregardless of theActiveSheetor anything else you do outside of the call toRange. This is because the call toRangeis unqualified. That is, there is no object to provide scope to the call so it runs in the current scope which is theWorksheet.So now we have a call to
Range("E4")in sheetWHERE_THE_BUTTON_ISwhich is trying toSelectthe cell. This is forbidden because sheetDraftis theActiveSheetandSo with all of this, how do we resolve this issue? There are a couple of ways out:
Selectto manipulate cells. This gets away from the main problem here, quoted above. This assumes your button lives on the same sheet as theSelectionto copy/paste.Rangeso that it executes in the proper context and chooses the right cell. You can use theSheets("Draft").Rangeobject to qualify this orApplication.Rangeinstead of the bareRange. I highly recommend option 1 instead of trying to figure out how to makeSelectwork.Subthat is outside of theWorksheetobject and call it from theCommandButton1_Clickevent.