Exit Or Skip Loop
Jul 10, 2007
I have a For Each...Next loop to read and write data from one workbook to another. Now, it's possible but unlikely that a certain "wellindex" (as defined by the Column A value) occurs more than once in the source spreadsheet, and only the most recent value should be used. And now to the actual question. Currently my macro runs a check to see if the current cell's wellindex has already been used, and if so, then it warns the user of potential double-entry. What I'd like to have it do is have it check whether the current cell's wellindex has duplicates further down (e.g., using a "findnext" method), and if so, then skip to the next cell in the For...next loop. I know that I can exit a loop with "Exit For" but I don't know how to have it go straight to "Next c" without embedding everything in an If statement.
Sub DataImport()
'Define variables
Dim sourcedata, sourcename, originname, sourcedate As String 'filename variables
Dim wellindex, ch4, co2, o2, bal, adj, com As String 'data variables
Dim cor, owp, owp2 As String, overwrite As Integer 'prompting variables
overwrite = 0
'Set up the data source
originname = ActiveWorkbook.Name
sourcedata = Application. GetOpenFilename("Data Output Files (*.csv), *.csv", , "Open the source file").........................
View 5 Replies
ADVERTISEMENT
Nov 27, 2009
My code is meant to ensure a string in a cell begins with three letters and ends with 5 numbers. It seems to work, but how can I exit the loop and go to the message box once the boolean variable is set to true? I was hoping to avoid using labels. Also, I would appreciate any suggestions in compacting the code if possible, but without using CreateObjects".
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("h17")) Is Nothing Then Exit Sub
Dim Ls As String
Dim i As Integer, z As Integer
Dim MyString As String
Dim MyClVal As String: MyClVal = Target.Value
Dim MyBL As Boolean: MyBL = False
View 9 Replies
View Related
Mar 23, 2008
I have a table named INPUT and a unique list named MODELLIST. I am doing a find function for each model from the modellist to the table input. I am using a autofilter to sort data and find to collect column data. Some models from the modellist may not be present in the table input. how do I check first if there is any data and exit loop if true. The following is part of the code that I am using and the error that is displayed is NEXT without FOR
Dim rFoundIt As Range
Dim iLoop As Integer
For Each model In Worksheets("sheet2").Range("modellist")
Worksheets("input").Activate
Selection.AutoFilter Field:=3, Criteria1:=model
With Sheets("input").Range("input")
Set rFoundIt = .Cells(1, 1)
For iLoop = 1 To WorksheetFunction. CountIf _
(Sheets("input").Range("input"), model)...............
View 2 Replies
View Related
Dec 15, 2011
So say you have a loop running a large number (on the order of 1 to 10 billion loops) and you want to exit after some amount of time x, what is the most efficient way to do this?
doing something like:
Code:
stTime=timer
do while timer-stTime < x
*do stuff*
loop
is slow...
i also could approximate the time per loop and only check when the counter passes that general area...but i dont want to use a counter either
View 3 Replies
View Related
Aug 31, 2007
This loop is to find if there is the string "Need Parent " in the range. If so the Msgbox notifies the user.
After it finds the string, the loop needs to exit- I tried to assign a counter to the MsgBox, but no go....
For i = 6 To lLrw
If Range("D" & i).Value = "Need Parent" Then
MsgBox "Stop. Parent Records Still Need To Be Created."
End If
ii = MsgBox + 1
If MsgBox > 0 Then Exit For
End If
Next i
View 9 Replies
View Related
Jun 5, 2007
I'd like to miss out a loop in my For/Next code. Basically I've written the code below which inserts a few rows above the object cell if it doesn't match the previous cell (to seperate my data). In light of the fact that I've inserted rows, the object cell is now a blank cell, so when my For/Next statement continues it insert some more rows thinking that the previous cell doesn't match the object cell. I wondered if it was possible to skip the object cell forward so the For/Next statement can continue in the place it left off (in effect, it would be like going to "Next" twice without doing any of the code in between).
Sub insertrows()
Set SubAss = Range("A11:A10000")
Cr = 0
prevcell = Range("A11")
For Each cell In SubAss
If cell.Value <> prevcell Then
prevcell = cell.Value
cell.Range("A1:A3").EntireRow.Insert
cell.Offset(-2, 1) = " Total"
cell.Offset(-2, 1).Font.Bold = True
r = 6
View 6 Replies
View Related
Apr 21, 2006
am using excel to go to a web site and download a series of pages
www.abc.com/1
www.abc.com/2
etc
However, if the requested page doesn't exist I just want excel to ignore that and carry on. So I have...
For f = 60000 To 100000
Workbooks.Add
mtch = f
gp = Left(f, 2)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.cricketarchive.com/Archive/Scorecards/" & gp & "/" & mtch & ".html", _
Destination:= Range("A1"))
.Name = f
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True..........................
but excel still returns a message box saying it couldn't open the requested page when i just want it to skip to close workbook and loop. i have to hit 'debug' and move next line to where i want.
View 5 Replies
View Related
Sep 24, 2013
I'm trying to loop through worksheets as follows:
If ws.Name "DataTable" And ws.Name "Summary" Then ws.Activate
code to do some copy/paste stuff ....
Next ws
Try as I may, I can't get the code to skip the worksheets named "DataTable" and "Summary". Based on other posts, I've tried swapping out the And for an Or. Also tried using LCase(ws.Name), to no avail. Note that I'm acually trying to skip several other sheets as well but only listed two here for simplicity.
I'm using Excel 2007.
View 4 Replies
View Related
Apr 24, 2009
I would like to call upon a function until a certain criterion is fullfilled. Then, I would like to have the result returned to me and exit (all) open functions. For value1 = 1 and value2 = 10 I expect value1*value2 = 100. Instead, the routine returnz zero. What is the logical flaw in the code below.
Function testfunction(value1, value2)
If value1 = value2 Then
'Calculating the difference
testfunction = value1 * value2
Exit Function
ElseIf value1 < value2 Then
value1 = value1 + 1
Call testfunction(matrix1, matrix2)
End If
End Function
View 9 Replies
View Related
Jun 29, 2006
How can I write a For-Next Loop and have the counter skip values in between. For example:
For i = 8 to 17
When that is completed, the counter jumps to 21:
For i = 21 to 30
When that is completed, the counter jumps to 34:
For i = 34 to 43
View 3 Replies
View Related
Aug 3, 2009
Exit Sub alternative. I have the following
View 5 Replies
View Related
May 20, 2009
I have some code that expects the user to select a file to open. If they don't select a file it posts an error message "Stopping because you didn't select a file" then it exits. However then Excel pops a debug window. How can I get it to cleanly exit and not pop the debug window?
View 9 Replies
View Related
Jul 21, 2006
how to set up a macro that will close my workbook without saving.
View 3 Replies
View Related
Aug 30, 2012
How to use Exit Sub when I push Cancel with InputBox. Heres my code
VB:
Dim fName As String
Dim strMessage As String
strMessage = "Select .csv File"
MsgBox strMessage
[Code] ....
Its working at the moment when I push cancel, but when I select a csv file to import it comes up with an incompatible error.
View 2 Replies
View Related
Jan 13, 2014
I entered an activex object command thing.. and now I'm not sure how to exit out of it.. it just keeps alerting errors messages like reference is not valid and other ones depending on what I type in the formula box.
I just want to remove the object, but I can't get past the error alerts.. they just keep coming every single time I click somewhere on the workbook trying to exit out of it.. so I'm not sure how to even get out of it.. I don't want to open up Task Manager to exit out of the entire program because I didn't save my workbook.
View 2 Replies
View Related
Aug 15, 2014
I have a userform that allows users to choose their name (cboName) and an item (cboItem) from comboboxes.
Now, 2 specific items can only be selected from certain people
Lets say there's ItemA, ItemB and ItemC, and the people are named Joe, Bob and John
Now, only Bob and Joe can select ItemA or ItemC. How do I formulate If statements that prevent John from selecting ItemA or ItemC? Something like:
[Code]....
View 1 Replies
View Related
Nov 24, 2008
I have the code below assigned to a commandbutton. I also have 4 or so other private sub macros on this worksheet. When the activecell is in range 2 and I push the button, it exits the sub routine like it's supposed to. But then it deactivates all the other macros and they won't work again until I close the workbook. How can I get this code to exit the macro if the first if statement is true and to not affect any other macros?
View 3 Replies
View Related
Jul 18, 2009
I have an input box which has OK and cancel buttons If the user clicks cancel I would like to routine to end, which I can do with exit sub. However this particular sub routine has been called from another so the rest of the code continues. Is there a way of exiting all the routines on cancel -- or a neater way than exit sub ?
View 4 Replies
View Related
May 28, 2012
I'm using a multipage control on a form that has the style set to fmTabStyleButtons. So it uses buttons instead of tabs. I named one of my buttons (tabs) to Close. Can I set this up to where just pressing the Close button will cause it to exit the app not just go to that page?
View 9 Replies
View Related
Sep 10, 2012
VBA - Exit all Subs not just current
View 2 Replies
View Related
Jun 5, 2013
I have two userform combo boxes, the second follows up on the results of the first.
The second Combo Box looks for incomplete fields and provides them in a drop box. If the data the second Combo Box is looking at (This is different data than the first Combo Box is looking at.) is complete it updates the field, if not then it adds it to the drop down list.
My problem is this, if it finds everything updated and nothing is added to the drop-down list, how do I exit? I just want a complete break where all programming ends.
I am in the Initialize subroutine and I have tried Exit Sub and it still brings up the user form.
View 3 Replies
View Related
Jul 2, 2013
I have 2 sheets to this program. I open the properties to change the ScrollArea of Sheet1 to $A$1:$N$24 so the users that will be using this cannot scroll down to see data. As soon as I hit save, or even Save As and replace the file, then reopen it, I am able to scroll on Sheet1. Now, I do not recall if this same thing was happening prior to restricting access to the workbook or not. One thought I have is because I am the fully authorized user, is that why, even after saving I am able to scroll? Would someone who does not have the permissions that I do in the work book not be able to scroll? Or am I just missing something small?
View 7 Replies
View Related
Dec 5, 2013
I use the macro below to loop through files, which works fine. Can the code be modified so that when a specific file is opened, the entire macro stops? Exit Sub only seems to stop the inner loop, but I would like the entire macro to be stopped.
Sub Loop_Through_Files()
Dim FSO As Scripting.FileSystemObject
Dim FF As Scripting.Folder
Set FSO = New Scripting.FileSystemObject
Set FF = FSO.GetFolder("D:")
DoOneFolder FF
[Code] ........
View 3 Replies
View Related
Mar 23, 2007
I a macro (Main) I start a userform (FrmMenu.show).
This userform has a cancel button (CmdCancel). When the cancel button is activated I want the macro (Main) to stop running.
View 9 Replies
View Related
Apr 6, 2007
I have a txt calendar control the askes the user for a date, then find that date in another sheet.
If the user enters a date out of my range the code bugs out. Is there an error handeler for this? Like can I set an error message?
View 9 Replies
View Related
Jun 30, 2008
If Sub A is calling Sub B which is calling Sub C, then how to exit the entire procedure from Sub C, if one needs to?
Placing Exit Sub in Sub C would exit the sub and returns control to Sub B, and the execution of procedure continues!
View 9 Replies
View Related
Oct 21, 2008
recently excel 2003 began forcing the save as dialogue box when i try to close the workbook. I do not remember installing anything in particular on my machine when it started acting up.
I tried uninstalling office with Revo Uninstaller (gets rid of those pesky registry entries that the control panel add/remove leaves behind).
I reinstalled and I have the same issue.
Im thinking it is a macro of some sort as when I open the program holding down shift, it does not prompt me to save as when I exit. I do have one excel add in installed, but I have used it for many years without any issues.
View 9 Replies
View Related
Nov 11, 2008
I have a worksheet that I protect from cell deletions by disabling the command function for 'delete' using the following
CommandBars.FindControl(ID:=292).Enabled = False
I execute this code in two subs:
Private Sub Worksheet_Change(ByVal Target As Range)
CommandBars.FindControl(ID:=292).Enabled = False
...
and
...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
CommandBars.FindControl(ID:=292).Enabled = False
...
all fine and good, works great only problem is...
once disabled this function is NOT re-enabled when I leave the workbook.
When the sheet deactivates I run this:
______________________________________________________________
Private Sub Worksheet_Deactivate()
CommandBars.FindControl(ID:=292).Enabled = True
End Sub
____________________________________________________
that works fine while I am changing worksheets within the workbook.
But it doesn't do for this function to remain disabled even after I close the workbook and then it's disabled in all the other workbooks.
What I need is to have the code re-enable this function when the workbook closes or when I activate a different workbook so that the function is only disabled within the specific worksheet of the specific workbook and it remains enabled everywhere else ESPECIALLY after I close that sheet and workbook where I have it disabled!
I tried this:
______________________________________________________________
Private Sub Workbook_Deactivate()
CommandBars.FindControl(ID:=292).Enabled = True
End Sub
____________________________________________________
View 9 Replies
View Related
Sep 8, 2005
Is there anyway I can make excel quit using VBA without the option of saving the changes popping up?
View 5 Replies
View Related
May 13, 2006
can i remove the exit button from a userform
View 2 Replies
View Related