[英]How to capture data to a specified excel sheet(first workbook) from two different workbooks from the Userform?
我有workbook-1,單擊添加按鈕時,實際上應該從用戶窗體捕獲數據。
在workbook-2中,我只有組合框列表,以便在用戶窗體中從comobox和textbox中自動選擇時顯示excel數據。
但是現在我遇到了一個問題,當我通過選擇所有組合框列表並手動填充其他數據來填充用戶表單,然后單擊添加按鈕時,數據將傳輸到我的Workbook-2(在我的組合框列表下方)。
如何將用戶窗體數據捕獲到工作表工作表1“ Sheet1”上。
我的Workbook-2路徑是“ C:\\ Users \\ Desktop \\ Work.xlmx”,我是否還需要在命令按鈕中包含該路徑?
下面是我的combox和添加命令按鈕的代碼:
Private Sub cboLs_DropButtonClick()
Dim wb As Workbook 'workbook 2 for combobox list
Dim i As Long
Dim ssheet As Worksheet
Set wb = Workbooks.Open("C:\Users\Desktop\Book1.xlsx")
Set ssheet = wb.Worksheets("LS")
If Me.cboLs.ListCount = 0 Then
For i = 2 To ssheet.Range("A" & ssheet.Rows.Count).End(xlUp).Row
Me.cboLs.AddItem Sheets("LS").Cells(i, "A").Value
Next i
End If
End Sub
Private Sub cboLs_Change()
Dim wb As Workbook
Dim ssheet As Worksheet
Dim i As Long
Set wb = Workbooks.Open("C:\Users\Desktop\Book1.xlsx")
Set ssheet = wb.Worksheets("LS")
For i = 2 To ssheet.Range("A" & ssheet.Rows.Count).End(xlUp).Row
If ssheet.Cells(i, "A").Value = (Me.cboLs) Or ssheet.Cells(i, "A").Value = Val(Me.cboLs) Then
Me.txtProject = ssheet.Cells(i, "B").Value
End If
Next i
End Sub
Private Sub cmdadd_Click()
Dim e As Long
Dim Sheet1 As String
Worksheets(Sheet1).Activate 'Workbook-1 here i need to capture my userform data but it is going to workbook-2 on sheetname LS
'position cursor in the correct cell A6.
ActiveSheet.Range("A6").Select
e = 1 'set as the first ID
'if all the above are false (OK) then carry on.
'check to see the next available blank row start at cell A6...
Do Until ActiveCell.Value = Empty
ActiveCell.Offset(1, 0).Select 'move down 1 row
e = e + 1 'keep a count of the ID for later use
Loop
'Populate the new data values into the 'Data' worksheet.
ActiveCell.Value = e 'Next ID number
ActiveCell.Offset(0, 2).Value = Me.txtname.Text 'set col B
ActiveCell.Offset(0, 3).Value = Me.txtbook.Text 'set col C
ActiveCell.Offset(0, 1).Value = Me.cboLs.Text 'set col D
Me.txtname.Text = Empty
Me.txtbook.Text = Empty
Me.cboLs.Text = Empty
End Sub
在您的代碼中,我從未見過您設置Sheet1
字符串變量的值。
請注意,不需要激活工作表即可使用它。 同樣,不需要選擇單元格。 試試這樣的東西...
Private Sub cmdadd_Click()
Dim e As Long
Dim destSheet As Worksheet
Set destSheet = Worksheets("Sheet1")
ActiveSheet.Range("A6").Select
e = 1 'set as the first ID
'if all the above are false (OK) then carry on.
'check to see the next available blank row start at cell A6...
Do Until ActiveCell.Value = Empty
ActiveCell.Offset(1, 0).Select 'move down 1 row
e = e + 1 'keep a count of the ID for later use
Loop
'Populate the new data values into the 'Data' worksheet.
destSheet.Range("A6").Value = e 'Next ID number
destSheet.Range("B6").Value = Me.txtname.Text 'set col B
destSheet.Range("C6").Value = Me.txtbook.Text 'set col C
destSheet.Range("D6").Value = Me.cboLs.Text 'set col D
Me.txtname.Text = Empty
Me.txtbook.Text = Empty
Me.cboLs.Text = Empty
End Sub
同樣,對循環使用相同的方法以獲得所需的e
值。 順便說一句,如果您只是在查找A列中最后一個填充行的值,而不是循環(這樣做效率不高),則可以使用
destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Value
這與轉到A列底部的最后一個單元格並按CTRL + Up轉到最后一個填充的單元格相同。 然后,您可以將該值加1。
在子cmdadd_Click中,第二個工作簿仍處於活動狀態。 因此在Worksheets('Sheet1')。Activate之前添加:
Dim wb As Workbook
Dim ssheet As Worksheet
Set wb = Workbooks.Open("C:\Users\Desktop\Work.xlmx")
Set ssheet = wb.Worksheets("Sheet1")
就像您在其他潛艇中所做的一樣。 接下來,在工作表('sheet1')之前添加以下內容:
wb.activate
ssheet.activate
從您的子目錄中刪除這些行:
Dim Sheet1 As String
Worksheets(Sheet1).Activate
這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.