[英]How to capture data to a specified excel sheet(first workbook) from two different workbooks from the Userform?
I have workbook-1 where the data should be actually captured from the userform when add button is clicked. 我有workbook-1,单击添加按钮时,实际上应该从用户窗体捕获数据。
In workbook-2 I just have my combobox list inorder to display the excel data when selected from the comobox and textbox automatically in the userform. 在workbook-2中,我只有组合框列表,以便在用户窗体中从comobox和textbox中自动选择时显示excel数据。
But now I am facing a problem, when I fill the userform by selecting all combobox list and filling other data manually then clicked on add button the data is trasferring to my Workbook-2 (below my combobox list). 但是现在我遇到了一个问题,当我通过选择所有组合框列表并手动填充其他数据来填充用户表单,然后单击添加按钮时,数据将传输到我的Workbook-2(在我的组合框列表下方)。
How to capture the userform data to my workbook-1 on Sheetname "Sheet1". 如何将用户窗体数据捕获到工作表工作表1“ Sheet1”上。
My Workbook-2 path is "C:\\Users\\Desktop\\Work.xlmx", will I need to include this path also for the commandbutton? 我的Workbook-2路径是“ C:\\ Users \\ Desktop \\ Work.xlmx”,我是否还需要在命令按钮中包含该路径?
Below is my code of combox and add commandbutton: 下面是我的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
In your code, I never see you setting the value of the Sheet1
string variable. 在您的代码中,我从未见过您设置
Sheet1
字符串变量的值。
Please note it is not required to activate a worksheet in order to work with it. 请注意,不需要激活工作表即可使用它。 Likewise, it is not required a select a cell.
同样,不需要选择单元格。 Try something like this...
试试这样的东西...
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
Likewise, use the same approach for your loop to get your desired value of e
. 同样,对循环使用相同的方法以获得所需的
e
值。 By the way, if you are just looking for the value of the last populated row in column A, instead of looping (which is inefficient for this purpose), you can do use 顺便说一句,如果您只是在查找A列中最后一个填充行的值,而不是循环(这样做效率不高),则可以使用
destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Value
This is the same as going to the last cell at the bottom of column A and pressing CTRL+Up to go to the last populated cell. 这与转到A列底部的最后一个单元格并按CTRL + Up转到最后一个填充的单元格相同。 Then you can just add 1 to that value.
然后,您可以将该值加1。
In the sub cmdadd_Click, the second workbook is still active. 在子cmdadd_Click中,第二个工作簿仍处于活动状态。 Therefore before Worksheets('Sheet1').Activate add:
因此在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")
Like you did in the other subs. 就像您在其他潜艇中所做的一样。 Next add the following before worksheets('sheet1”):
接下来,在工作表('sheet1')之前添加以下内容:
wb.activate
ssheet.activate
Delete these lines from your sub: 从您的子目录中删除这些行:
Dim Sheet1 As String
Worksheets(Sheet1).Activate
This should do the trick. 这应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.