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

match & copy value to last column instead of last row

0

hi

I need  help  to  copy  data  from userform  to  sheet2  .   so  as  my  work  copy  to  the  bottom  . this  case  should  be  in  one  case  when  data are not  duplicated then  should  copy  from  the  userform  to  sheet2  , but  if    the  data   are  existed  then  when  write  the  value  in textbox 1  should  match  with  col a,b,c,d   and fill  the  value  to  last  column after  filled  column  by values  .as  you  see  I  have  many  columns  (price)   beacuse  the  prices  change . so  when select  combobox1 and  populate  the  others  will match in sheet2  with COL A,B,C,D   if  they are  matched  just  copy  the  value  from textbox1 to next to filled column . every  time  will move  next to  filled column  and  if  they  are  not  match  then should  copy  to  the  bottom .

I  hope  this  clear 

Answer
Discuss

Answers

0
Selected Answer

Mussa

In the attached revised file, the UserForm macro below is modified by the addition of the lines in bold (commented for your help):

Private Sub CommandButton1_Click()
Dim sh As Worksheet, ss As Integer
Dim n As Integer

Set sh = Worksheets("sheet2")

With sh
    ss = .Range("a" & .Rows.Count).End(xlUp).Row
    ' See if Code already exists...
    For n = 2 To ss
        If .Range("A" & n) = ComboBox1.Value Then
            'if so the add new price to the right of last recorded price
          .Cells(n, .Cells(n, .Columns.Count).End(xlToLeft).Column + 1) = TextBox1.Value
          Exit Sub
        End If
    Next n

' If loop completes (i.e. not found) then add new row
    .Range("a" & ss + 1) = ComboBox1.Value
    .Range("b" & ss + 1) = ComboBox2.Value
    .Range("c" & ss + 1) = ComboBox3.Value
    .Range("d" & ss + 1) = ComboBox4.Value
    .Range("e" & ss + 1) = TextBox1.Value

End With

End Sub
That loops through sheet2 to see if a match (first only) is found in column A. If so, it works out the last used column in that row n then adds the new price in the next cell. See Don's tutorial Find the Last Column with Data in Excel VBA to understand the line which writes the new price. It then exits the sub.

If there is no match, your original code adds a new row.

Note too that in  I've cleared the price box in macro ComboBox1_Change by adding the line:

TextBox1.Value = ""
Hope this works for you.
Discuss

Discussion

exactly  . this  is what I want  
thanks  so  much 
Mussa (rep: 48) Sep 15, '21 at 3:26 pm
Corrected my answer/file since I realised an early line should have said ".Rows" in 
ss = .Range("a" & .Rows.Count).End(xlUp).Row
John_Ru (rep: 6142) Sep 15, '21 at 4:36 pm
Add to Discussion


Answer the Question

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