#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Macro Help

Is there a way to modify the macro below so that it only looks at the cells
that have actually been changed? Rather than having it look at the specific
columns when something on the spreadsheet has been changed.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wS As Worksheet
Dim MyRange As Range
Dim c As Range
For Each wS In Worksheets
If wS.Range("C1").Value = "changed" Then
lastrow = Cells(Cells.Rows.Count, "AO").End(xlUp).Row
lastrow1 = Cells(Cells.Rows.Count, "AQ").End(xlUp).Row
lastrow2 = Cells(Cells.Rows.Count, "AY").End(xlUp).Row
lastrow3 = Cells(Cells.Rows.Count, "AZ").End(xlUp).Row
lastrow4 = Cells(Cells.Rows.Count, "BF").End(xlUp).Row
lastrow5 = Cells(Cells.Rows.Count, "BG").End(xlUp).Row
lastrow6 = Cells(Cells.Rows.Count, "BL").End(xlUp).Row
Set MyRange = Range("X1:BL" & WorksheetFunction.Max(lastrow, lastrow1,
lastrow2, _
lastrow3, lastrow4, lastrow5, lastrow6))
For Each c In MyRange
If Not c.HasFormula Then
c.Value = Trim(c.Value)
End If
Next c
End If
Next wS

Thanks in advance for any help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Macro Help

Maybe use a Worksheet_Change event instead of the BeforeClose event.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target Is Nothing and Not Target.HasFormula Then
Target.Value = Trim(Target.Value)
End If
End Sub

This would monitor the changes as they occur and immediately apply fix.


"akemeny" wrote:

Is there a way to modify the macro below so that it only looks at the cells
that have actually been changed? Rather than having it look at the specific
columns when something on the spreadsheet has been changed.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wS As Worksheet
Dim MyRange As Range
Dim c As Range
For Each wS In Worksheets
If wS.Range("C1").Value = "changed" Then
lastrow = Cells(Cells.Rows.Count, "AO").End(xlUp).Row
lastrow1 = Cells(Cells.Rows.Count, "AQ").End(xlUp).Row
lastrow2 = Cells(Cells.Rows.Count, "AY").End(xlUp).Row
lastrow3 = Cells(Cells.Rows.Count, "AZ").End(xlUp).Row
lastrow4 = Cells(Cells.Rows.Count, "BF").End(xlUp).Row
lastrow5 = Cells(Cells.Rows.Count, "BG").End(xlUp).Row
lastrow6 = Cells(Cells.Rows.Count, "BL").End(xlUp).Row
Set MyRange = Range("X1:BL" & WorksheetFunction.Max(lastrow, lastrow1,
lastrow2, _
lastrow3, lastrow4, lastrow5, lastrow6))
For Each c In MyRange
If Not c.HasFormula Then
c.Value = Trim(c.Value)
End If
Next c
End If
Next wS

Thanks in advance for any help.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro recorded... tabs & file names changed, macro hangs Steve Excel Worksheet Functions 3 October 30th 09 11:41 AM
Macro to copy and paste values (columns)I have a macro file built C02C04 Excel Programming 2 May 2nd 08 01:51 PM
AutoRun Macro with a delay to give user the choice to cancel the macro wanderlust Excel Programming 2 September 28th 07 04:09 PM
Need syntax for RUNning a Word macro with an argument, called from an Excel macro Steve[_84_] Excel Programming 3 July 6th 06 07:42 PM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 01:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"