Only run piece of Macro if cell is...

Rowanhf12

New Member
Joined
Jan 22, 2016
Messages
27
I have a piece of code:

Code:
     Windows(MyWorkbook).Activate    Sheets("List").Select
    Range("A1").Select
    Dim Code1a As Excel.Range, Code1b As Excel.Range, Code1c As Excel.Range
    Dim Code2a As Excel.Range, Code2b As Excel.Range, Code2c As Excel.Range
    Dim Code3a As Excel.Range, Code3b As Excel.Range, Code3c As Excel.Range
    Set Code1a = Range("U2")
    Set Code1b = Range("V2")
    Set Code1c = Range("W2")
    Set Code2a = Range("U3")
    Set Code2b = Range("V3")
    Set Code2c = Range("W3")
    Set Code3a = Range("U4")
    Set Code3b = Range("V4")
    Set Code3c = Range("W4")
    
    Windows(JointWorkbook).Activate
    Sheets("P1T1").Select
    Range("A1").Select
    With Selection.ListObject.QueryTable
        .Connection = _
        "ODBC;DRIVER=SQL Server;SERVER=DW-SQL;UID=gbell;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=SOUR-00093-L;DATABASE=RDW"
        .CommandText = Array( _
        "SELECT view_Billing_v4.BillingDoc, view_Billing_v4.BillToCountry, view_Billing_v4.BillingDate, view_Billing_v4.ShipToCountry, view_Billing_v4.SalesDoc, view_Billing_v4.SalesDistrictText, view_Billing_" _
        , _
        "v4.ProductNo, view_Billing_v4.ProductText, view_Billing_v4.BillingQty, view_Billing_v4.ProductHierarchy, view_Billing_v4.USD_NetSales1, view_Billing_v4.USD_Cost, view_Billing_v4.BillMonth, view_Billin" _
        , _
        "g_v4.BillQuarter, view_Billing_v4.BillYear, view_Billing_v4.ProductHierarchyText_Material, view_Billing_v4.ItemCategoryCode" & Chr(13) & "" & Chr(10) & "FROM RDW.dm.view_Billing_v4 v" _
        , _
        "iew_Billing_v4" & Chr(13) & "" & Chr(10) & "WHERE (" & Code1a & ")" _
        , _
        " AND (" & Code1b & ")" _
        , _
        " AND (" & Code1c & ")" _
        , _
        " OR (" & Code2a & ")" _
        , _
        " AND (" & Code2b & ")" _
        , _
        " AND (" & Code2c & ")" _
        , _
        " OR (" & Code3a & ")" _
        , _
        " AND (" & Code3b & ")" _
        , _
        " AND (" & Code3c & ")" _
        )
        .Refresh BackgroundQuery:=False
    End With
    Range("A1").Select
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    Range("Table_view_Billings_V4[[#Headers],[BillingDate]]").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("P1T2").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("A1").Select

This basically refreshes a table in the data warehouse. Then copies data and pastes it into a separate tab.

At the moment this query gets repeated about 20 times.
Is there anyway to make it so the query runs if a number is greater than a cell value.

For example: I have the number 5 written in cell A1 on one of the tabs ("Sheet1")
If the above code was given 'PieceOfCode' number 4, it would run the macro code (because 4 is less than 5).
However if the above code was given 'PieceOfCode' number 6, it would NOT run the macro code (because 6 is greater than 5)

Hope that makes sense.

Any help, would be appreciated.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Add this in worksheet event if A1 in worksheet will get change and less than 5 it will run the macro



Code:
   Private Sub Worksheet_Change(ByVal Target As Range)  if not intersect(target,Range("A1").value) is nothing then 
		if target.value <5 then 
			Call macro name 
		end if 
End if 
End sub
 
Upvote 0
Thank you for the response:

The first row shows up as red. What am I doing wrong?

Code:
Private Sub Sheets("Sheet1")_(ByVal Target As Range)  if not intersect(target,Range("K3").value) is nothing then
        If Target.Value < 5 Then
            Call Macro1
        End If
End If
End Sub
 
Upvote 0
Sorry. The last message is now mute. Realised my error.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Not Intersect(Target, Range("K3").Value) Is Nothing Then
If Target.Value < 5 Then
Call RunQuery
End If
End Sub

Where RunQuery is a Macro to run the table.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top