使用动态FileName将数据从工作簿1传输到工作簿2

时间:2017-11-07 03:43:52

标签: excel vba

我需要你的帮助。下面的代码将从Workbook1中提取数据到Workbook2中的特定单元格。目前,如果我对Workbook1的文件名进行硬编码,我就会努力工作。这是代码:

Sub PopulateFields()

Workbooks("BUPA_LHC_Reconciliation_20171018204045.xlsx").Activate
Workbooks("BUPA_LHC_Reconciliation_20171018204045.xlsx").Worksheets   ("Unconfirmed").Activate

Dim cell_count As Integer
Dim twbrow_number As Integer
Dim owbrow_number As Integer
Dim owb2row_number As Integer
Dim OWBLastRow As Integer
Dim TWB As Workbook 'This workbook
Dim OWB As Workbook 'Other workbook
Dim TWBWS As Worksheet 'Data
Dim OWBWS As Worksheet 'Report worksheet, Unconfirmed
Dim OWBWS2 As Worksheet 'Report worksheet, Error

owbrow_number = 8
twbrow_number = 1
owb2row_number = 8

Set TWB = Workbooks("LHC Report Email test v1.1 (IN TESTING).xlsm")
Set OWB = Workbooks("BUPA_LHC_Reconciliation_20171018204045.xlsx")
Set TWBWS = TWB.Worksheets("Sheet1")
Set OWBWS = OWB.Worksheets("Unconfirmed")
Set OWBWS2 = OWB.Worksheets("Errors")

cell_count = 0
OWBLastRow = OWBWS.Cells(OWBWS.Rows.Count, "A").End(xlUp).Row - 2
OWBLastRow2 = OWBWS2.Cells(OWBWS2.Rows.Count, "A").End(xlUp).Row - 2


Do
DoEvents

cell_count = cell_count + 1
owbrow_number = owbrow_number + 1
twbrow_number = twbrownumber + 1

TWBWS.Cells(2, "A").Resize(OWBLastRow - 8, 1).Value = OWBWS.Cells(9, "A").Resize(OWBLastRow - 8, 1).Value
TWBWS.Cells(2, "B").Resize(OWBLastRow - 8, 1).Value = "LHC Unconfirmed"
TWBWS.Cells(2, "C").Resize(OWBLastRow - 8, 1).Value = "Please refer to link below to process"
TWBWS.Cells(2, "E").Resize(OWBLastRow - 8, 1).Value = OWBWS.Cells(9, "C").Resize(OWBLastRow - 8, 1).Value
TWBWS.Cells(2, "G").Resize(OWBLastRow - 8, 1).Value = OWBWS.Cells(9, "B").Resize(OWBLastRow - 8, 1).Value

Loop Until owbrow_number = OWBLastRow

twb2row_number = twb_rownumber

Do
DoEvents

owb2row_number = owb2row_number + 1
twb2row_number = twb2rownumber + 1

TWBWS.Cells(cell_count, "A").Resize(OWBLastRow2 - 8, 1).Value = OWBWS2.Cells(9, "A").Resize(OWBLastRow2 - 8, 1).Value
TWBWS.Cells(cell_count, "B").Resize(OWBLastRow2 - 8, 1).Value = "LHC Certificate Errors"
TWBWS.Cells(cell_count, "C").Resize(OWBLastRow2 - 8, 1).Value = "Please refer to link below to process"
TWBWS.Cells(cell_count, "E").Resize(OWBLastRow2 - 8, 1).Value = OWBWS2.Cells(9, "C").Resize(OWBLastRow2 - 8, 1).Value
TWBWS.Cells(cell_count, "G").Resize(OWBLastRow2 - 8, 1).Value = OWBWS2.Cells(9, "B").Resize(OWBLastRow2 - 8, 1).Value

Loop Until owb2row_number = OWBLastRow2

End Sub

我现在已经创建了一段代码,它将有一个打开文件对话框,将所选文件的名称存储到一个名为'FileName'的变量中但是在将此变量实现到我的PopulateFields函数中时遇到了麻烦,因此它将使用选择文件名而不是我硬编码的文件名。我需要这样做,因为文件名一直在变化,我不必一直更改文件名。

这是我用来获取文件名的代码:

Sub SelectFile()

Dim f As String, OpenMsg As String, FileName As String

f = Application.GetOpenFilename _
(Title:="Please choose a file to open", _
FileFilter:="Excel Files *.xls* (*.xls*),")

'Exit if cancel is selected
If f = "False" Then Exit Sub

'Find Filename
FileName = Dir(f, vbDirectory)

MsgBox FileName

现在我尝试用变量FileName替换硬编码文件名,但它不起作用......这就是我尝试过的:

Workbooks(FileName).Activate
Workbooks(FileName).Worksheets("Unconfirmed").Activate

Set OWB = Workbooks(FileName)

我得到的错误是'运行时错误'9':下标超出范围。'

有人可以帮忙吗?

谢谢, 荣

0 个答案:

没有答案