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 for column is not same location

0

Hi

sorry  I come back  . here I  have  case  occures  rarely  . I  want  to  deal  with  this  case . I  add  sheet3  and  the  column PCR    is  different  in  location in sheet1,sheet3 

so  I  have curisoty  how   could  modify  this  line 

 Me.Label15.Caption = "PRC"
                Me.TextBox2.Value = c.Offset(, 5).Value
to  deal  with  two  columns 4,5 instead  of  just  use column 5  as  in  orginal  code . so  how  bring  the  last  value for  differnt  column PCR in  location  in sheet1,3 after  select combobox1,optionbutton2.

best regards,

Maklil

Answer
Discuss

Answers

0
Selected Answer

Maklil

Since your column number can vary, you can solve this by using a function (private to the UserForm) as below:

Private Function FindColumn(OutStr As String, WKS As String)

'reset output
FindColumn = 0
'find match
For p = 1 To 10
    If Worksheets(WKS).Cells(1, p).Value = OutStr Then FindColumn = p
Next p

End Function

It has input values OutStr = PCR or PRICE (from the OptionButtons- see below) and WKS =the sheet name. These are passed to it from your ComboBox1_Change sub. It returns the column number matching the option button caption (or 0 is that is not found in the sheet).

Rather than hard-coding PRICE and PCR, I've relied on the captions on the option button to set both  Label5 and what is looked for in row 1 of the searched sheets to find the column (so if the caption text of OptionButton1 is changed from "PRICE" to PRICE ($)" say, then the form and results will use that new value if OptionButton1. is clicked).

The ComboBox1_Change code is modified so the column containing PCR or PRICE is determined as OutCol once per sheet (at the start of the loop) then written to TextBox 5 by the line:

If OutCol > 2 Then Me.TextBox2.Value = C.Offset(, OutCol - 2).Value

where the >2 test prevents picking cells which don't exist (column <0).

The modified sub in the attached revised file and shown below (changes in bold):

Private Sub ComboBox1_Change()
Dim C As Range, rng As Range
Dim search  As String
Dim ws As Variant, n As Long
Dim Cll As Range, OutCol As Long


'rewrite label 5
If Me.OptionButton1.Value = True Then Me.Label15.Caption = OptionButton1.Caption
If Me.OptionButton2.Value = True Then Me.Label15.Caption = OptionButton2.Caption

ws = Array("sheet1", "sheet2", "sheet3")
'loop through worksheets
For n = LBound(ws) To UBound(ws)
    
    ' find PCR or PRICE column (if any) = OutCol
    If Me.OptionButton1.Value = True Then
        OutCol = FindColumn(OptionButton1.Caption, CStr(ws(n)))
    ElseIf Me.OptionButton2.Value = True Then
        OutCol = FindColumn(OptionButton2.Caption, CStr(ws(n)))
    Else
        OutCol = 0
    End If

    ' look in worksheet
    Set rng = Worksheets(ws(n)).Range("B2:B12")

    search = Me.ComboBox1.Value
    Set C = rng.Find(What:=search, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious, MatchCase:=False, _
                    SearchFormat:=False)
    For Each Cll In rng
        If Not C Is Nothing Then
            'search value found
            Me.ComboBox2.Value = C.Offset(, 1).Value
            Me.ComboBox3.Value = C.Offset(, 2).Value
            Me.ComboBox4.Value = C.Offset(, 3).Value
            If OutCol > 2 Then Me.TextBox2.Value = C.Offset(, OutCol - 2).Value
        End If
    Next Cll
 Next n

End Sub

Note that I've now corrected the reference to PCR in the UserForm (was PRC in the previous answer)

Hope this works well for you.

Discuss

Discussion

Hi John,
thanks   for  this  function  treat  this  case .
MAKLIL (rep: 34) Aug 22, '22 at 7:03 am
Thanks for selecting my Answer, Maklil.
John_Ru (rep: 6142) Aug 22, '22 at 8:33 am
Add to Discussion


Answer the Question

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