473,624 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel --Use VBA to delete all cells with numbers in column?

3 New Member
I am trying to write an Excel VBA macro that will find all cells in Column A that have numbers (as opposed to text) and delete the entire rows (with the numbers in Column A). As a start, I tried writing a routine that I thought should at least enable me to select the first number in the column -- if I selected Cell A1 before running the macro:

Public Sub FindNumber ()
Dim X As Integer
X = 0
Do Until ActiveCell > 0
ActiveCell.Offs et(X, 0).Select
X = X + 1
Loop
End Sub

Unfortunately, even this first step didn't work although all numbers were positive.

If anyone can help this eager but slow VBA learner get back on the right track, I would certainly be most appreciative.
Dec 5 '06 #1
4 6490
Killer42
8,435 Recognized Expert Expert
Just a quick question about your code - when you use .Select, doesn't that change the active cell? (I'm not certain, just checking).

Also, could you give us more detail than "didn't work"? What happened when you tried this?
Dec 5 '06 #2
Andrew Thackray
76 New Member
This code does the trick. It deletes all rows with a number in column A and stops at the first row with an emplty column A

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub DeleteNumericRows()
  3.  
  4. Dim x As Integer
  5. Dim Y As Double
  6. Dim Contents As String
  7.  
  8. On Error Resume Next
  9. x = 1
  10.  
  11. NextRow:
  12.     Contents = Cells(x, 1)
  13.     If Len(Contents) > 0 Then
  14.         Y = CDbl(Contents)
  15.         If Err.Number <> 13 Then
  16.             Rows(x).Delete
  17.             x = x - 1
  18.         End If
  19.     x = x + 1
  20.     Err.Number = 0
  21.     Contents = ""
  22.     GoTo NextRow
  23.     End If
  24.  
  25. End Sub
  26.  
Dec 6 '06 #3
Eager VBA Learner
3 New Member
Just a quick question about your code - when you use .Select, doesn't that change the active cell? (I'm not certain, just checking).

Also, could you give us more detail than "didn't work"? What happened when you tried this?
Yes. For instance, if my routing is just:
:
Public Sub FindNumber()
Dim X As Integer
X = 0
X = X + 1
ActiveCell.Offs et(X, 0).Select
Public Sub FindNumber()
Dim X As Integer
X = 0
X = X + 1
ActiveCell.Offs et(X, 0).Select
End Sub
..then the active cell moves down one row in the column each time the macro is run. But when I include the loop, it doesn't move at all. I thought it should keep moving down until it reaches a number greater than zero.





End Sub
Dec 6 '06 #4
Eager VBA Learner
3 New Member
This code does the trick. It deletes all rows with a number in column A and stops at the first row with an emplty column A

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub DeleteNumericRows()
  3.  
  4. Dim x As Integer
  5. Dim Y As Double
  6. Dim Contents As String
  7.  
  8. On Error Resume Next
  9. x = 1
  10.  
  11. NextRow:
  12.     Contents = Cells(x, 1)
  13.     If Len(Contents) > 0 Then
  14.         Y = CDbl(Contents)
  15.         If Err.Number <> 13 Then
  16.             Rows(x).Delete
  17.             x = x - 1
  18.         End If
  19.     x = x + 1
  20.     Err.Number = 0
  21.     Contents = ""
  22.     GoTo NextRow
  23.     End If
  24.  
  25. End Sub
  26.  
It works perfectly, Andrew. Many Thanks!
Dec 6 '06 #5

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

Similar topics

3
13612
by: Woodster | last post by:
I knoew that free is usually paired with malloc and delete is usually paired with malloc, alloc or similar. Can I use delete with malloc? The main reason I ask is for using the strdup function. If I have a char *, I have no real way of knowing whether that char * had memory allocated for it using new or malloc (as used by strdup). Thanks in advance
1
4145
by: Howard Weiss | last post by:
I would like to set a particular cell in an Excel Spreadsheet to a specific value Visual Basic Equivalent would be Cells(row, column) = x where row and column locate the cell and x is the value In C++, I can execute code such as
1
1854
by: Dgates | last post by:
This request might belong in a "SQL"-related newsgroup, but it's such a pain using Agent to subscribe to a new group (and download its 50,000 messages!) just to ask one question. So, since it deals with an ASP.NET app, I thought I'd ask here. ============================================== I am using Web Data Adminstrator, which I downloaded recently from the Microsoft site. I get an error when I create tables or columns:
1
6316
by: Julien | last post by:
Hi, I'm actually trying to export a repeater to excel, everything seems ok but the problem is that the cells are not validated This means that for example date, it is left-centered instead of been right-centered when it is correctly validated, apprently excel doesnt recognize the numbers with coma and it is not possible to calculate formulas with this numbers. Does anybody know how to do that? Is it possible to use mso-number-format to...
3
5390
by: secretspot | last post by:
Hello group, I want to delete the content of a single column on a MySQL 3.23 database. Tried DELETE column_name FROM table_name; and other varieties with e.g. WHERE clause. No luck until now. The above statement gives the error msg: ERROR 1066: Not unique table/alias: column_name
0
1527
by: Shenoy.D | last post by:
Hi, I have a datagrid and I am able to export it to excel just fine - with one exception. The first column of the dg is a hyperlink column which works fine as well. I would like to know if there is a way to export to excel but maintain the hyperlink column such that I can click on the link and it will open to the link specified. Currently it just exports that column as a text column. Thanks in advance.
13
6949
by: Shelley | last post by:
Compare Current Year Worksheet with Previous Year Worksheet and if SSN exists in Current Year Worksheet & Not in Previous Year - Copy this Row from Current Year Worksheet & Paste into Previous Year Worksheet Compare Previous Year Worksheet with Current Year Worksheet and if SSN exists in Previous Year Worksheet & Not in Current Year Worksheet - Delete this Row out of Previous Year Worksheet - THIS IS WHERE I'M HAVING TROUBLE. This is the...
0
849
by: Aan | last post by:
I need to use e.cancel from column changing event to keep the selected cell from moving to the next cell when the error arise. Is there any one know? Or is there another way to solve the problem? Thx before...
2
2785
JAMBAI
by: JAMBAI | last post by:
Hi, How to delete large numbers (100,000 - 1,000,000) records from linked table. I am trying to delete from MS Access Forms. Thanks Jambai
0
8172
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,...
0
8677
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8335
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
7158
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...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
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
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.