Solved

Where do I find the Trigger in a macro

Posted on 2014-11-06
3
76 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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