VBA Code to Loop through Data and Copy if it does not equal one of the following

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any suggestions as I will post feedback for them.

I am trying to loop through Column A and when the cell does not equal one of the listed items, I would like to copy a section of that data. Here is my code thus far.

Code:
Sub Macro(1)
 
'Dimensioning
          Dim LastRow As Long
          Dim LRPHDW As Long
          Dim StartRow As Long
          Dim i As Long
 
'Find the Last Row of all the data
              LastRow = Cells.Find(What:="*", after:=Range("A1"), LookAt:=xlPart, _
              LookIn:=xlFormulas, SearchOrder:=xlByRows, _
              SearchDirection:=xlPrevious, MatchCase:=False).Row
 
'Find the Last Row of this section of data which will be the same length for all _
sections of data that will be copied
              LRPHDW = Range("A5").End(xlDown).Offset(1).Row
              LRPHDW = LRPHDW - 2
 
'Loop through Column A to copy the appropriate sections
 
              For i = 1 to LastRow
 
               'I want to loop through column A starting with cell "A1" through "A"&LastRow._
               Once the cell in Column A does not equal one of the following, I would like _
               copy the data two rows below it going from column B through Column P through _
               LRPHDW _
                          Blank cell _
                          Left 4 characters will be equal to "Date" _
                           "Rem." _
                          Have a date in it with the format "m/d/yyyy" _
             
              'Copy the data
 
                         'Copy tab "SheetA" after tab "SheetB" and paste the copied data _
                         starting in cell B11
 
End Sub
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
two thing are not clear.
1. Is your data separated by blank rows (including no formulas in the row)
2. Do you want to add a sheet to paste to, or is there a sheet C that currently exists that you want to paste to?
 
Upvote 0
Thanks JLGWhiz!

Responses are as follows:

Question 1. Is your data separated by blank rows (including no formulas in the row)
Response: Not entirely, but column A which I am looping through has blank cells between each set of data.

Question 2. Do you want to add a sheet to paste to, or is there a sheet C that currently exists that you want to paste to?
Response: There is an existing sheet "SheetA" which I would like to make a copy of and place it after "SheetB." We can call that "SheetC" and then paste the copied date into it.

Hope this clarifies and thanks so much for your help.
 
Upvote 0
If your data is separated by blank rows and there is a SheetC in existing, then this code might do what you want.

Code:
Sub t()
Dim rng As Range
With Sheets("SheetA").UsedRange
    For Each rng In .SpecialCells(xlCellTypeConstants).Areas
        If Sheets("SheetC").Range("B11") = "" Then
            rng.Offset(, 1).Copy Sheets("SheetC").Range("B11")
        Else
            rng.Offset(, 1).Copy Sheets("SheetC").Cells(Rows.Count, 2).End(xlUp)(2)
        End If
    Next
End With
End Sub
 
Last edited:
Upvote 0
I am confused about where you are copying from and what you want ot paste to. To save time let's get the sheet situation cleared up.

1. What is the sheet name you copy from?
2. If you are copying from sheet A, then why not just copy the sheet and delete what you do not want?
3. If you are not copying from sheet A and you want to make a copy of it to paste the copied data to, then do you want to delete any existing data from the copy, overwrite existing data or append at the bottom of existing data?
 
Upvote 0
Guesing at the answers. Here is what I came up with.

Code:
Sub t2()
Dim c As Range, sh As Worksheet
Set sh = ActiveSheet
Sheets("SheetA").Copy After:=Sheets("SheetB")
ActiveSheet.Name = "SheetC"
ActiveSheet.UsedRange.Offset(4).ClearContents
    With sh
        For Each c In .Range("A5", .Cells(Rows.Count, 1).End(xlUp))
            If c <> "" And c <> "Date" And c <> "Rem." And Not IsDate(c) Then
                If Sheets("SheetC").Range("B11") = "" Then
                    c.Offset(, 1).Resize(, 15).Copy Sheets("SheetC").Range("B11")
                Else
                    c.Offset(, 1).Resize(, 15).Copy Sheets("SheetC").Cells(Rows.Count, 2).End(xlUp)(2)
                End If
            End If
        Next
    End With
End Sub
 
Upvote 0
JLGWhiz Thanks so much for your hard work!

I tested it and it did not work most probably because of my lack of clarity in my request. Now the good news is I think I can use this to modify for my request. I will piece something together and let you know the outcome.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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