I'm very new to VBA. I used the "Find & Select" option in Excel to find cells (in column A) containing a certain text, then I used CNTRL A and CNTRL C to select all the matching cells in the column, and finally selected a cell in another workbook where I wanted to copy the selected cells. Of course it works when done manually but later, when I run the macro on a new sheet, it gets stuck. Below is what the code looks like - it seems to err at line 4 "ActiveSheet.Paste".
A few notes:
- Column A is the column containing data I'm using to find & select from
- L5 is one of the cells I select to paste cells found/selected (trying to paste other selections in I5, o5, o12, o26, o40, and o53
- "HS" in Line 11 and later "602" are two of seven search criteria used in "find & select". Interesting 5 of the search criteria I used don't show up in coding.
Line 4 Error message includes "Runtime error '1004': You can't paste this here because the Copy area and paste area aren't the same size. Select one cell in the paste area (which I think I did when building the macro) or an area that's the same size and try pasting again.
When I click "debug" in the pop-up error message box, it highlights Line 4.
Next, see Line 11. I was doing the same function and yet it says "Selection."... with a much longer description of the selection criteria. I tried moving Lines 10-15 above line 4, to test what this different coding would do. It failed as well
Error message: "Run-time error '91': Object variable or With block variable not set
Thanks for any help you all can offer! Much appreciated!
Columns("A:A").Select.
Selection.Copy
Range("L5").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Copy
Range("I5").Select
ActiveSheet.Paste
Columns("A:A").Select
Selection.Find(What:="HS", After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Application.CutCopyMode = False
Selection.Copy
Range("O5").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Copy
Range("O12").Select
ActiveSheet.Paste
Columns("A:A").Select
Range("A17").Activate
Application.CutCopyMode = False
Columns("A:A").Select
Range("A93").Activate
Selection.Copy
Range("O26").Select
ActiveSheet.Paste
Range("O26:O37").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Cut
Application.CutCopyMode = False
Selection.ClearContents
Columns("A:A").Select
Range("A21").Activate
Selection.Copy
Range("O40").Select
ActiveSheet.Paste
Selection.Find(What:="602", After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Application.CutCopyMode = False
Selection.ClearContents
Columns("A:A").Select
Range("A28").Activate
Selection.Copy
Range("O53").Select
ActiveSheet.Paste
I am at a loss as to what to try to correct the problem(s).
I am sure there is space where I am trying to paste the cells. It works very smoothly when I am doing it manually ;-) But somehow, the coding isn't picking up what I'm doing.
I see suggestions we should avoid "Select" in coding but I'm at a loss what to replace it with, especially given I'm not trying to copy a range but rather only cells in that range matching a criteria.