Dina-mic
asked on
VB Looping in Excel
I have an excel worksheet with 30000 records. I need to write a macro that reads a store number from sheet 1 (i.e. in cell B1) and grabs all records that correspond with that store number. For example:
1 Help
1 Help
1 Help
2 Simpson
2 Simpson
2 Simpson
3 Fred
3 Fred
In this case, if I have store 2 entered in cell B1, I need the macro to pull only those records and copy them to sheet 2. What I have so far (that doesn't work):
Sub looptest()
Dim I As Integer
I = Sheets("sheet2").Range("B1 :B1")
Sheets("Sheet1").Range("A1 :A1").Sele ct
Do While Not ActiveCell = I
Selection.Offset(1, 0).Select
Loop
Sheets("Sheet2").Select
Range("D1").Select
Sheets("Sheet1").Select
Do While ActiveCell = I
Range(ActiveCell, Selection.Offset(0, 2)).Select
Selection.Copy
Sheets("Sheet2").Select
Selection.Offset(1, 0).Select
Selection.Paste
Sheets("Sheet1").Select
Range(ActiveCell, Selection.Offset(0, 2)).Select
Selection.Offset(1, 0).Select
Loop
End Sub
PLEASE HELP!
1 Help
1 Help
1 Help
2 Simpson
2 Simpson
2 Simpson
3 Fred
3 Fred
In this case, if I have store 2 entered in cell B1, I need the macro to pull only those records and copy them to sheet 2. What I have so far (that doesn't work):
Sub looptest()
Dim I As Integer
I = Sheets("sheet2").Range("B1
Sheets("Sheet1").Range("A1
Do While Not ActiveCell = I
Selection.Offset(1, 0).Select
Loop
Sheets("Sheet2").Select
Range("D1").Select
Sheets("Sheet1").Select
Do While ActiveCell = I
Range(ActiveCell, Selection.Offset(0, 2)).Select
Selection.Copy
Sheets("Sheet2").Select
Selection.Offset(1, 0).Select
Selection.Paste
Sheets("Sheet1").Select
Range(ActiveCell, Selection.Offset(0, 2)).Select
Selection.Offset(1, 0).Select
Loop
End Sub
PLEASE HELP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That didn't work either
ASKER
THANKS EDDYKT! THAT WORKED! YOU ROCK!
Sub looptest()
Dim I As Integer
I = Sheets("sheet2").Range("B1
Sheets("Sheet1").Range("A1
Do While Not ActiveCell.Value = I
Selection.Offset(1, 0).Select
Loop
Sheets("Sheet2").Select
Range("D1").Select
Sheets("Sheet1").Select
Do While ActiveCell.Value = I
Range(ActiveCell, Selection.Offset(0, 2)).Select
Selection.Copy
Sheets("Sheet2").Select
Selection.Offset(1, 0).Select
Selection.Paste
Sheets("Sheet1").Select
Range(ActiveCell, Selection.Offset(0, 2)).Select
Selection.Offset(1, 0).Select
Loop
End Sub
as you had it coded:
Sheets("sheet2").Range("B1
AW