?
Solved

VB Looping in Excel

Posted on 2003-03-13
4
Medium Priority
?
177 Views
Last Modified: 2010-05-01
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").Select
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!
0
Comment
Question by:Dina-mic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8128941
try this:

Sub looptest()
Dim I As Integer
I = Sheets("sheet2").Range("B1:B1").Value
Sheets("Sheet1").Range("A1:A1").Select
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:B1")   returns a Range Object, not the Value if the Cell IN THAT RANGE.  To get the VALUE of the selected RAANGE (cell in your case), you need to retrieve the .Value Property of the Range object (which returns the Value in the UPPER LEFT Cell, if there are more than 1 cell(s) in the Range).

AW
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 80 total points
ID: 8129050
Try this


Sub looptest()
Dim I As Integer
I = Sheets("sheet2").Range("B1:B1")
Sheets("sheet1").Select
Sheets("Sheet1").Range("A1:A1").Select
Do While Not ActiveCell = I
   Selection.Offset(1, 0).Select
Loop
Sheets("Sheet2").Select
Range("D1").Select
Sheets("Sheet1").Select
Range(ActiveCell, Selection.Offset(0, 2)).Select
Do While ActiveCell = I
   Selection.Copy
   Sheets("Sheet2").Select
   Selection.Offset(1, 0).Select
   ActiveSheet.Paste
   Sheets("Sheet1").Select
   Selection.Offset(1, 0).Select
Loop
End Sub
0
 

Author Comment

by:Dina-mic
ID: 8129298
That didn't work either
0
 

Author Comment

by:Dina-mic
ID: 8129335
THANKS EDDYKT!  THAT WORKED!  YOU ROCK!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question