首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >VBA复制错误,将多个工作表导入中央工作簿数据库

VBA复制错误,将多个工作表导入中央工作簿数据库
EN

Stack Overflow用户
提问于 2018-06-02 02:50:32
回答 1查看 93关注 0票数 0

我正在尝试创建一个集中式数据库,将多个工作簿中相同的选项卡(名为"Import")导入到不同工作簿上的某个选项卡中。我是VBA的新手,并且在这里VBA Import multiple sheets into Workbook和这里https://danwagner.co/how-to-combine-data-from-multiple-sheets-into-a-single-sheet/修改别人的代码。

当代码试图从源工作表上的"Import“选项卡复制到目标工作表上的"Data”选项卡时,我遇到了运行时错误91 (参见以下代码):

代码语言:javascript
复制
rngSrcCountry.Copy Destination:=rngDstDatabase

关于如何改进代码以有效地将“导入”选项卡从多个工作簿复制到另一个工作簿上的“数据”选项卡,有什么建议吗?提前感谢您的帮助!

代码语言:javascript
复制
Sub InsertDatabase()

Dim FileNames As Variant 'Group of files to be looped through
Dim FileName As Variant 'Country of focus (file open)
Dim ActiveCountryWB As Workbook 'Active workbook of country
Dim wksSrcCountry As Worksheet 'Import worksheet in country
Dim wksDstDatabase As Worksheet 'Data worksheet in database
Dim rngSrcCountry As Range 'Range of data in import worksheet
Dim rngDstDatabase As Range 'Range of data in data worksheet in database
Dim lngSrcLastRow As Long
Dim lngDstLastRow As Long

'Set destination reference
Set wksDstDatabase = ThisWorkbook.Worksheets("Data")

MsgBox "In the following browser, please choose the Excel file(s) you want     
to copy data from"
FileNames = Application.GetOpenFilename _
(Title:="Please choose the files you want to copy data FROM", _
FileFilter:="All Files (*.*),*.*", _
MultiSelect:=True)

If VarType(CountriesGroup) = vbBoolean Then
    If Not CountriesGroup Then Exit Sub
End If

'Set initial destination range
Set rngDstDatabase = wksDstDatabase.Cells(lngDstLastRow + 1, 1)

'Loop over all files selected by user, and import the desired "Import" sheet
For Each FileName In FileNames

'Set country workbook references
Set ActiveCountryWB = Workbooks.Open(FileName)
Set wksSrcCountry = ActiveCountryWB.Sheets("Import")

'Identify last occupied row on import sheet
lngSrcLastRow = LastOccupiedRowNum(wksSrcCountry)

'Store source data
With wksSrcCountry
    Set rngSrcCountry = .Range(.Cells(1, 1), .Cells(lngSrcLastRow, 20))
    rngSrcCountry.Copy Destination:=rngDstDatabase
End With

'Redefine destination range now that new data has been added
lngDstLastRow = LastOccupiedRowNum(wksDstDatabase)
Set rngDstDatabase = wksDstDatabase.Cells(lngDstLawRow + 1, 3)

Next FileName

End Sub

Public Function LastOccupiedRowNum(Sheet As Worksheet) As Long
Dim lng As Long

If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
    With Sheet

       lng = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlValues, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
    End With
Else
    lng = 1
End If
LastOccupiedRowNum = lng
End Function

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 03:42:54

在定义变量之前,您的代码正在使用变量。下面这行代码必须位于试图引用" lngDstLastRow“的任何其他代码之上,否则,它将不知道什么是lngDstLastRow(它默认为空)

lngDstLastRow = LastOccupiedRowNum(wksDstDatabase)

具体地说,上行需要放在以下行的上方:

集合rngDstDatabase = wksDstDatabase.Cells(lngDstLastRow + 1,1)

否则,您将一个未定义的变量添加到另一个变量中,这意味着您的代码将转换为: Set rngDstDatabase = wksDstDatabase.Cells( nothing + 1,1)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50649471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档