VBA-命令按钮将TextBox值保存到其他工作簿中

时间:2018-10-01 14:59:31

标签: excel vba excel-vba

我需要一些帮助来创建代码,使我可以将输入到TextBox中的数据保存到另一个名为FoundData.xlsm-工作表FoundData的工作簿中

工作簿位置= FileShare-MFG (N:)/Manufacturing/PhysicalInventory/Tools

到目前为止,我已经能够使用CommandButton保存到同一文件的工作表中。

非常感谢您的投入和帮助。

下面是当前的VBA代码。

谢谢,

Private Sub cmdSave_Click() 
    Dim lRow As Long 
    Dim lPart As Long 
    Dim wb As Workbook 
    Set wb = Worksheets("FoundItems") 

    'find first empty row in database 
    lRow = wb.Cells.Find(What:="*", SearchOrder:=xlRows, _ 
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 

    With wb 
        ' .Unprotect Password:="password" 
        .Cells(lRow, 1).Value = Me.txtQuarter.Value 
        .Cells(lRow, 2).Value = Me.txtDate.Value 
        .Cells(lRow, 3).Value = Me.txtTSN.Value 
        .Cells(lRow, 4).Value = Me.txtPN.Value 
        .Cells(lRow, 5).Value = Me.txtQty.Value 
        .Cells(lRow, 6).Value = Me.txtLoc.Value 
        .Cells(lRow, 7).Value = Me.txtName.Value 
        .Cells(lRow, 8).Value = Me.txtComments.Value 
        ' .Protect Password:="password" 
    End With 

    'clear the data 
    Me.txtDate.Value = "" 
    Me.txtTSN.Value = "" 
    Me.txtPN.Value = "" 
    Me.txtQty.Value = "" 
    Me.txtLoc.Value = "" 
    Me.txtComments.Value = "" 
End Sub

1 个答案:

答案 0 :(得分:1)

如果工作簿已经打开,您可以引用它

x = '20/2020'
month, year = x.split('/')

请注意,您必须像以前一样使用工作表对象而不是工作簿对象。所以Dim Ws As Worksheet Set Ws = Workbooks("FoundData").Worksheets("FoundItems") 在这里。


如果关闭,则可以使用Workbooks.Open Method

打开它
Dim ws As Worksheet

,然后使用

引用工作表
Dim Wb As Workbook
Set Wb = Workbooks.Open(FileName:="N:\Manufacturing\PhysicalInventory\Tools\FoundData.xlsm")