粘贴在特定范围内

时间:2016-11-01 21:51:40

标签: vba

enter image description here

查看下面的代码:

 With Worksheets("L.NAM.M")
        With .Cells.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False)
            .Parent.Range(.Offset(1), .Parent.Cells(Rows.Count, .Column).End(xlUp)).Copy _
            Destination:=Worksheets("NewForecast").Range("K" & Rows.Count).End(xlUp).Offset(1)
        End With
    End With

With Worksheets("L.EMEA.O")
    With .Cells.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)
        .Parent.Range(.Offset(1), .Parent.Cells(Rows.Count, .Column).End(xlUp)).Copy Destination:=Worksheets("NewForecast").Range("K" & Rows.Count).End(xlUp).Offset(1)
    End With
End With


      'Record Source

        With Worksheets("L.EMEO.O")
        With .Cells.Find(What:="Record Source", After:=ActiveCell, LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False)
            .Parent.Range(.Offset(1), .Parent.Cells(Rows.Count, .Column).End(xlUp)).Copy Destination:=Worksheets("NewForecast").Range("N" & Rows.Count).End(xlUp).Offset(1)
        End With
    End With

我在这里做什么......我正在获取这两个电子表格的数据预测_四分之一并粘贴在新电子表格的标题下方。因此,它正在寻找带有数据和粘贴在下面的lastrow。当从“L.EMEA.O”查找记录源时,技巧部分出现,因为“L.NAM.M”没有列L的任何数据,当我粘贴L.EMEA.O数据时它会混乱,因为它与L.EMEA.O不一致,但与L.NAM.O.我想要的是粘贴对齐粘贴了ForecastEM的L._EAEA.O的文本行。

有什么想法吗?我附上一张照片

enter image description here

在这种情况下,它粘贴在K114中,如果我运行另一块,我想要粘贴在N110中,而不是N114。

1 个答案:

答案 0 :(得分:0)

存储用于“L.EMEA.O”的行索引,并将其用于“L.EMEO.O”:

   With Worksheets("L.NAM.M")
           With .Cells.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
               xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
               xlNext, MatchCase:=False, SearchFormat:=False)
               .Parent.Range(.Offset(1), .Parent.Cells(Rows.Count, .Column).End(xlUp)).Copy _
               Destination:=Worksheets("NewForecast").Range("K" & Rows.Count).End(xlUp).Offset(1)
           End With
       End With

    Dim L_EMEA_ORow As Long '<--| variable to hold "forecast_quarter" sheet column "K" row index where to start pasting "L.EMEA.O" sheet data from
    With Worksheets("L.EMEA.O")
       With .Cells.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
           xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
           xlNext, MatchCase:=False, SearchFormat:=False)
           L_EMEA_ORow = Worksheets("NewForecast").Range("K" & Rows.Count).End(xlUp).Offset(1).Row '<--| store relevant row index where to start pasting data from
           .Parent.Range(.Offset(1), .Parent.Cells(Rows.Count, .Column).End(xlUp)).Copy Destination:=Worksheets("NewForecast").Range("K" & L_EMEA_ORow) '<--| use relevant row index
       End With
    End With


    'Record Source

    With Worksheets("L.EMEO.O")
        With .Cells.Find(What:="Record Source", After:=ActiveCell, LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False)
            .Parent.Range(.Offset(1), .Parent.Cells(Rows.Count, .Column).End(xlUp)).Copy Destination:=Worksheets("NewForecast").Range("N" & L_EMEA_ORow) '<--| use same relevant row index calculated above
        End With
    End With