Help with Macro already written

woody1983

New Member
Joined
Feb 25, 2010
Messages
17
I have the following macro written which follows the purpose of what I want it to do:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Range("a2").Select
Dim rng As Range
If Target.Count > 1 Then Exit Sub
Set rng = Range("A1")
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Value = 1 Then
Application.GoTo Sheet2.Range("b1:r1")
Selection.Copy
Sheets("Sheet1").Select
Range("D4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
End Sub

I have a formula in cell A1 which does the following:

=IF(ISERROR(IF(VLOOKUP($E$1,$K$4:$K$5,1,0)=$E$1,1,"")),"",IF(VLOOKUP($E$1,$K$4:$K$5,1,0)=$E$1,1,""))

However even with calculations on I have to go into this cell and select the formulae and hit return for it to run the macro.

I wondered if there was a way I could either write this into the macro:

Range("A1").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(IF(VLOOKUP(R1C5,R4C11:R5C11,1,0)=R1C5,1,"""")),"""",IF(VLOOKUP(R1C5,R4C11:R5C11,1,0)=R1C5,1,""""))"
Range("A2").Select

or if this was even possible?
 

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
Hi

Give this a go

Range("A1").Formula = "Yourformula"
 
Upvote 0
Apologies my Macro currently looks like;

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim rng As Range
If Target.Count > 1 Then Exit Sub
Set rng = Range("A1")
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Value = 1 Then
Application.GoTo Sheet2.Range("b1:r1")
Selection.Copy
Sheets("Sheet1").Select
Range("D4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
End Sub
 
Upvote 0
Hi

The Spread sheet event "Worksheet_Change(ByVal Target As Excel.Range)" only runs when a change is made to your spread sheet, specifically Range("A1") because of the intersection. This means that nothing will happen until a change is made to "A1". I figure that is why you are having to actuate the cell.

Perhaps concider moving the macro to a Worksheet_SelectionChange(ByVal Target As Range) event and limiting the range as you already have with INTERSECT to perhaps A1 and A2 then when you move in these cells the macro will run.

Code:
Dim isect As Excel.Range
Set isect = Application.Intersect(Range(ActiveCell.Address), Range("a1:a2"))
If isect Is Nothing Then
Else

Dim rng As Range
If Target.Count > 1 Then Exit Sub
Set rng = Range("A1")
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Value = 1 Then
Application.GoTo Sheet2.Range("b1:r1")
Selection.Copy
Sheets("Sheet1").Select
Range("D4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
 

End If




regards

Kev
 
Upvote 0
Hi

Try Adding after your Dim rng As Range


sheets("sheet1").Range("a1").formula = "=yourformula"


Kev
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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