【问题标题】:userform using variable of a commandbutton使用命令按钮变量的用户窗体
【发布时间】:2017-09-03 18:02:20
【问题描述】:

我有一个无法解决的问题……我的问题是这样的:

我需要创建一个在 4 个工作表内执行数据搜索的用户表单,此数据仅在电子表格中逐年变化,但当我尝试链接我的 CommandBotton 并使用错误组合框中的变量时...

业务规则如下:

用户在此输入员工的注册信息,这会自动将数据拉到用户表单的字段中,如果他想更改工作表,他使用组合框在这些工作表之间进行更改并执行相同的搜索但在不同的工作表中.

Public plan As Worksheet

Sub ComboBox1_Change()

Sheets(ComboBox1.ListIndex + 1).Activate

End Sub

Sub UserForm_Initialize()

For Each plan In ActiveWorkbook.Worksheets

ComboBox1.AddItem plan.Name

Next plan

End Sub

Sub bnt1_Click()

   With ThisWorkbook.Sheets(plan).Range("A:A")


Set c = .Find(textCp.Value, LookIn:=xlValues, lookat:=xlPart)

If Not c Is Nothing Then

c.Activate
textCp.Value = c.Value
textName.Value = c.Offset(0, 1).Value
textAd.Value = c.Offset(0, 2).Value
text60.Value = c.Offset(0, 65).Value
text60_20.Value = c.Offset(0, 66).Value
text100.Value = c.Offset(0, 67).Value
text100_20.Value = c.Offset(0, 68).Value
textAdc.Value = c.Offset(0, 69).Value
textAdcT.Value = c.Offset(0, 70).Value

End If
End With

End Sub

Sub btnSair_Click()

Unload FormPes

End Sub

【问题讨论】:

    标签: vba excel combobox userform


    【解决方案1】:

    只需使用With plan.Range("A:A")

    这是我编写代码的方式。我觉得With c.EntireRowc.Offset(0, 65).Value 更容易识别您所指的列。

    Sub bnt1_Click()
    
       With plan.Range("A:A")
            Set c = .Find(textCp.Value, LookIn:=xlValues, lookat:=xlPart)
    
            If Not c Is Nothing Then
                With c.EntireRow
                    textCp.Value = .Value
                    textName.Value = .Range("B1").Value
                    textAd.Value = .Range("C1").Value
                    text60.Value = .Range("BN1").Value
                    text60_20.Value = .Range("BO1").Value
                    text100.Value = .Range("BP1").Value
                    text100_20.Value = .Range("BQ1").Value
                    textAdc.Value = .Range("BR1").Value
                    textAdcT.Value = .Range("BS1").Value
                End With
            End If
        End With
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 2021-02-05
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多