You are here:

Excel/2 macros creating issue with each other

Advertisement


Question
Hi Expert,

I ma facing a very strange issue.

I am using below 2 macros :

macro 1 : to enter date when user enter any value
below is the code:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("MyWork").Select
If Target.Cells.count > 1 Then

Exit Sub

       If Not Intersect(Target, Range("W2:W20000")) Is Nothing Then

         With Target(1, 8)

         .Value = Date
         

         .EntireColumn.AutoFit
         End With

       End If
Sheets("MyWork").Select
Range("A2").Select


 
End Sub

Macro 2:

Sub clear()
'
' clear Macro
'

'
   Sheets("MyWork").Select
   Range("A2").Select
   Range(Selection, Selection.End(xlDown)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.ClearContents
   Range("A2").Select
    
   Sheets("Main").Select
   Range("A2").Select
   
End Sub

Issue is , Macro 2 is working fine but as soon as I add macro 1 , it stats giving error , object not defined.

Please guide what to do , so that both the macros works fine together.

Thanks

Answer
Modify macro2 to:

Sub clear()
'
' clear Macro
'

'
   Application.EnableEvents = False
   Sheets("MyWork").Select
   Range("A2").Select
   Range(Selection, Selection.End(xlDown)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.ClearContents
   Range("A2").Select
    
   Sheets("Main").Select
   Range("A2").Select
   Application.EnableEvents = True
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jan Karel Pieterse

Expertise

Excel and Excel/VBA questions

Experience

Excel MVP

Organizations
Self employed Excel developer

Education/Credentials
Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

Past/Present Clients
Shell, Fortis bank, ABN-AMRO bank, Morgan Stanley, ...

©2016 About.com. All rights reserved.