特定工作表中特定行中的数据传输

时间:2018-07-09 05:15:42

标签: excel vba excel-vba

需要您的帮助和专业知识来解决我的问题。

我有一个带有vba代码的报告,使它能够根据“列表”表中输入的路径提取报告。但是,它不会根据我指定的位置读取应该在哪个确切的单元格中输入。

预期: 由于报表具有自己的标题,因此将在“ MasterData”表中输入的数据应从第1行开始。

将在“第二页”和“第三页”中输入的数据应从第2行开始,因为我将是创建标头的人。从第2行开始,这已经是正确的。

在我的文件“列表”表中,有报告的单元格位置,将其粘贴到该位置,但是即使我将其定位到预期的单元格中,代码也不会跟随它。

以下是vba:

Public strFileName As String
Public currentWB As Workbook
Public dataWB As Workbook
Public strCopyRange As String

Sub GetData()
    Dim strWhereToCopy As String, strStartCellColName As String
    Dim strListSheet As String

strListSheet = "List"

On Error GoTo ErrH
Sheets(strListSheet).Select
Range("B2").Select

'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
Set currentWB = ActiveWorkbook
Do While ActiveCell.Value <> ""

    strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
    strCopyRange = ActiveCell.Offset(0, 2) & ":" & ActiveCell.Offset(0, 3)
    strWhereToCopy = ActiveCell.Offset(0, 4).Value
    strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1)

    Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=False
    Set dataWB = ActiveWorkbook

    Range(strCopyRange).Select
    Selection.Copy

    currentWB.Activate
    Sheets(strWhereToCopy).Select
    lastRow = LastRowInOneColumn(strStartCellColName)
    Cells(lastRow + 1, 1).Select

    Selection.PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
    Application.CutCopyMode = False
    dataWB.Close False
    Sheets("List").Select
    ActiveCell.Offset(1, 0).Select


Loop

Exit Sub

ErrH:
    MsgBox "It seems some file was missing. The data copy operation is not complete."
    Exit Sub
End Sub

Public Function LastRowInOneColumn(col)
    Dim lastRow As Long
    With ActiveSheet
    lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With
    LastRowInOneColumn = lastRow
End Function

请查看所附的屏幕截图:

列表:

List Sheet

MasterData工作表:

MasterData Sheet

第二张纸-正确的行:

2nd Sheet

谢谢。

0 个答案:

没有答案