Macro Help - Loop through column, find a match for the left 1 or 2 digits, if match copy row beneath to row above

bblitz

New Member
Joined
Sep 14, 2010
Messages
5
I am trying to write code that does the following:

I need to loop through column E for any values that start with "DW" or "3".
If a cell starts with "DW" or "3", I would like to copy a range (K:X) in one row beneath the found cell and copy it up one row (row in which "DW" or "3" was found). I would like the paste to start at row Y.

I would like to loop through the entire column and perform the copy/paste function explained above in any instance that DW or 3 is found as the start value for the cell.

Can someone please help with the code?

Thanks, BBLITZ
 

jc.021286

Well-known Member
Joined
Apr 12, 2010
Messages
725
Hi there,
give this a quick try

Code:
Sub copythrough()
Dim a As Long
a = Cells(65000, 5).End(xlUp).Row

For i = 2 To a
If Left(Cells(i, 5), 2) = "DW" Or Left(Cells(i, 5), 1) = 3 Then
Range(Cells(i - 1, 11), Cells(i - 1, 24)).Copy Destination:=Cells(i, 25)
End If
Next i
End Sub
 

jc.021286

Well-known Member
Joined
Apr 12, 2010
Messages
725
In regards to my last post,
I may have gone in the wrong direction and the for statement can start with a 1 instead of 2 and instead of the copying using (-1) it may be +1 for the next row under.

Got confused with under being related to a greater number...

try that and see how you fare,
jc
 

prabby25101981

Active Member
Joined
Jul 28, 2010
Messages
348
Probably this -

Rich (BB code):
Sub FindandCopy()
Dim LRow As Long
LRow = Range("E65536").End(xlUp).Row
For i = 2 To LRow Step 2
    If Left(Range("E" & i).Value) = "DW" Or Left(Range("E" & i).Value) = "3" Then
        Range("K" & i + 1 & ":X" & i + 1).Copy
        Range("Y" & i).PasteSpecial (xlValues)
    End If
Next
End Sub
Change the 2 in the above code to the row number where the DATA starts...
 

bblitz

New Member
Joined
Sep 14, 2010
Messages
5
Prabby and JC

Thanks for your help. However, I got errors on trying both codes at the first Left statement

Prabby, At: If Left(Range("E" & i).Value)... I got error was "compile error: Argument not optional"

JC, At: If Left(Cells(i, 5), 2) = "DW" Or Left(Cells(i, 5), 1) = 3 Then, I got "Run Time 13 error, Type mismatch

I am not familiar with the errors or how to fix as I am new to vba code, any suggestions?

Thanks, B
 

prabby25101981

Active Member
Joined
Jul 28, 2010
Messages
348
I missed something in my code. Try this. This should work -

Rich (BB code):
Sub FindandCopy()
Dim LRow As Long
LRow = Range("E65536").End(xlUp).Row
For i = 2 To LRow Step 2
    If Left(Range("E" & i).Value, 2) = "DW" Or Left(Range("E" & i).Value) = "3" Then
        Range("K" & i + 1 & ":X" & i + 1).Copy
        Range("Y" & i).PasteSpecial (xlValues)
    End If
Next
End Sub
 

bblitz

New Member
Joined
Sep 14, 2010
Messages
5
Hi Prabby,

Again I got the same error, but now on the 2nd Left (underlined) in the statement below. The first time I ran it, I got the error on the 1st Left in the statement.

If Left(Range("E" & i).Value, 2) = "DW" Or Left(Range("E" & i).Value) = "3" Then

Any further suggestions?

Thanks, Bblitz
 

prabby25101981

Active Member
Joined
Jul 28, 2010
Messages
348
Again I made the same mistake ! Not my day I suppose... Try this -

Rich (BB code):
Sub FindandCopy()
Dim LRow As Long
LRow = Range("E65536").End(xlUp).Row
For i = 2 To LRow Step 2
    If Left(Range("E" & i).Value, 2) = "DW" Or _
           Left(Range("E" & i).Value, 1) = "3" Then
        Range("K" & i + 1 & ":X" & i + 1).Copy
        Range("Y" & i).PasteSpecial (xlValues)
    End If
Next
End Sub
 

bblitz

New Member
Joined
Sep 14, 2010
Messages
5
Thanks! This works great. I also had a problem in which I have #value! in the column so i cleaned that up.

Thanks, Betsy
 

Forum statistics

Threads
1,082,323
Messages
5,364,579
Members
400,809
Latest member
formulasataglance

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top