我有一个主工作簿和一个子工作簿,宏命令可以打开
1)主要工作簿=数据库
主要工作簿中的工作表=客户数据库
子工作簿=订单
这两本书可供宏观运行。
1)如果来自Range C:F
的{{1}}包含Orders
Customer Database B:E
从Cell B
导入Orders
2)数据库中的工作表已包含数据。因此,我希望它添加到New Member
请帮忙。
Database
答案 0 :(得分:0)
此答案基于您在上面列举的第1项和第2项
我不确定你想用.Replace
方法完成什么,所以我将其删除。
Sub Consolidate()
Dim wsSource As Worksheet
Dim wbSource As Workbook
Dim wsTarget As Worksheet
Dim wbTarget As Workbook
Dim findRange As Range
Dim lastline As Integer
Dim file As Variant '~~> declared as Variant
Set wbSource = Thisworkbook
Set wsSource = wbSource.Sheets(1) '~~>use index only if you have 1 sheet only
file = Application.GetOpenFilename("Excel Files, *.xlsx") '~~> allows you to select the file to load
If file <> False Then
Set wbTarget = Workbooks.Open(file)
Set wsTarget = wbTarget.Sheets(1)
With wsTarget
lastline = .Range("B" & .Rows.Count).End(xlUp).Row
Set findRange = .Range("B1:F" & lastline)
With findRange
.Autofilter 1, "New Member"
.Offset(1,1).Resize(.Rows.Count-1, .Columns.Count-1).SpecialCells(xlCellTypeVisible).Copy _
wsSource.Range("B" & wsSource.Rows.Count).End(xlUp).Offset(1,0)
End With
End With
Else
MsgBox "No file selected. Exiting now." : Exit Sub
End If
wbTarget.Close False
wbSource.Save
End Sub
无需遍历整个范围。
我改用了.AutoFilter
方法
希望这接近你想要的。