Solved

Where do I find the Trigger in a macro

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

706 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

19 Experts available now in Live!

Get 1:1 Help Now