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! -
Sub Delete_Loop()
-
-
Dim x As Integer
-
-
Range("B1").Select
-
-
If Range("B1").Value = "Contract Type" Then
-
Rows("1:1").Select
-
Selection.Delete Shift:=xlUp
-
Exit Sub
-
-
x = 1
-
-
Do
-
-
If Range("B" & x).Value = "SMBS" Then
-
Exit Sub
-
If Range("B" & x).Value = ActiveCell.Value <> Empty Then
-
Rows("x:x").Select
-
Selection.Delete Shift:=xlUp
-
End If
-
-
x = x + 1
-
-
Loop While x < 50
-
-
End Sub
-
-
5 4956
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. -
Sub Test()
-
Range("B1").Select
-
Dim x As Long
-
Do
-
If x > 50 Then Exit Sub
-
If ActiveCell.Value = "Contract Type" Then
-
ActiveCell.Rows.Delete
-
Exit Sub
-
Else
-
ActiveCell.Rows.Delete
-
End If
-
x = x + 1
-
Loop Until ActiveCell.Value = "SMBS"
-
End Sub
-
NeoPa 32,569
Recognized Expert Moderator MVP
A couple of things to bear in mind here (referring to N2Deep's original code). - 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.
- 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).
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?
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?
Yes that makes sense...thank you.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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
|
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.
|
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,...
|
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...
| |
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()
{
|
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...
|
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...
|
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
...
|
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...
|
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...
| |
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,...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |