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.
4 6490
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?
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 -
-
Sub DeleteNumericRows()
-
-
Dim x As Integer
-
Dim Y As Double
-
Dim Contents As String
-
-
On Error Resume Next
-
x = 1
-
-
NextRow:
-
Contents = Cells(x, 1)
-
If Len(Contents) > 0 Then
-
Y = CDbl(Contents)
-
If Err.Number <> 13 Then
-
Rows(x).Delete
-
x = x - 1
-
End If
-
x = x + 1
-
Err.Number = 0
-
Contents = ""
-
GoTo NextRow
-
End If
-
-
End Sub
-
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
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 -
-
Sub DeleteNumericRows()
-
-
Dim x As Integer
-
Dim Y As Double
-
Dim Contents As String
-
-
On Error Resume Next
-
x = 1
-
-
NextRow:
-
Contents = Cells(x, 1)
-
If Len(Contents) > 0 Then
-
Y = CDbl(Contents)
-
If Err.Number <> 13 Then
-
Rows(x).Delete
-
x = x - 1
-
End If
-
x = x + 1
-
Err.Number = 0
-
Contents = ""
-
GoTo NextRow
-
End If
-
-
End Sub
-
It works perfectly, Andrew. Many Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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:
|
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...
|
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
| |
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.
|
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...
|
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...
|
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
|
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: 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...
| |
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: 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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |