开发者

Extracting data with userform

I am trying to extract data using a userform.

Here is the structure of Excel columns:

sheet1:  
column 1|column 2|column 3|column 4|column 5  
Sheet2:开发者_C百科  
column 1|column 2| ......................................... column xx  

The column 1s are same in both sheets. I want a dropdown to select any value from column 1 and these fields should be displayed in a userform based on the selected value of column 1.

column 1|column 2 of sheet1|column 2 ------ column xx of sheet 2| column 3, 4, 5 of sheet 1

Here is the code I created but it is unable to pick required data

Private Sub ComboBox1_Change()

  Application.ScreenUpdating = False    
  Dim CL As Object

  Worksheets(2).Select

  For Each CL In Worksheets(2).Range("A2:A20")
    If CL = ComboBox1.Text Then
      Range(ActiveCell, ActiveCell.Offset(0, 4)).Copy Destination:=ActiveCell.Offset(0, 5)    
    End If
  Next

  Worksheets(2).Select
End Sub 

Private Sub UserForm_Activate()
  ComboBox1.RowSource = ("A2:A20")
End Sub


How about something like...

Private Sub ComboBox1_Change()

  Application.ScreenUpdating = False    
  Dim CL As Excel.Range

  For Each CL In Worksheets(2).Range("A2:A20")
      If CL = ComboBox1.Text Then
          '# CL.Resize(0, 5).Copy Destination:=CL.Offset(0, 5)
          '# Or even better...
          CL.Offset(0, 5).Resize(0, 5).Value = CL.Resize(0, 5)
      End If
  Next

  Application.ScreenUpdating = True
End Sub 

Private Sub UserForm_Activate()
    ComboBox1.RowSource = "A2:A20"
End Sub

I've left the destination for the copy as before, although I'm not sure what this is achieving...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜