EXCEL将数据保存到其他工作簿中-VBA [英] EXCEL Save data into different workbook - VBA

查看:512
本文介绍了EXCEL将数据保存到其他工作簿中-VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我有一个工作表,要求用户输入数据,然后单击一个按钮以将数据保存到数据库中.该数据库当前位于同一工作簿不同的工作表"中.

I have a sheet that requests a user to enter data and then click a button to save the data into the database. The database is currently located in the same workbook "Different worksheet".

我基本上需要将此数据保存在另一个名为"Consolidated.xlsx"的工作表上,该工作表位于其他文件夹"C:/reports/consolidates.xlsx"中,以便只有我可以访问此文件数据,没有其他人

What i basically need is for this data to be saved on a different worksheet that i call "Consolidated.xlsx" which is available in a different folder "C:/reports/consolidates.xlsx" so that only I can access this data and no one else

请让我知道您能否提供帮助

Please let me know if you can help

当前可通过以下链接获得的文档: www.dropbox.com/s/3wea245lmek8hef/FormSheet.xls

Document currently available under the following link: www.dropbox.com/s/3wea245lmek8hef/FormSheet.xls

谢谢

推荐答案

编辑4/19:下面的代码已更新,可以写入本地"PartsData"工作表以及合并的目标...您仍然需要确保您的合并"文件上有一个名为"PartsData"的工作表:

EDIT 4/19: the code below has been updated to write to the local "PartsData" sheet as well as the consolidated target... You still need to make sure there is a sheet on your "consolidated" file that is named "PartsData":

Option Explicit
Sub UpdateLogWorksheet()

Dim historyWks As Worksheet, localWks As Worksheet, _
    inputWks As Worksheet, indexWks As Worksheet
Dim historyWb As Workbook
Dim MyWorksheets As New Collection
Dim nextRow As Long, oCol As Long
Dim myRng As Range, myCell As Range
Dim myCopy As String

'cells to copy from Input sheet - some contain formulas
myCopy = "D5,D7,D9,D11,D13"

'assign variables for easy reference
Set inputWks = ThisWorkbook.Worksheets("Input")
Set localWks = ThisWorkbook.Worksheets("PartsData")
Set historyWb = Workbooks.Open("C:\reports\consolidated.xlsx")
Set historyWks = historyWb.Worksheets("PartsData")

'put both target worksheets into a collection for an easy loop
MyWorksheets.Add Item:=localWks
MyWorksheets.Add Item:=historyWks

With historyWks
    nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With

With inputWks
    Set myRng = .Range(myCopy)
    If Application.CountA(myRng) <> myRng.Cells.Count Then
        MsgBox "Please fill in all the cells!"
        Exit Sub
    End If
End With

'write results of form to both local parts data and consolidated parts data
For Each indexWks In MyWorksheets
    With indexWks
        With .Cells(nextRow, "A")
            .Value = Now
            .NumberFormat = "mm/dd/yyyy hh:mm:ss"
        End With
        .Cells(nextRow, "B").Value = Application.UserName
        oCol = 3
        For Each myCell In myRng.Cells
            indexWks.Cells(nextRow, oCol).Value = myCell.Value
            oCol = oCol + 1
        Next myCell
    End With
Next indexWks

historyWb.Save '<~ save and close the target workbook
historyWb.Close SaveChanges:=False

'clear input cells that contain constants
With inputWks
  On Error Resume Next
     With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
          .ClearContents
          Application.GoTo .Cells(1) ', Scroll:=True
     End With
  On Error GoTo 0
End With
End Sub

这篇关于EXCEL将数据保存到其他工作簿中-VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆