Looping in Macro for 3 values to place in another worksheet

DThib

Active Member
Joined
Mar 19, 2010
Messages
464
Office Version
  1. 365
Platform
  1. Windows
Hello,

Latest quandary. I need to set 3 conditions for a row of information to be chosen to have 8 columns copied and placed in another worksheet.
This should pull only those records that match these three conditions from a worksheet with ~30,000 rows.

Can anyone help me figure out how to set up 3 constraints before copying appropriate cells?
I haven't placed the cell result pull yet.

Code:
Private Sub WorkaSet()

Dim Rws As Long
Dim Rng As Range
Dim ws, sh As Worksheet
Dim c, a, b As Range
Dim x As Integer


    Set ws = Sheets("Workable")  'specify sheet name here to paste to
    x = 3   'begins pasting in Sheet RFQ on row 2
Application.ScreenUpdating = False


    Set sh = Sheets("Initial Query Pull")
    Set a = sh.Range("$K:$K")
    Set b = sh.Range("$Y:$Y")
            With sh
                For Each c In .Range("$AF:$AF")
                    If (c.Value <> "") And (a.Value = "Assigned") And (b.Value = "") Then
                       'If b.Value = "" Then
                      'searches for "FI ready" cells And (C1.Value = "Assigned")
                        c.EntireRow.Copy
                        ws.Range("B" & x).PasteSpecial Paste:=xlValues
                         
                        x = x + 1


                    End If
                Next c
            End With
    
ws.Range("A1").Select
Application.ScreenUpdating = True


End Sub

DThib
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi Dthib,

I have not yet tried to code your requirements. However this is just as an FYI for you. As I read your code, I can see what you want your variables to be declared as. If you look at your declarations in Red:

Code:
Dim Rws As Long
Dim Rng As Range
Dim [COLOR=#ff0000]ws,[/COLOR] sh As Worksheet
Dim [COLOR=#ff0000]c, a[/COLOR], b As Range
Dim x As Integer

What you have done is declared the variables ws, c, a as variants. Your declaration should really read as follows:

Code:
Dim Rws As Long
Dim Rng As Range
Dim ws As Worksheet, sh As Worksheet
Dim c As Range, a As Range, b As Range
Dim x As Integer

Will it affect the outcome of the code- probably not. I just thought you might like the heads-up.
 
Upvote 0
Where does it search for "FI ready" (what column) and what happen if it is found or not found...

Code:
   'If b.Value = "" Then
                      'searches for "FI ready" cells And (C1.Value = "Assigned")
 
Upvote 0
Hi and Thanks

The c is getting the first requirement from AF column in the donor worksheet. Then it will need to make sure the other two cells in the row match to pull the cells from that row.

does that help?
 
Upvote 0
Why not autofilter for the three criteria & then copy the visible rows?
 
Upvote 0
Hi and Thanks

The c is getting the first requirement from AF column in the donor worksheet. Then it will need to make sure the other two cells in the row match to pull the cells from that row.

does that help?

Does not answer my question. I still don't know where the code is suppose to find the text "FI ready"

Additionally, Fluff has provided good logic to solve your requirements.
 
Upvote 0
This is the first of a trio of UserForms to pull data for three escalating meetings. The data is a download from a database query that gets refreshed with each level. This is the part that is giving me trouble.
The FI Ready is a note to myself.

Doing this manually would defeat the purpose.

Deborah
 
Last edited:
Upvote 0
Which part of this do you see as being done manually. Using AutoFilter and copying the visible rows is all done through VBA.
 
Upvote 0
Thanks for you patience, I'll post more later.
 
Upvote 0
Without seeing your data, I do not know where it starts and ends. You really don't want the code to search the entire one million plus rows. You want to pick a column that will always be have data on the last row of your data set. I arbitrarily used column AF. Perhaps you could modify something like this to fill your particular requirements. I am assuming row 1 is a header row.

Code:
Sub test()


    Dim lRow As Long
    Dim ws As Worksheet, sh As Worksheet


    Application.ScreenUpdating = False
    Set sh = Sheets("Initial Query Pull")
    Set ws = Sheets("Workable")  'specify sheet name here to paste to
    lRow = sh.Cells(Rows.Count, "AF").End(xlUp).Row ' pick the column that will always have the last row of data
    
    With sh
        .Range("K1:AF" & lRow).AutoFilter
        .Range("K1:AF" & lRow).AutoFilter Field:=1, Criteria1:="Assigned"
        .Range("K1:AF" & lRow).AutoFilter Field:=15, Criteria1:=""
        .Range("K1:AF" & lRow).AutoFilter Field:=22, Criteria1:=""
    End With


    sh.Range("K1:AF" & lRow).Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
    ws.Range("B3").PasteSpecial Paste:=xlValues
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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