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

populate value into textbox based on last column

0

hello

first   thanks  for  mr.John  to  help  me in earlier time 

the  second  in this  thread . I  try  to populate  the  value in textbox1 based on last column contains  value  . after select  combobox1 and fill the  others  comboboxes  should populate  the  value in texbox1  based on last column contains values .

I try  add  this  line

TextBox1.Value = .Offset(, 4).Value
  in  the  end of  code in theses events Private Sub ComboBox1_Change()  ,Private Sub UserForm_Initialize()    but  it  gives  error  could  not  set   the  value property. type mismatch.

I  no  know  if  I  need more  than this  to  mod  the  code 

thanks in advance 

Answer
Discuss

Answers

0
Selected Answer

Mussa

The problem is that in your UserForm_Initialize macro, your With statement refers to a range (A2:A10) to which you apply offsets to populate Combobox lists but that does NOT work for a Textbox. The latter can contain only one value so I've changed your macro to show a - (until there's a change), shown in bold below: 

Private Sub UserForm_Initialize()
With Sheets("Sheet1")
    With .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
        ComboBox1.List = .Value
        ComboBox2.List = .Offset(, 1).Value
        ComboBox3.List = .Offset(, 2).Value
        ComboBox4.List = .Offset(, 3).Value
       TextBox1.Value = "-" '.Offset(, 4).Value 'TextBox can contain only one value
    End With
End With
Then in the ComboBox1_Change macro, you find the Code but you try to get a price using the line
TextBox1.Value = .Offset(, 4).Value
but offset from which cell?

The ComboBox1_Change macro could be changed to find the last price from sheet2. Here I've re-used/modified the loop using in the CommandButton1_Click macro to seek out the last price (changes in bold):

Private Sub ComboBox1_Change()
Dim c As Range
With Sheets("Sheet1")

    Set c = .Range("A:A").Find(What:=ComboBox1.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not c Is Nothing Then
        ComboBox2.Value = c.Offset(, 1).Value
        ComboBox3.Value = c.Offset(, 2).Value
        ComboBox4.Value = c.Offset(, 3).Value

        'Find last price for Code
        ss = Sheets("Sheet1").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 get price from last used cell in row
              TextBox1.Value = Cells(n, Cells(n, Columns.Count).End(xlToLeft).Column)
              Exit Sub
            End If
        Next n
        'if not found, blank price
        TextBox1.Value = "-"

    End If
End With
End Sub
Notice that if there is no price in sheet2, I've said "if not found, blank price". That does not reset the other fields but if ComboBox1 might be overwritten with a new code then you should consider searching sheet1 (to check it's new) and blank ComboBoxes 2-4 if it is..
Discuss

Discussion

thanks  again   but  I  would  search  for  dynamic  way  without   every time change  the  column contain the  price  . currently  it's existed  in  COL E    and  if    i add   in COL F   i have  to change  in combobox change  event . if  there  is  way  to  do  that  without   change  maually every time  .  it  'll  be  great
thanks  for  your cooperation
Mussa (rep: 48) Sep 15, '21 at 5:28 pm
Mussa. It's late here so I have no more time to look at this.

I think you're referring to COL E and F of sheet2 and so my second suggestion "Alternatively, the _Change could be changed to find the last price from sheet2.",

For that, you can use Find (as you did) or the For loop I used then use the  .Cells method  to find the last cell (and so the last price )
John_Ru (rep: 6142) Sep 15, '21 at 5:40 pm
Mussa

Okay. See revised Answer and file. There's still work for you to do though!
John_Ru (rep: 6142) Sep 16, '21 at 1:41 am
John thanks  again  when  said  me you can use Find (as you did) or the For loop I used then use the  .Cells method  to find the last cell (and so the last price ) I  tried by  for  loop as  you did it . but it  doesn't  show  the value in textbox  . so  I  decided  to wait for you to  reply  me  . if  you  don't  .  I will inform  you  . but  now  when you  update  your  code  I 've found  my  mistake    I write  this  line
  Cells(n, Cells(n, Columns.Count).End(xlToLeft).Column)=TextBox1.Value
but  the  correct  way as  you  wrote 
TextBox1.Value = Cells(n, Cells(n, Columns.Count).End(xlToLeft).Column)

I  have  notice  in  your  files  I  no  know  what  you  did  it  until  command  button in the  sheet  doesn't  run and  I try  making  design mod  is off  to click and  link  with  userform  .  it 's  not  work at all  . I  have  to enter in  vba project editor .
thanks   so  much  professional
Mussa (rep: 48) Sep 16, '21 at 4:21 am
Mussa. Thanks for sekecting my Answer. 
I'm not clear what you mean by the paragraph starting "I  have  notice  in  your  files..."  Us the file working or not? (I have some trouble with VBA sheet naming in  naming in Excel versions used in Asia) 
John_Ru (rep: 6142) Sep 16, '21 at 5:18 am
your  files  works  just  from the  vba editor  not  from inside  the  sheet by  command  button . it  should  load  the  userform when click the  command button  which is existed inside the sheet  .I  have  to  enter to vba editor  to  run   user form  from inside editor vba  . every  thing  is  ok. but  this  the first  time  shows like  this  problem . just  I  would  inform  you  with  this  problem  may  be  you have  idea  to  solve  it  until in the  future I  can  overcome  for like this  problems 
thanks again
Mussa (rep: 48) Sep 16, '21 at 6:56 am
Mussa. In the Deveioper ribbon, click Design then double click the button on Sheet2. There will be a macro to  which you just need to add
UserForm1.Show[
then return to sheet2 and try the button
John_Ru (rep: 6142) Sep 16, '21 at 7:25 am
I  know  . and  I did  it  and  I  try  to  explain  this  way  not  succeed . it  doesn't move  to vba editor  when click  command button  just  keep in the  sheet . so  when  I  enter   into vba editor about  modules in sheet1,2 .  it  doesn't  show  the  event  where  put the  codes. actually . extremely hidden bar which  contains  arrow  to  choice  the  event  . I  put  the  updating  code  in  my orginal  file  all of  goods  are  thing . I  don't  waste  your  time .  but  just   this   problem make  my curiosity because you are familiar with the mysteries of programming .  
Mussa (rep: 48) Sep 16, '21 at 9:10 am
In Design mode, suggest you delete my button, add a new one (and macro as above) then be sure to leave Design mode. Should work then. (I'm working from menory since I'm miles from my PC! ) 
John_Ru (rep: 6142) Sep 16, '21 at 9:31 am
I  did  it  and  shows message " you can't insert  object " 
it prevents  me  insert the  button .
Mussa (rep: 48) Sep 16, '21 at 3:47 pm
Mussa

My memory failed me! Once you have deleted my button, in the Developer tab click the down arrow on Insert then, under Active X controls, click the top left button - mouse hover will show "Command Button (Active X Control)"- and draw a button. Then add macro etc.
John_Ru (rep: 6142) Sep 16, '21 at 4:13 pm
all of  your steps     I  did  it  ,and when  draw  "Command Button (Active X Control)" shows  message"  you can't insert object "  .by  the  way  this  problem occurs  just  in your  files  otherwise  my  excel  and  the  files  in my PC   work without  any problem .
Mussa (rep: 48) Sep 17, '21 at 8:50 am
Mussa. I think ActiveX Controls are often disabled in corporate versions of Excel (to reduce risk- see Add or register an ActiveX control).. Sounds like that's the case for you so you could add a Form Button instead- top left in the section above ActiveX in the Design button. It will ask you to assign a macro to it.
John_Ru (rep: 6142) Sep 17, '21 at 9:14 am
Add to Discussion


Answer the Question

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