PDA

View Full Version : Solved: Change code to Private Sub Worksheet_Change (ByVal Target As Range)



marreco
09-08-2012, 12:35 PM
Hi.
I need to change code Private Sub Worksheet_Change (ByVal Target As Range)

each time a data is added in column 'B' code will fire

can anyone help me
Option Explicit
Sub Insert_Formula()
Dim iRow As Long
With Worksheets("FIN")
iRow = .Cells(.Rows.Count, "B").End(xlUp).Row
With .Range("A2", .Cells(iRow, "A"))
.Formula = "=index(BD!$A$2:$A$2000, match(FIN!B2, BD!$B$2:$B$2000, 0))"
.Value = .Value
End With
End With
End Sub

Thank you!!!

patel
09-08-2012, 02:01 PM
It's not clear for me what you want

marreco
09-08-2012, 02:07 PM
Hi.
I would like to leave this code automatically!

exemple
Private Sub Worksheet_Change (ByVal Target As Range)
Dim iRow As Long
With Worksheets("FIN")
iRow = .Cells(.Rows.Count, "B").End(xlUp).Row
With .Range("A2", .Cells(iRow, "A"))
.Formula = "=index(BD!$A$2:$A$2000, match(FIN!B2, BD!$B$2:$B$2000, 0))"
.Value = .Value
End With
End With
End Sub

patel
09-08-2012, 11:31 PM
do you want help ? help me to understand

marreco
09-09-2012, 04:10 AM
Hi.

I want to use the code so automatically.

instead of Sub ('Sub Insert_Formula ()'), I want to use ('Private Sub Worksheet_Change (ByVal Target As Range)').

patel
09-09-2012, 08:37 AM
you're repeating the same thing, describe me your goal, I don't want see your code

jolivanes
09-09-2012, 01:51 PM
I think (???????????)
After the Dim statements, add

If Target.Column = 2 Then


'Your code

Before the End Sub statement, add "End If"

marreco
09-09-2012, 04:25 PM
jolivanes hello, I have not tried because I can not build the code based on your tip.

I'll be grateful if someone could help me!

jolivanes
09-09-2012, 09:01 PM
Try this. It goes in the Code Sheet where you are changing Column B


Private Sub Worksheet_Change (ByVal Target As Range)
Dim iRow As Long
If Target.Column = 2 Then
With Worksheets("FIN")
iRow = .Cells(.Rows.Count, "B").End(xlUp).Row
With .Range("A2", .Cells(iRow, "A"))
.Formula = "=index(BD!$A$2:$A$2000, match(FIN!B2, BD!$B$2:$B$2000, 0))"
.Value = .Value
End With
End With
End If
End Sub

Aussiebear
09-09-2012, 11:46 PM
Try the following:
Private Sub WorkSheet_Change(ByVal Target As Range)
Dim iRow As Long
With Worksheets("FIN")
If Target.Columns = 2 Then
iRow = .Cells(.Rows.Count, "B").End(xlUp).Row
With .Range("A2", .Cells(iRow, "A"))
.Formula = "=Index(BD!$A$2:$A$2000, Match(FIN!B2, BD!$B$2:$B$2000, 0))"
.Value = .Value
End With
End If
End With
End Sub

Aflatoon
09-10-2012, 01:06 AM
Perhaps you mean:
Private Sub WorkSheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error Resume Next
Application.EnableEvents = False
With Intersect(Target, Range("B:B")).Offset(0, -1)
.FormulaR1C1 = "=Index(BD!R2C1:R2000C1,Match(RC[1],BD!R2C2:R2000C2, 0))"
.Value = .Value
End With
Application.EnableEvents = True
End If
End Sub

marreco
09-10-2012, 02:26 PM
Aflatoon, that was a great answer, worthy of an expert in Excel and VBA.:clap:
I'm very happy for helping me!:rotlaugh:
thank you very much!!

Aussiebear
09-11-2012, 03:10 AM
I'm not surprised. After all it was Aflatoon and he does have high standards.