0

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:

  1. Column A is the column containing data I'm using to find & select from
  2. 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
  3. "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.

braX
  • 11,506
  • 5
  • 20
  • 33
Gina
  • 1
  • Welcome to SO. I recommend reading [How to avoid using select](https://stackoverflow.com/a/23913882/16578424) and [How to avoid copy/paste](https://stackoverflow.com/a/64611707/16578424) - you will understand "handwritten" VBA better - Maybe that helps already to fix your issues. – Ike Jan 24 '23 at 07:03
  • Use Autofilter. It will be must faster than looping. See [THIS](https://stackoverflow.com/questions/11631363/how-to-copy-a-line-in-excel-using-a-specific-word-and-pasting-to-another-excel-s) example. Here I am copying the rows. You can copy the filtered values. – Siddharth Rout Jan 24 '23 at 08:00

1 Answers1

0

Two or three things (beside what Ike is recommending you in the comments, recommendation that should be considered):

  • What gives you an error in line 4 is that you are trying to paste an entire column in a space smaller. It would only work if you paste it in the first cell of the column. I'm sure if you delete that row, it will give you the same error in other similar "activesheet.paste".
  • It's better to stop using the "range" method for referring to only one cell. Some methods only work using "cells(2, 3)" for cell C2, for example. It's easier to use variables as well with this.
  • Instead of using the "find" method, try using a combination of a for loop and an "if statement".

Here some examples:

'For declaring variables
Dim I as long
Dim Ro as long
Dim Tex as string
'For finding the last cell with content in column A
Ro = Cells(Rows.Count, 1).End(xlUp).Row
'This declares the text you are looking in the cells value
Tex = "HS"
'Now the loop, that will go from I = whatever value you want to the value limit you give, which in this case will be the last row, "Ro"
For I = 1 to Ro
  'Here the "if" conditional, that will do something as long as the condition is met
  if InStr(cells(I, 1), Tex) > 0 then 'InStr is a method that gives you the position of a text in the value of a cell, resulting in 0 if can't be found in said value (in this case, the cell in column A and every row from 1 to I)
    Cells(I, 12) = cells(I, 1)
  end if
next I

And finally, you can repeat this code for "602", or just add it to the "if" statement adding more string variables:

Dim Tex2 as string
Tex2 = "602"

and then...

if InStr(cells(I, 1), Tex) > 0 or InStr(cells(I, 1), Tex2) > 0 then

Let me know if it works

MikeWasos
  • 68
  • 7
  • A couple of comments, Mike. You've commented your code to help OP understand what is happening - which is admirable - it's always nice to teach rather than just answer. However, there are a few slight issues. 1. Its always best to fully qualify ranges, yours are not. 2. Your variable names are slightly confusing - so much so I believe you've confused yourself in your `Instr` test looking for `Ro` not `Loo`. 3. Instr does not *count* the number of times the text can be found, but returns the *position* of the text. – CLR Jan 24 '23 at 08:15
  • Totally right. I feel kinda stupid... I guess I do this myself to learn as well. I'll edit my answer then. Many thanks for your corrections. – MikeWasos Jan 24 '23 at 08:45