Visual Basic for Applications (VBA)


Excel VBA - For Each Next Question (How to go to The Top of The For Loop)


Hi all,How can I avoid evaluating some sections of code and skip to the next value (the top of the loop) in a For Each Next construct???I would like to do something like the code below, but I am getting an error.    For Each c In Range("Datas")            If c.Interior.ColorIndex = 45 Then  '*** Do Nothing ***            Next c   <===<<<  Error!!!        end if        If c.Interior.ColorIndex = 22 Then  '*** Do Nothing ***            Next c   <===<<<  Error!!!        end if                If c.Interior.ColorIndex = 30 Then  '*** Execute Code ***            Statments            Statments            ...        end if        ...        ...        ...            Next cThanks in advance!!!
Why don't you eliminate the first 2 if statements as there is no need for it. And the statements after endif will be executed only if colorindex =30, so put them inside the loop.    For Each c In Range("Datas")                     If c.Interior.ColorIndex = 30 Then  '*** Execute Code ***            Statments            Statments            ...            ...
            ....        end if    Next c
Try something like this...
For Each c In Range("Datas")
If c.Interior.ColorIndex = 45 Then '*** Do Nothing ***
ElseIf c.Interior.ColorIndex = 22 Then '*** Do Nothing ***
ElseIf c.Interior.ColorIndex = 30 Then '*** Execute Code ***
Statments
Statments
...
end if
...
...
...
Next c
"The new phonebooks are here!"
Hi Suznal,That's exactly what I am currently doing but since I have many diferent conditions, this type of code gets a little bit confusing.Thats why I'd like to use separate ifs and go directly to the top of the loop without evaluating more conditions.Thanks for your help!!!
Why don't you eliminate the first 2 if statements as there is no need for it. And the statements after endif will be executed only if colorindex =30, so put them inside the loop.    For Each c In Range("Datas")                     If c.Interior.ColorIndex = 30 Then  '*** Execute Code ***            Statments            Statments            ...            ...
            ....        end if    Next c

Related Links

Searching for partial text in vba
Referring to arnage which has had its duplicates removed
Screen is not wide enough to show the whole page the right is not showing until I scroll back and forth
Assigning cells to ranges depending on a value (image included!)
Vlookup in VBA and adding text
Outlook 2016 VBAProject.otm question
Application.GetSaveAsFilename
Comparing count in VBA
Splitting a range into to 2 ranges based on values from another range
VBA - Select Case Statement - Array
For/Next Loop not working
Using Dictionary in VBA
Changing destination sheet using VBA
VB script for Excel 2010 that will do a search for any word or phrase in my entire workbook.
Reading data from Notepad and writing into a table in Access
VBA code for saving a ppt working in debug mode,not in real run

Categories

MSDN
Visual Studio Development
Game Development
Microsoft Azure
Data Platform Development
Data Platform Developm...
Samsung PC
Office Live Development
Expression
Project
Archived Forums C-D
Archived Forums I-L
Archived Forums W
OneDrive Development
Microsoft Partner Center
Forums Redesign - Q&A ...
Off-Topic Posts (Do No...
Visual Studio General ...
Windows Desktop Develo...
Media Foundation Devel...
General Office Develop...
Outlook for Developers
Developing Apps for Sh...
SharePoint 2010 - Usin...
SharePoint Legacy Vers...
Azure BizTalk Services
Azure App Service - We...
Azure Mobile Apps
Microsoft Azure Site R...
Azure Automation
Azure Cache
Azure Notification Hubs
Azure Key Vault
Azure Stream Analytics
Azure Service Fabric
Language Understanding...
Azure SQL Data Warehouse
Azure Functions
Azure Log Integration
Team Foundation Server...
BizTalk Server General
LINQ to SQL
Microsoft Solver Found...
JScript Native
Microsoft Translator D...
Small Basic
Data Mining
.NET Framework inside ...
SQL Server Replication
SQL Server Security
SQL Server Compact
SQL Server Spatial
SQL Server 2012 High A...
SQL Server 2012 Spatia...
SQL Server 2012 Analys...
SQL Server 2012 Master...
Windows Live General: ...
Windows Live ID: Devel...
Speech Service for Dev...
Developing for the Mob...
Bing Maps WPF, WP7/8/8...
Open Specifications Qu...
Project Customization ...
Project Server - Setup...
Bing Ads API: Development
Exchange Server Develo...
Microsoft Unified Comm...
Silverlight Installati...
Silverlight 4
Expression Studio with...
Microsoft Online Syndi...
Customer Care Framework
Retired SharePoint Bus...
Retired SharePoint Col...
ASMX Web Services and ...
Azure Startups
Charts & Reports in th...
Deep Zoom Composer
Game Technologies: Aud...
Hobbyist Zone
Git, Mercurial and Dro...
LightSwitch in Visual ...
Model-Based Testing wi...
Multimap API Developme...
Office Live Small Busi...
Parallel Computing Gen...
Parallel Computing Pla...
Project Codename:  Jasper
Storage Platform – ATA
SyncFx - Technical Dis...
SQL Azure Labs Support
SQL Server 2014 Data Q...
SQL Server 2014 In-Mem...
Team Foundation Server...
Team Foundation Server...
Using Visual Round Tri...
Tools for Windows Phon...
Visual Basic Express 2...
Visual Basic Express E...
Visual Studio 2008 SP1...
Visual Studio Gallery ...
Visual Studio Guidance...
Windows Live Alerts De...
Working with Data and ...
Mango Update (Archived)
Windows Phone Preview ...
SQL Server 2016 Preview
Azure N-Series Virtual...

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile