AllExperts > Visual Basic 
Search      
Visual Basic
Volunteer
Answers to thousands of questions
 Home · More Visual Basic Questions · Answer Library  · Encyclopedia ·
More Visual Basic Answers
Question Library

Ask a question about Visual Basic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Golfnut_1969
Expertise

Instantiating form variables instead of using Global instances.
User Defined Types
DLLs, ActiveX Exes
String Manipulation
Working with Classes
Custom User Controls
Object Oriented Programming
Database access (SQL Server, MS Access, RDO, DAO, ADO)
Use of Arrays

I do not answer questions regarding databound controls so please ask those questions of one of the other experts.

Experience
I've been working with Visual Basic 5.0 and 6.0 for over 8 years. I've worked extensively with multi-tier database business solutions created in Visual Basic. I've also been working in Visual Basic .NET for over 2 years and Visual Basic .NET 2.0 since its first BETA release. I currently work developing custom applications for a variety of industries. In addition to all the work in Visual Basic 6.0 and .NET I have also worked extensively with ASP .NET and ASP .NET 2.0.

Education/Credentials
Associate’s Degree: Computer Programming St. Louis Community College at Florissant Valley (4.0 gpa) Microsoft Certified Technology Specialist (MCTS) .NET Framework 2.0 Windows Applications

Past/Present clients
Mallinckrodt Pharmaceuticals Commerce Bank CitiMortgage Elmwood Marine Services FedEx Memco Barge Lines Crounse Corporation AG Edwards

 
   

You are here:  Experts > Computing/Technology > Basic > Visual Basic > For Loop

Visual Basic - For Loop


Expert: Golfnut_1969 - 11/24/2007

Question
I was a golfnut until rotator cuff surgery. Hope to be back playing in a month.

I have a macro I run on 46 stock symbols. I put a symbol in cell A6 and run the macro. Change symbol and do it again 46 times. The data keeps changing or I would do it 46 times and be through with it.

I would like to list all my symbols in column "A" and let my macro run down the list one by one.

Here are the first few lines of my macro.
Dim ticker As String
   ticker = Worksheets("Sheet1").Range("A6").Value
  
   Workbooks.Open Filename:="C:\MyDocuments\Stops\" + ticker + "\2007.csv"
   Windows("2007.csv").Activate

I was told I need a 'For loop'. I haven't been able to find examples of what I need.

Thank for your help, Joe

Answer
I usually don't answer VBA questions but I've got a little experience in what your trying to do so I'll give it a shot.  However, if this answer isn't what you need or you have more questions, please direct your question to an Excel Expert with VBA knowledge.

Basically, you are loading the value of A6 each time.  What you need to do is load the value of AX, where X is a value of 6 to start with and ends when X is a value of 46. This is where the For Loop comes in.

Private Sub ExecuteAll()
   Dim i as Integer 'i is a very common For Loop variable

   For i = 6 to 46
      'Call your Macro and send it the value of i
      ExecuteTicker i
   Next i


Private Sub ExecuteTicker(ByVal vnRowNumber as Integer)
   Dim sTicker As String

   
   sTicker = Worksheets("Sheet1").Range("A" & vnRowNumber).Value
 
  Workbooks.Open Filename:="C:\MyDocuments\Stops\" & sTicker & "\2007.csv"
  Windows("2007.csv").Activate

End Sub

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.