Results 1 to 21 of 21

Thread: [RESOLVED] How to read the value of an excel combo box from VB?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Resolved [RESOLVED] How to read the value of an excel combo box from VB?

    I can read 'regular' cell values from Excel into a VB program (either thru a recordset or as an Excel Object). But the cell value for a combo box reads into VB from Excel as an empty string even though there is clearly a text string visible in the box. How can I get the actual string that the user selected from the combo box?

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to read the value of an excel combo box from VB?

    What is the code you use to read "the cell value for a combo box" into VB???
    The task will be easier for you if you link the combobox to a cell then read the value of that cell.
    However, there are 2 different kinds of combobox in Excel, which one have you used? Form combobox or ActiveX combobox?
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to read the value of an excel combo box from VB?

    you need to get the text property of the combobox
    shtobject.combobox1.text
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    Thank you for your responses. I did not set up the Excel spreadsheet I am trying to read. I can use either rs.Fields(1) with ADO or xlsheet.Cells(5, 2) with Excel Objects, but the result is the same. I can read combo boxes with single and integer values in them into my VB6 app. However, I can not read combo boxes that have string values in them from the same spreadsheet. What am I doing incorrectly? How can I get these string values?

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to read the value of an excel combo box from VB?

    are the you reading the values into a numeric type variable?
    show us some code of what you are trying to do, including declaration of relevant variables
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    Code:
    Private Sub GetExcelData()
      Dim xl As New Excel.Application
      Dim xlsheet As Excel.Worksheet
      Dim xlwbook As Excel.Workbook
      Dim txt1 As String
      Dim txt2 As String
     
      strfile = "C:\Documents and Settings\" & Windowsid & "\Desktop\Windload\" & exsht
      Set xlwbook = xl.Workbooks.Open(strfile)
      Set xlsheet = xlwbook.Sheets.Item(2) 
      
      txt1 = xlsheet.Cells(5, 2).Value  'drop-down list with string values
      txt2 = xlsheet.Cells(28, 2).Value 'drop-down list with single values
      
      xl.ActiveWorkbook.Close False, strfile
      xl.Quit
    
      Set xlwbook = Nothing
      Set xl = Nothing
    End Sub
    Txt2's cell reads into VB6 app fine as "0.500", txt1's cell is "Alternate" in Excel but reads into VB6 as an empty string. Any ideas?

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    txt1's cell is "Alternate" in Excel
    Though you have mentioned it here but do this for me.

    manually open the excel file and check what is the value of cell B5 in the relevant sheet?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    As you requested, I manually opened the excel file and checked the value of the cell I'm trying to get as txt1. That particular cell is a drop-down where the excel user can select either Alternate or Continuous. The value that shows up at the top of the spreadsheet in the fx box is Alternate.

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    Hi Can I see your excel sheet. Also you mentioned combo box in the title but you are refering to a Drop down list which is created with Data=>Validation right?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    I am a not that familiar with excel. I said combo box because that's what it reminded me of first. I'm learning excel teminology on the fly here just to do this project. Due to company policy, I am not allowed to post the spreadsheet. I found that if I write the excel spreadsheet out to a csv file, then the text strings I need do show up correctly in that file. If I can't find a clean way to read the string directly from excel then I could do that and parse the .csv file to get what I need.

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    Quote Originally Posted by TheOldGuy
    I am a not that familiar with excel. I said combo box because that's what it reminded me of first. I'm learning excel teminology on the fly here just to do this project. Due to company policy, I am not allowed to post the spreadsheet. I found that if I write the excel spreadsheet out to a csv file, then the text strings I need do show up correctly in that file. If I can't find a clean way to read the string directly from excel then I could do that and parse the .csv file to get what I need.
    That's ok

    I actually wanted to check a few things but you can check them for me.

    1) When Cells B5 and C5 are merged the ceel address becomes B5. So could you check if the cells are merged.

    The value that shows up at the top of the spreadsheet in the fx box is Alternate.
    2) What do you mean by "top of the spreadsheet" are you refering to the formula bar or the cell?

    3) What is the name of the workbook and the worksheet?

    I can think of these 3 at the moment...

    Let see how it unfolds
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    1.) If by 'merged cells', you mean that when I click on B5 then the box covers B5 and C6, then yes that's true.
    2.) I meant the formula bar right under the toolbars at the top of the excel screen.
    3.) Workbook name is 3_OUT_STL_WL, worksheet name is Windload Calcs.

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    sorry I mis-typed.
    'I click on B5 then the box covers B5 and C6' should be B5 and C5.

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    1.) If by 'merged cells', you mean that when I click on B5 then the box covers B5 and C6, then yes that's true.
    What does

    txt1 = xlsheet.Cells(5, 3).Value

    in the above code give you?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    I tried that too. Still gives an empty string.

  16. #16
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    okay lets try this experiment...

    open the excel sheet

    in the module paste this and then tell me what do you get when you run it...

    Code:
    Sub aaa()
    'First try this
    MsgBox Sheets("Windload Calcs").Cells(5, 2)
    
    'then comment the above and then uncomment the below
    'MsgBox Sheets("Windload Calcs").Cells(5, 3)
    
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  17. #17

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    MsgBox Sheets("Windload Calcs").Cells(5, 2) - showed the string I need to get

    'MsgBox Sheets("Windload Calcs").Cells(5, 3) - was an empty string.

  18. #18
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    Then the problem is here....

    Set xlsheet = xlwbook.Sheets.Item(2)

    This is not setting it to the right worksheet which is ("Windload Calcs")

    Try this

    Set xlsheet = xlwbook.Sheets("Windload Calcs")
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  19. #19

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Re: How to read the value of an excel combo box from VB?

    Ok. That returns the string. Thank you ver y much. I don't understand though why it would return the value of the first column but not the value of the second column in the same row before? I certainly do appreciate your help and time on this. I will mark this as resolved (as soon as I figure out how to do that).

  20. #20
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to read the value of an excel combo box from VB?

    more than that what bothers me is that with that code you could get the right values for

    txt2 = xlsheet.Cells(28, 2).Value
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    35

    Question Re: How to read the value of an excel combo box from VB?

    Yes, that was a typo, it should have been (5, 2). Thx.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width