473,698 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA for Excel 03' need help with delete row macro

10 New Member
I'm new at trying to write VBA, and have been working on this delete loop for a while, and could use some help.

I want the macro to start in cell B1, if it finds the string "Contract Type" in cell B1 I want it to delete that entire row and exit (Contract Type will only ever appear in cell B1).

If it doesn't find string "Contract Type" in above, I would like it to move down the B column one cell at a time....deletin g all rows with an empty B cell, until it comes to the string "SMBS" at which point I would like it to exit (SMBS will never be more than 46 rows down the sheet).

Here is what I have so far...thanks in advance for your help!

Expand|Select|Wrap|Line Numbers
  1. Sub Delete_Loop()
  2.  
  3. Dim x As Integer
  4.  
  5. Range("B1").Select
  6.  
  7. If Range("B1").Value = "Contract Type" Then
  8.     Rows("1:1").Select
  9.     Selection.Delete Shift:=xlUp
  10.     Exit Sub
  11.  
  12. x = 1
  13.  
  14. Do
  15.  
  16. If Range("B" & x).Value = "SMBS" Then
  17.     Exit Sub
  18. If Range("B" & x).Value = ActiveCell.Value <> Empty Then
  19.     Rows("x:x").Select
  20.     Selection.Delete Shift:=xlUp
  21. End If
  22.  
  23. x = x + 1
  24.  
  25. Loop While x < 50
  26.  
  27. End Sub
  28.  
  29.  
Aug 6 '08 #1
5 4956
janders468
112 Recognized Expert New Member
this isn't the most extensible solution it's fairly hardcoded, assumes that you want to use the ActiveSheet etc. You could work with it but this logic does what I believe you are looking for.
Expand|Select|Wrap|Line Numbers
  1. Sub Test()
  2.     Range("B1").Select
  3.     Dim x As Long
  4.     Do
  5.         If x > 50 Then Exit Sub
  6.         If ActiveCell.Value = "Contract Type" Then
  7.             ActiveCell.Rows.Delete
  8.             Exit Sub
  9.         Else
  10.             ActiveCell.Rows.Delete
  11.         End If
  12.         x = x + 1
  13.     Loop Until ActiveCell.Value = "SMBS"
  14. End Sub
  15.  
Aug 6 '08 #2
NeoPa
32,569 Recognized Expert Moderator MVP
A couple of things to bear in mind here (referring to N2Deep's original code).
  1. When deleting lines it is better to start from the bottom. Otherwise you have to remember NOT to increment the row number after a line is deleted. Think about it.
  2. In your code at line #18, this is a continuation of the If statement from line #16. In other words it can never be run (as it follows a line Exit Sub).
Otherwise your code is not far off.

Perhaps look through it using the debugger, and see where it's not behaving as you'd expect it to (Debugging in VBA).
Aug 6 '08 #3
N2Deep
10 New Member
Janders468...th ank you very much for that. I added EntireRow (ActiveCell.Row s.EntireRow.Del ete) and it worked like a charm.

NeoPa...thank you for helping me understand. I see what you are saying about lines 18 & 16. I do also understand about starting from the bottom, but would you recommend that in this case where I sometimes I have up to 30k+ lines of data?
Aug 6 '08 #4
NeoPa
32,569 Recognized Expert Moderator MVP
If all 30K lines needed to be searched, yes.

If you are still talking about a range of 50 lines at the top, then "the bottom" in this case means at line 50.

Does that clarify?
Aug 6 '08 #5
N2Deep
10 New Member
Yes that makes sense...thank you.
Aug 6 '08 #6

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

Similar topics

11
4049
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it again, Excel hangs. OR if I open Excel again (say from a desktop icon) before I close Access, Excel hangs. (this has happened for both 97 & 2000 for me) I of course thought that I mustn't be unloading a variable properly.
17
6336
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
14
5793
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
6
16111
by: Karl Richards | last post by:
I am attempting to delete duplicate rows in a spreadsheet using the Excel object. Does anyone have any idea how to do this? I've looked everywhere that I can find on the Web and have not been able to find anything on it. What I have so far is: Excel.Range range = oSheet.get_Range("A1", Type.Missing); range = range.get_End(Excel.XlDirection.xlDown); string downAddress = range.get_Address(false, false, XlReferenceStyle.xlA1,...
6
6309
by: jcf378 | last post by:
hello-- i am having trouble figuring out how to export individual records from an Access 2002 Form into a pre-existing Excel spreadsheet, such that the exported record is merely appended to the Excel file, rather than completely overwriting it (as occurs with the basic OutputTo function). specifically, I am trying to create a macro that "backs-up" any deleted record. So, when someone goes to delete a record (i have a delete-record macro...
0
7275
by: shantanu | last post by:
I am trying to convert a macro code to c# that will copy the values of a column and paste to anather through paste special. Everything is working fine but the transpose meathod to paste the column value as row value is not working. Can someone kindly look into the code, if i am missing something. C# Code ------------ private void ManipulateXLS() {
3
9918
by: ManuelValdez | last post by:
Hello everybody! I need your valuable help to get an Excel macro to delete the single zeros only and no the zeros containing numbers like 360, 90, etc., because if I chose the search and replace in the Edit option, then all the zeros containing the numbers 360, 90, 502, etc. will be deleted. Some nice person helped me with the following code to delete the zeros: Selection.Replace What:="0", Replacement:="" , but unfortunately this code...
3
7664
by: JFKJr | last post by:
Hello everyone, the following is the code which opens an excel file and creates a toolbar button. And, when I click the toolbar button as shown at line #28, the code should call the "Example" macro. But, it is displaying "The Macro "Example" cannot be found" Message Box. Can anyone suggest an idea on how to call the macro whenever a user clicks the toolbar button? Your help will be much appreciated. Thanks! Sub RunMacro1() Dim...
1
1843
by: ericjohnson | last post by:
I've got a macro that has already been written for an Excel spreadsheet used for estimating at work. I know enough VB to write very basic macros, but I cannot understand the logic and purpose behind this function. It is called in several of the other macros already written for the worksheet. Here is the macro: Function ExSht(shtnam As String) Dim rn As String Dim a As String Worksheets(shtnam).Select ...
1
2254
by: Catbkr1 | last post by:
I have to automatically create some Excel Spreadsheets based on automatically generated .CSV files that are produced overnight. Each .CSV has several columns that need to be deleted. The same columns (both in location and name) are deleted each time. The macro checks for Column Headers/Names and if the value isn't equal to one of 8 specifc values, I want to delete the column. I am attempting to create a Macro that executes automatically when...
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8895
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.