Solved

Where do I find the Trigger in a macro

Posted on 2014-11-06
3
70 Views
Last Modified: 2014-11-12
I am using the below Macro provided by Gowflow whic automatically gets a file and updates it. However as there are 3 fields it updates the file 3 times and I only need the one update once C5, H5 and J5 have been entered. So it only needs to get the file and enter the details after J5. I am not sure where the 'trigger' is in the macro to do this so would appreciate help.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C5")) Is Nothing Or Not Intersect(Target, Range("H5")) Is Nothing Or Not Intersect(Target, Range("J5")) Is Nothing Then
    If Range("C5") <> "" And Range("H5") <> "" Then CreateLog ActiveSheet
    
    
ElseIf Not Intersect(Target, Range("P3")) Is Nothing Or Not Intersect(Target, Range("Q3")) Is Nothing Then
    If Range("P3") <> "" And Range("Q3") <> "" Then UpdateLog ActiveSheet
End If

End Sub

Sub CreateLog(WS As Worksheet)
Dim MaxRow As Long
Dim oApp As Object
Dim WB As Workbook
Dim WSLog As Worksheet
Dim sFileName As String

Set oApp = CreateObject("Excel.application")
sFileName = ActiveWorkbook.Path & "\Sign-Off-Log.xlsx"
Set WB = oApp.Workbooks.Open(sFileName)
Set WSLog = WB.ActiveSheet
MaxRow = WSLog.Range("A" & WSLog.Rows.Count).End(xlUp).Row + 1

WSLog.Range("A" & MaxRow) = Now
WSLog.Range("B" & MaxRow) = WS.Range("C5")
WSLog.Range("C" & MaxRow) = WS.Range("H5")

Application.DisplayAlerts = False
WB.Close savechanges:=True
Application.DisplayAlerts = True
Set WB = Nothing
Set WSLog = Nothing
oApp.Quit
Set oApp = Nothing


End Sub

Sub UpdateLog(WS As Worksheet)
Dim MaxRow As Long
Dim oApp As Object
Dim WB As Workbook
Dim WSLog As Worksheet
Dim sFileName As String
Dim cCell As Range

Set oApp = CreateObject("Excel.application")
sFileName = ActiveWorkbook.Path & "\Sign-Off-Log.xlsx"
Set WB = oApp.Workbooks.Open(sFileName)
Set WSLog = WB.ActiveSheet

Set cCell = WSLog.Range("B:B").Find(what:=WS.Range("C5"), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
If Not cCell Is Nothing Then
    WSLog.Range("D" & cCell.Row) = WS.Range("P3")
    WSLog.Range("E" & cCell.Row) = WS.Range("Q3")
End If

Application.DisplayAlerts = False
WB.Close savechanges:=True
Application.DisplayAlerts = True
Set WB = Nothing
Set WSLog = Nothing
oApp.Quit
Set oApp = Nothing

End Sub

Open in new window

0
Comment
Question by:Jagwarman
3 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 40425843
Change this:
If Not Intersect(Target, Range("C5")) Is Nothing Or Not Intersect(Target, Range("H5")) Is Nothing Or Not Intersect(Target, Range("J5")) Is Nothing Then

Open in new window

to this:
If Not Intersect(Target, Range("J5")) Is Nothing Then

Open in new window


Note that if you subsequently need to amend C5 or H5, you would have to re-enter J5 to get the log file recreated.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40426049
This is a simpler version of the change event.  Is J5 allowed to be empty?
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C5,H5,J5")) Is Nothing Then
        If Len(Range("C5").Value) <> 0 And Len(Range("H5").Value) <> 0 Then CreateLog ActiveSheet
    ElseIf Not Intersect(Target, Range("P3,Q3")) Is Nothing Then
        If Len(Range("P3").Value) <> 0 And Len(Range("Q3").Value) <> 0 Then UpdateLog ActiveSheet
    End If
End Sub

Open in new window


I would recommend that you use some other repository format for your log.  Opening a workbook is relatively expensive.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

18 Experts available now in Live!

Get 1:1 Help Now