复制列而不覆盖

时间:2015-04-10 15:58:15

标签: excel vba

Tengo 3 hojas de excel diferentes,llamadas:wsOrigin1,wsOrigin2,wsDestiny,Quiero usarestecódigo,para que busque y copie columnas completas,de wsOrigin1 y wsOrigin2 en wsDestiny,donde encuentre en primera fila de wsOrigin1 y wsOrigin2,la columna donde diga“aquí”,






返回首页,sobre la que copie primero de wsOrigin1。 este eselcódigoqueuso:

我有3张不同的excel表,名为:wsOrigin1,wsOrigin2,wsDestiny。我使用此代码来定位和复制wsDestin中的wsOrigin1和wsOrigin2的整列,它位于wsOrigin1和wsOrigin2的第一行,即“here”的列,并复制wsOrigin1列在wsDestin表中找到wsOrigin2的列下面的列wsOrigin1 ,但我遇到的问题是我做了这样做的代码,这就是写了你复制了wsOrigin2的列,wsOrigin1首先复制了。这是我使用的代码:

  For c = 1 To 13
      If wsOrigin1.Cells(1, c).Value = "here" Then
         For cc = cc + 1 To 13
         ' fila , columna

            With wsOrigin1.Columns(c).CurrentRegion
          wsDestiny.Columns(cc).End(xlDown) _
          .Offset(1, 0).Resize(.Rows.Count).End(xlDown) = .Columns(c).Value
       End With

          Exit For
          Next cc
     End If
   Next c


   For c2 = 1 To 13
      If wsOrigin2.Cells(1, c2).Value = "here" Then
         For cc2 = cc2 + 1 To 13
         ' fila , columna

            With wsOrigin2.Columns(c2).CurrentRegion
         wsDestiny.Columns(cc2).End(xlUp) _
          .Offset(1, 0).Resize(.Rows.Count) = .Columns(c2).End(xlUp).Value
       End With

          Exit For
          Next cc2
     End If
   Next c2

1 个答案:

答案 0 :(得分:0)

For c = 1 To 13
      If wsOrigin1.Cells(1, c).Value = "here" Then
         For cc = cc + 1 To 13

           With wsOrigin1.Columns(c).CurrentRegion
           wsDestiny.Cells(Rows.Count, cc).End(xlUp) _
           .Offset(1, 0).Resize(.Rows.Count) = .Columns(c).Value
           End With

           Exit For
          Next cc
     End If
   Next c


For c2 = 1 To 13
     If wsOrigin2.Cells(1, c2).Value = "here" Then
      For cc2 = cc2 + 1 To 13

       With wsOrigin2.Columns(c2).CurrentRegion
         wsDestiny.Cells(Rows.Count, cc2).End(xlUp) _
          .Offset(1, 0).Resize(.Rows.Count) = .Columns(c2).Value
         End With

         Exit For
        Next cc2
   End If
 Next c2   

示例结果:

wsOrigin1:

   A    B    C  
1 here      here   
2 bla  ble  bli
3 bla  ble  bli
4 bla  ble  bli

wsOrgin2:

   A
1 here
2 blo
3 blo
4 blo

Result
code compile

wsDestiny:

   A    B      
1 here here   
2 bla  bli
3 bla  bli
4 bla  bli
5 here
6 blo
7 blo
8 blo