?
Solved

Simple VB operation needed for excel Macro

Posted on 2003-10-22
4
Medium Priority
?
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9599994
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
ID: 9600293
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 2000 total points
ID: 9600316
yes... Sheet1 should be the name of the sheet

OR

replace all instances of Sheet1 w/ Application.ActiveSheet
0
 

Author Comment

by:the_duduman
ID: 9600364
Works like a charm!

Thanks!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

752 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