Solved

Simple VB operation needed for excel Macro

Posted on 2003-10-22
4
156 Views
Last Modified: 2010-05-01
Hello,

I could really use a macro that can perform the following function:

Delete the contents of the last non-empty cell in the column before the active cell.

For example:

Say the active cell is B5, I would like the macro to delete the contents  of the last non-empty cell in column A (so if A1-A10 is a list, delete the contents of cell A10). If possible B5 would return to be the actice cell.

Greatly appreciated.

Dudu
0
Comment
Question by:the_duduman
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
Sub DeleteLastNonEmpty()
   Dim curCol As Integer
   curCol = Application.ActiveCell.Column

   prevCol = curCol - 1
   
   '' make sure there is a previous column
   Dim i As Integer
   i = 1
   If prevCol >= 1 Then
      While Not IsNull(Sheet1.Cells(i, prevCol)) And Trim(Sheet1.Cells(i, prevCol)) <> ""
        i = i + 1
      Wend
   End If
   
   Sheet1.Cells(i - 1, prevCol) = Null
End Sub
0
 

Author Comment

by:the_duduman
Comment Utility
Thanks for your quick reply bobbit31,

I seem to get an "1004  run-time error - application defined- or object-defined error". Pressing debug takes me to the the last line of the script:

 Sheet1.Cells(i - 1, prevCol) = Null

The sheet is not called Sheet1, could this be the problem?

There is no deletion taking place by the way.

Is this fixable?

Dudu

0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 500 total points
Comment Utility
yes... Sheet1 should be the name of the sheet

OR

replace all instances of Sheet1 w/ Application.ActiveSheet
0
 

Author Comment

by:the_duduman
Comment Utility
Works like a charm!

Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now