2017-09-03 83 views
0

我有解決不了......我的問題是這樣的一個問題:用戶窗體使用命令按鈕的變量

我需要創建4張內進行數據搜索用戶表單,這個數據是一樣的僅僅從一年改爲一年的電子表格,但是當我試圖將我的CommandBotton,並在錯誤組合框中使用變量...

的業務規則如下:

用戶輸入員工的在此登記會自動將數據拉到用戶表單的字段中,以防他想更改他使用組合框查閱工作表的情況這些工作表之間的nge和執行相同的搜索,但在不同的工作表中。

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

回答

0

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

下面是我將如何編寫代碼。我像With c.EntireRow那樣倒下,可以更容易地識別出您所指的是哪一列,而不是c.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