473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Help! How to do a basic loop in VB?

I have worked extensively with Pivot Tables, etc in Excel (97version). But, the closest I have gotten to VB is using the "record" feature.

I now have a neat little Macro that performs several steps. But I need to be able to loop it until all the "assignments" have been completed. I presume there is some simple feature to call up the Macro that I now have, and execute it until all the assignments have been made, stopping when the "To be Assigned" is zero?

If someone could write me out a simple command to launch, and stop my Macro it would be greatly appreciated.

Thanks for any help anyone can give me.
Aug 6 '06 #1
6 25398
sashi
1,754 Expert 1GB
Hi there,

pls refer to below sample code segment.. take care..

for loop..
Expand|Select|Wrap|Line Numbers
  1. For index=start to end[Step step]
  2. [statements]
  3. [Exit For]
  4. [statements]
  5. Next[index] 
  6.  
example usage of for loop
Expand|Select|Wrap|Line Numbers
  1. Dim d As Integer
  2. For d = 0 To 2
  3. System.Console.WriteLine("In the For Loop")
  4. Next d
  5.  
while loop..
Expand|Select|Wrap|Line Numbers
  1. While condition
  2. [statements]
  3. End While 
  4.  
example usage of while loop..
Expand|Select|Wrap|Line Numbers
  1. Dim d, e As Integer
  2. d = 0
  3. e = 6
  4. While e > 4
  5. e -= 1
  6. d += 1
  7. End While
  8. Debug.Print("The Loop ran " & e & "times")
  9.  
do loop..
Expand|Select|Wrap|Line Numbers
  1. Do[{while | Until} condition]
  2. [statements]
  3. [Exit Do]
  4. [statements]
  5. Loop 
  6.  
example usage of do loop..
Expand|Select|Wrap|Line Numbers
  1. Dim str As String
  2. Do Until str = "Cool"
  3. System.Console.WriteLine("What to do?")
  4. str = System.Console.ReadLine()
  5. Loop
  6.  
Aug 7 '06 #2
I found similar code in a book yesterday, but I have failed at the posting. What I am trying to do is to have the Macro stop when the value in a cell reaches zero. That is, there is no one left to assign a dining spot to. I think I need to know how to reference the cell value, if it possible to do so from the Macro. Here is what I have tried, and got a syntax error message.

Sub Macro1_test_DoLoop()
'
' Macro1 Macro
' Macro recorded 8/6/2006
'
' The following is a "Recorded" Macro, which does work independently.
'
Application.Run "'NC - Table Assignments Calculations.xls'!Assign_Diners"
'
' The following cell will be at zero when the required cycles have been run.
'
x = [NC - Table Assignments Calculations.xls]Table Compute!$R$8
Do Until x = 0
Loop
Msg Box x
End
End Sub

If anyone can offer a suggestion it would be appreciated. I am totally new at this, so I have no idea what is supposed to come first, or if I can even do it the way I am trying. Thanks.
Aug 8 '06 #3
Thank you sashi for the code!
Aug 8 '06 #4
BSOB
77
'i believe this is what you want.
'<cell value> is your "value in a cell"

do
if <cell value> = 0 then exit do
'<your macro loops here>
loop

it will go round and round until cell value = 0
also (more efficeintly) writen as:

do until <cell value> = 0
'<your macro loops here>
loop
Aug 9 '06 #5
'i believe this is what you want.
'<cell value> is your "value in a cell"

do
if <cell value> = 0 then exit do
'<your macro loops here>
loop

it will go round and round until cell value = 0
also (more efficeintly) writen as:

do until <cell value> = 0
'<your macro loops here>
loop
Thank you BSOB.

I have tried creating a simple exercise to test the Loop. The loop should stop when the cell value reaches zero. When I run the Macro, the steps work, but the "do until" does not recognize the "zero" when it is actually computed. Therefore, I have to shut the operation down. Amazingly though, if I hardcode the zero, the loop stops as desired. The "test" code follows. Maybe someone will have an idea. Thanks.
-----------------------------------------------------------------------------

Sub Test_Loop()
'
' Test_Loop Macro
' Macro recorded 8/8/2006
'

' Go to ActiveCell.Value which should become zero:
Sheets("Control Sheet").Select
Range("F2").Select
' Do Until test:
Do Until ActiveCell.Value = 0
ActiveCell.Select
' Now to execute Macro:
Sheets("Piv-Summary").Select
ActiveSheet.PivotTables("PivotTable2").PivotSelect "", xlDataOnly
ActiveSheet.PivotTables("PivotTable2").RefreshTabl e
Sheets("Pivot-detail").Select
Rows("3:3").Select
Selection.Copy
Sheets("Assigned").Select
Rows("100:100").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
' Sort
Columns("A: D").Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
' position cursor:
Range("c2").Select
' End of recorded Macro
Loop
End Sub
Aug 9 '06 #6
I finally got it to work by going back to the "ActiveCell" at the end of the Macro as well as at the start. Maybe there is a better way, but it worked for me.

Thanks to all you who offered suggestions.
Aug 13 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.