VBA Find-Copy and Loop Trouble

antora

New Member
Joined
May 13, 2013
Messages
5
hi guys need your help.

I have a problem in using the 'find-copy and loop' method,

here's my data:
ABCDEFG
1Name 1A1OKA2OK--
2Name 2B1OK----
3Name 3C1OKC2OKC3OK
4Name 4D1OK--D3OK
5Name 5E1OK----
6....

<tbody>
</tbody>

and this is the code I used:
Code:
Sub Copas()
  Dim DestSheet        As Worksheet
  Set DestSheet = Worksheets("Sheet2")
  
  Dim sRow       As Long
  Dim dRow       As Long
  Dim sCount     As Long
  Dim X As Long
  sCount = 0
  dRow = 1

X = 1
Do Until Cells(X, 1) = ""

  For sRow = Cells(X, 1) To Cells(X, 1000).End(xlToRight).Column
     If Cells(X, sRow) Like "*OK*" Then
        sCount = sCount + 1
        dRow = dRow + 1
        Cells(X, 1).Copy Destination:=DestSheet.Cells(dRow, "A")
        Cells(X, sRow).Copy Destination:=DestSheet.Cells(dRow, "B")
        Cells(X, sRow - 1).Copy Destination:=DestSheet.Cells(dRow, "C")
     End If
Next sRow
  
X = X + 1
Loop
End Sub

what happens is:
The code does not copy all the results that I found. whereas, based on the above data should be there around 9 results but that appears there's only 8 results. what is lacking or wrong of my code.?

thanks before.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
try:
Code:
Sub Copas()
Dim DestSheet As Worksheet
Set DestSheet = Worksheets("Sheet2")

Dim sColm As Long
Dim dRow As Long
Dim sCount As Long
Dim X As Long
sCount = 0
dRow = 1

X = 1
Do Until Cells(X, 1) = ""
    For sColm = 2 To Cells(X, Columns.Count).End(xlToLeft).Column
        If Cells(X, sColm) Like "*OK*" Then
            sCount = sCount + 1
            dRow = dRow + 1
            Cells(X, 1).Copy Destination:=DestSheet.Cells(dRow, "A")
            Cells(X, sColm).Copy Destination:=DestSheet.Cells(dRow, "B")
            Cells(X, sColm - 1).Copy Destination:=DestSheet.Cells(dRow, "C")
        End If
    Next sColm
    X = X + 1
Loop
End Sub
(I changed sRow to sColm as it seemed odd to name a variable with the word row in it when it refers to a column!)
 
Upvote 0
try:
Code:
Sub Copas()
Dim DestSheet As Worksheet
Set DestSheet = Worksheets("Sheet2")

Dim sColm As Long
Dim dRow As Long
Dim sCount As Long
Dim X As Long
sCount = 0
dRow = 1

X = 1
Do Until Cells(X, 1) = ""
    For sColm = 2 To Cells(X, Columns.Count).End(xlToLeft).Column
        If Cells(X, sColm) Like "*OK*" Then
            sCount = sCount + 1
            dRow = dRow + 1
            Cells(X, 1).Copy Destination:=DestSheet.Cells(dRow, "A")
            Cells(X, sColm).Copy Destination:=DestSheet.Cells(dRow, "B")
            Cells(X, sColm - 1).Copy Destination:=DestSheet.Cells(dRow, "C")
        End If
    Next sColm
    X = X + 1
Loop
End Sub
(I changed sRow to sColm as it seemed odd to name a variable with the word row in it when it refers to a column!)

Thanks P45cal... it works
so., what if the search is based on rows. which lines should be replaced?
 
Upvote 0
so., what if the search is based on rows. which lines should be replaced?
I don't really understand the question. Currently there's a nested loop:

Code:
Do Until Cells(X, 1) = "" 'X is used to define the row number and is incremented each time (the outside loop) 
  For sColm = 2 To Cells(X, Columns.Count).End(xlToLeft).Column 'scolm is used to define the column number and is incremented each time (the inner loop)
    'do stuff here with Cells(X,sColm)  'syntax is: Cells(row number, column number)
  Next sColm   
  X = X + 1 
Loop

The comments in the above snippet show that both rows and colums are used.
I suppose you could invert the loops and put the inside one on the outside:

Code:
For sColm = 2 To Cells(X, Columns.Count).End(xlToLeft).Column
   Do Until Cells(X, 1) = ""
     'do stuff here with Cells(X,sColm)     
     X = X + 1   
   Loop
Next sColm

Is that what you meant?
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top