Solved

Simple VB operation needed for excel Macro

Posted on 2003-10-22
4
160 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 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