添加一列说明数据的工作表来源

时间:2019-06-20 07:03:19

标签: excel vba

程序的主要功能是复制打开的工作表中的所有数据,并将其放入名为“合并”的工作表中。但是,一切工作正常,我想添加一列“数据源”,其中可以说明数据的来源(例如Sheet1,Sheet2)。但事实是,它输出了错误的工作表名称。

我尝试了不同的代码,并附上了以下当前可用的代码。我将突出显示我认为问题来自的部分。我真的很感谢您的帮助,因为我自己不是编码人员(一周前才刚刚学习)。

  For Each wksSrc In ThisWorkbook.Worksheets

       'Skip Destination worksheet
        If wksSrc.Name <> wksDst.Name And wksSrc.Name <> "Tool" Then
            With wksSrc

                'Identify the last row and column on this sheet
                'so we know when to stop looping through the data
                lngLastSrcRowNum = LastOccupiedRowNum(wksSrc)
                lngLastSrcColNum = LastOccupiedColNum(wksSrc)

                'Identify the last row of the Destination sheet
                'so we know where to (eventually) paste the data
                lngLastDstRowNum = LastOccupiedRowNum(wksDst)

                'Loop through the headers on this sheet, looking up
                'the appropriate Destination column from the Final
                'Headers dictionary and creating ranges on the fly

                For lngIdx = 1 To lngLastSrcColNum

                    strColHeader = Trim(CStr(.Cells(1, lngIdx)))

                    'Set the Destination target range using the
                    'looked up value from the Final Headers dictionary
                    Set rngDst = wksDst.Cells(lngLastDstRowNum + 1, _
                                              dicFinalHeaders(strColHeader))

                    'Set the source target range using the current
                    'column number and the last-occupied row
                    Set rngSrc = .Range(.Cells(2, lngIdx), _
                                        .Cells(lngLastSrcRowNum, lngIdx))

                    'Copy the data from this sheet to the destination
                    rngSrc.Copy Destination:=rngDst

                Next lngIdx

                Dim TargetColumn As Long
                Dim FinalRow As Long
                Dim rngAddress As Range
                Dim i As Long


                With wksDst
                    FinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                    Set rngAddress = .Range("A:Z").Find("Data Source")

                     If Not rngAddress Is Nothing Then
                        TargetColumn = rngAddress.Column 

           ''''THIS IS THE PART WHERE I ASSUME THE PROBLEM COMES FROM'''''
                            For i = 1 To FinalRow
                            .Cells(i, TargetColumn).Value = wksSrc.Name
                            Next i

                     End If

                 End With

            End With
        End If

    Next wksSrc

End Sub

我已经添加了“数据源”列,但是代码中的wksSrc.Name部分始终应该仅输出最后一个工作表名称,这取决于我从何处获取数据。

这可能会有所帮助。当我将i值设置为1时,它将输出正确的工作表名称,但是,它将列标题替换为工作表名称,并且循环仅执行一次。当我将i值设置为2时,它会输出错误的工作表名称,但会开始在“数据源”列的正确的第一空白行中输出。

1 个答案:

答案 0 :(得分:0)

如评论中所述。您每次都在“整个源名称”列中循环。这样,在过程结束时,您将获得最后一个工作表的循环。因此,为了克服这个问题,您需要一个StartRow和一个FinalRow才能循环遍历该工作表中的数据。参见下面的代码(未经测试),但是我认为您会了解如何实现,并且可能会立即起作用。

For Each wksSrc In ThisWorkbook.Worksheets

       'Skip Destination worksheet
        If wksSrc.Name <> wksDst.Name And wksSrc.Name <> "Tool" Then
            With wksSrc

                'Identify the last row and column on this sheet
                'so we know when to stop looping through the data
                lngLastSrcRowNum = LastOccupiedRowNum(wksSrc)
                lngLastSrcColNum = LastOccupiedColNum(wksSrc)

                'Identify the last row of the Destination sheet
                'so we know where to (eventually) paste the data
                lngLastDstRowNum = LastOccupiedRowNum(wksDst)

                'Loop through the headers on this sheet, looking up
                'the appropriate Destination column from the Final
                'Headers dictionary and creating ranges on the fly

                For lngIdx = 1 To lngLastSrcColNum

                    strColHeader = Trim(CStr(.Cells(1, lngIdx)))

                    'Set the Destination target range using the
                    'looked up value from the Final Headers dictionary
                    Set rngDst = wksDst.Cells(lngLastDstRowNum + 1, _
                                              dicFinalHeaders(strColHeader))

                    'Set the source target range using the current
                    'column number and the last-occupied row
                    Set rngSrc = .Range(.Cells(2, lngIdx), _
                                        .Cells(lngLastSrcRowNum, lngIdx))

                    'Copy the data from this sheet to the destination
                    rngSrc.Copy Destination:=rngDst

                Next lngIdx

                Dim TargetColumn As Long
                Dim FinalRow As Long, StartRow As Long
                Dim rngAddress As Range
                Dim i As Long


                With wksDst

                    Set rngAddress = .Range("A:Z").Find("Data Source")

                     If Not rngAddress Is Nothing Then
                        TargetColumn = rngAddress.Column
                        'set the start for this sheet
                        StartRow = lngLastDstRowNum + 1
                        'set the final row for this worksheet
                        FinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row

                        .Range(.Cells(StartRow, TargetColumn), .Cells(FinalRow, TargetColumn)).Value = wksSrc.Name
                     End If

                 End With

            End With
        End If

    Next wksSrc

可以看出,在wksDst上进行范围调整时应考虑到图纸数据。