Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

code doesn't work bring multiple values based on one value

0

hello 

i have  this   code    it  supposing   bring  the  values  on col b,e,f  baesd  on  cell value  in col d    if  i  rewritng  the  same    company   then  bring  values  in col b,e,f   

but  the  code  not  works 

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Long
    Dim rng As Range
    Dim val As Variant

    If Target.CountLarge > 1 Then Exit Sub

    If (Target.Column > 1) Or (Target.Row <= 2) Then Exit Sub

    If Target.Value = "" Then Exit Sub

    r = Target.Row
    Set rng = Range("b4:f" & r - 1)

    On Error GoTo err_exit
    val = Application.VLookup(Target.Value, rng, 6, 0)
 val = Application.VLookup(Target.Value, rng, 5, 0)
 val = Application.VLookup(Target.Value, rng, 2, 0)
    If IsError(val) Then Exit Sub
    If Cells(r, "e") & Cells(r, "f") & Cells(r, "b") = "" Then
    Cells(r, "e") = val
     Cells(r, "f") = val
      Cells(r, "b") = val
      End If

    Exit Sub

err_exit:
    Err.Clear

End Sub
Answer
Discuss

Answers

0
Selected Answer

I had a very tough time trying to understand what you want to do. I hope it is what the code below does. Please try it. Also read the comments carefully.

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 109 - 31 Oct 2020

    Dim Rng         As Range
    Dim R           As Long

    With Target
        If .CountLarge > 1 Then Exit Sub

        ' set up the trigger range
        Set Rng = Range(Cells(3, "D"), Cells(Rows.Count, "D").End(xlUp))
        ' no action if change wasn't within the trigger range
        If Not Application.Intersect(Target, Rng) Is Nothing Then
            ' target cells next to the trigger cell must be blank
            ' set up the range required to be blank
            Set Rng = Range(Cells(.Row, "A"), Cells(.Row, "E"))
            If WorksheetFunction.CountA(Rng) = 1 Then       ' skip if not blank (all but column D)

                ' set up the look-up range
                ' exclude all rows above Target.Row
                Set Rng = Range(Cells(2, "D"), Cells(.Row - 1, "D"))

                ' find the row where Taget.Value is found in column D
                On Error Resume Next
                R = WorksheetFunction.Match(.Value, Rng, 0)
                If R Then
                    R = R + Rng.Row - 1
                    Cells(.Row, "B").Value = Cells(R, "B").Value
                    Cells(.Row, "E").Value = Cells(R, "E").Value
                    Cells(.Row, "F").Value = Cells(R, "F").Value
                End If
                Err.Clear
            End If
        End If
    End With
End Sub
Discuss

Discussion

hi, variatus  unfortinatly  nothing happend     when  i  write  data  in  column   d   if the  data  already  is  existed  it  should  brings  data  in  col  b,e,f   thanks 
leopard (rep: 88) Oct 25, '20 at 12:17 am
Change this line Set Rng = Range(Cells(3, "A"), Cells(Rows.Count, "A").End(xlUp)) to point to column "D" instead of column A.
Variatus (rep: 4889) Oct 25, '20 at 5:14 am
it  gives me error     worksheetfunction.match error 1004 in this line 
R = WorksheetFunction.Match(.Value, Rng, 3)
leopard (rep: 88) Oct 25, '20 at 7:57 am
My code can't show that error because it has On Error Resume Next before the MATCH function is called. The code giving you the error isn't my code.
Variatus (rep: 4889) Oct 27, '20 at 11:05 pm
very strange if you work for you i no know if is because of version i use version office 2016 maybe properties are different    but  in  fact  it  gives  me  error
leopard (rep: 88) Oct 28, '20 at 9:13 am
No. On Error Resume Next absolutely prevents a break if there is an error. Works on all versions since at least 2003.Therefore you must have altered the code. Restart with a fresh copy of the procedure I posted.
Variatus (rep: 4889) Oct 28, '20 at 9:19 pm
i  updated   a new  file  and  test  your  code   but  the  error  still continues    please check it 
leopard (rep: 88) Oct 29, '20 at 5:22 am
The code in the file you uploaded runs without error on my system.
Variatus (rep: 4889) Oct 29, '20 at 6:17 am
so what  you  think   my system  is  the  problem   you  know   some  codes  causes  me   the  headache    i  face   many  errors  first  "invalid procedure call or argument"   some  body  told  me  update   office  or windows  actually  i  did  it  but  not  successes    after   search  for  long  time   the  problem   in my  code  is   comma  and  semi-colon  it  should  use   semi-colon   some  version office  beleive  that!  this  simple  takes  me  about  2 weeks  to  solve  it    the  second   i  have  another  code   it  gives  me  error  no cells  found    the  problem the  code  works  some  body  but  me   it  gives me  error   like   your  code    i  no  know  what's  the  problem  updating  or  files  system  missed    if  you  have  any  idea  or  suggest to  find   the  problem  , please inform  me 
leopard (rep: 88) Oct 29, '20 at 7:22 am
obviously  the  poblem is wndows10 or 0ffice2016  becuase   i  use  another  pc  windows7   and  office 2010   your  code  works  but  threre is  somthing  in  your  code  as  to   it  it  suppossing  bring  values in cols b,e,f  if  i  fill data  is  already existed  in  col  a   but   it  gives  me message  not  found  whether  data is existed   or  not 
leopard (rep: 88) Oct 29, '20 at 7:51 am
No, I don't think anything is wrong wikth your system. Frankly, I think you are not payhing close atytention. As you found out, even a wrong comma can crash a program. Therefore you should be very exact.
1. You say this line of code gives you a problem.
R = WorksheetFunction.Match(.Value, Rng, 3)
. Actually, there is no such line in my code. In my code the number is 0, not 3. the MATCH function isn't supposed to work with the parameter 3 but in my test it did work. I don't know why but I do know that "3" is not my code. You can check above. But it's possible that another version of Excel might be less forgiving.
2. On Error Resume Next will prevent notification of errors. Even if there is an error in that line there can't be a notification unless "On Error Resume Next" was removed. Since I already know that you made changes I must presume that you also removed this line, perhaps for testing.
3. An error can indeed occur on that line, either because of the 3 or because the search is unsuccessful. The latter is quite likely because your description of where to search is imprecise. Also the entry is made by hand and there might be a speclling mistake - many reasons. Differences are easy to repair if you tell the EXACT result of your test. Progress will be impossible while we can't even agree on which code you test.
Variatus (rep: 4889) Oct 29, '20 at 9:01 pm
Now I have updated my answer to let you enter the search criterium on column D instead of column A. Please read the code and understand every line of it. There are comments to help you. There is no other description of what it does - not from you and neither from me - anywhere else. Therefore you absolutely have to understand that code if you ever want it to work.
The fact that it doesn't run on your PC is secondary. It has to do with faulty copying. The problem should be resolved quickly and easily.
Variatus (rep: 4889) Oct 29, '20 at 9:20 pm
hi, varitus   i  would  thank  you  about  your   efforts  to  solve  my problem but  unfortunatly doesn't  work  as  what  i  want  when  i  write  in  col  d  and the  data  already is  existed  it  doesn't  show  the  rests  of  data  in  col  b,e,f   it  shows  the  message  box  as  in  code  whether    the  data  is  existed  or  not 
leopard (rep: 88) Oct 30, '20 at 12:34 am
I know that. Now we're making progress! Two questions:- (1) Where can the data exist? In column D or another column?
(2) On which row of column D does the user write? Is it a new row at the bottom of the table? Is it any row anywhere in the table?
Please answer my questions precisely, not excessively.
Variatus (rep: 4889) Oct 30, '20 at 8:05 pm
the   data   are  existed in col a,b,c,d,e , f     but   the  standard  based   on  col d   and  it  supposes  writing   in  at bottom  of  table   
leopard (rep: 88) Oct 31, '20 at 4:36 am
OK. I have changed my solution to incorporate your new information. Still, that are several points where I just guessed at what you might want because you provided no information. Therefore, please test the code and let me know what's different from your expectation.
Variatus (rep: 4889) Oct 31, '20 at 5:05 am
well done !   this  is   exactly   what   i   want    the   code  works   excellantly  and  i'm  sorry  if  i  don't  explain   enoughly      i  thought   my   post  is  clear    , sorry  again   because     my  question   taked  more time    to   solve   it 
leopard (rep: 88) Oct 31, '20 at 1:37 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login