473,394 Members | 1,674 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,394 software developers and data experts.

Simple VB Loop

13
I am trying to create a do loop that will delete rows when the value in a column is 'Sale' or 'Distribution' and I need it to run until it reaches the bottom of the spreadsheet (incorporating blank cells along the way). Once this is complete, the data then needs to be output into a need tab.

I have tried and tried, but excel just freezes. It might be my coding (probably). I know this is minor, but help is needed quickly.

Many thanks
Nov 13 '06 #1
7 6351
willakawill
1,646 1GB
I am trying to create a do loop that will delete rows when the value in a column is 'Sale' or 'Distribution' and I need it to run until it reaches the bottom of the spreadsheet (incorporating blank cells along the way). Once this is complete, the data then needs to be output into a need tab.

I have tried and tried, but excel just freezes. It might be my coding (probably). I know this is minor, but help is needed quickly.

Many thanks
Sound like you have an endless loop
Would you please post your code as that will be the fastest way to a solution
Thanks
Nov 13 '06 #2
MAdcock
13
Sound like you have an endless loop
Would you please post your code as that will be the fastest way to a solution
Thanks
Code is as follows (so far):

Sub GetCache()

Sheets("PurchasesSales").Select
Range("A:K").Select
Selection.Copy

Sheets("Cache").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Sheets("Cache").Select
Range("B2:B10000").Select
Do Until ActiveCell.Value = 0
If ActiveCell.Value = "Sale" Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ElseIf ActiveCell.Value = "Purchase" Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ElseIf ActiveCell.Value = "Distribution" Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ElseIf ActiveCell.Value = " " Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(1, 1).Select
End If
Loop

End Sub
Nov 13 '06 #3
willakawill
1,646 1GB
Code is as follows (so far):

Sub GetCache()

Sheets("PurchasesSales").Select
Range("A:K").Select
Selection.Copy

Sheets("Cache").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Sheets("Cache").Select
Range("B2:B10000").Select
Do Until ActiveCell.Value = 0
If ActiveCell.Value = "Sale" Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ElseIf ActiveCell.Value = "Purchase" Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ElseIf ActiveCell.Value = "Distribution" Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ElseIf ActiveCell.Value = " " Then
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(1, 1).Select
End If
Loop

End Sub
Hi. Thanks for the code.
You are running a risk of an endless loop by looking for the condition 0
Do Until ActiveCell.Value = 0

Also this might be easier to read, debug

Select Case ActiveCell.Value
Case "Sale", "Purchase", "Distribution"
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp

Case " "
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(1, 1).Select
End Select

What exactly do you intend to do with that last statement;
ActiveCell.Offset(1, 1).Select
Nov 13 '06 #4
Killer42
8,435 Expert 8TB
You are running a risk of an endless loop by looking for the condition 0
Do Until ActiveCell.Value = 0
Perhaps as a first cut, this could be changed to watch for null?

Also, is it in fact locked, or could it simply be taking longer than expected? It might be a good idea to insert a DoEvents just before the Loop statement.
Nov 13 '06 #5
MAdcock
13
Hi. Thanks for the code.
You are running a risk of an endless loop by looking for the condition 0
Do Until ActiveCell.Value = 0

Also this might be easier to read, debug

Select Case ActiveCell.Value
Case "Sale", "Purchase", "Distribution"
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp

Case " "
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(1, 1).Select
End Select

What exactly do you intend to do with that last statement;
ActiveCell.Offset(1, 1).Select
I have tried your code, but it results in the same problem, that my Excel freezes and I have to restart the application.

All I want is for the lines in the spreadsheet with Distribution, Sale, Purchase in column B to be deleted. Is anyone able to write a quick piece of code that will do this process? Ive tried till im blue!

The ActiveCell.Offset statement was to move the curosr down to the next line.

HELP!
Nov 14 '06 #6
willakawill
1,646 1GB
I have tried your code, but it results in the same problem, that my Excel freezes and I have to restart the application.

All I want is for the lines in the spreadsheet with Distribution, Sale, Purchase in column B to be deleted. Is anyone able to write a quick piece of code that will do this process? Ive tried till im blue!

The ActiveCell.Offset statement was to move the curosr down to the next line.

HELP!
Yes it will move it down one row. It will also move it to the right by one column.
The code you are using works. I just tested it. There are two problems. First you are only exiting the loop if you encounter 0. This works if there is one and locks you into a permanent loop if there isn't one.
Second the Offset statement moves you to a new column. Try the code without the offset. Put a counter in the code as a safety exit. You are looking through 10,000 rows so;

Dim intCounter As Integer

'and inside your loop
intCounter = intCouter + 1
If intCounter = 10000 Then
Exit Sub
End If
Nov 14 '06 #7
Killer42
8,435 Expert 8TB
Another important point. If you have a DoEvents in there, you won't have to restart the application, even if you do get stuck in a loop - you can just interrupt the code. DoEvents allows Windows to notice other things, like the Break key (or mouse-clicks).
Nov 14 '06 #8

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

Similar topics

6
by: Arthur J. O'Dwyer | last post by:
I was paging through Coplien's book "Advanced C++ Programming Styles and Idioms" this afternoon and found some code that looked something like void sort(vector<foo> a) { int flip; do { for...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
6
by: porky008 | last post by:
I need to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
0
by: rich | last post by:
Hi all, I have a fairly complex "feed" application that recieves messages from an external user-supplied API via a callback function, and attempts to forward these messages to another...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
by: Jon Slaughter | last post by:
Say I have two nested foreach loops, is there any simple way of continuing out of the outter most loop depending on what happens in the inner most? e.g., foreach(int i in I) { foreach(int j...
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.