如何指定要将数据复制到不同Excel工作表中的行。

huangapple go评论48阅读模式
英文:

How to specify rows for data to copy into for separate excel sheets

问题

我想将一个Excel表格中的数据每隔一行复制到另一个单独的表格中。例如,第一行要复制到的单元格将是B2,一直到E2。接下来的一行需要复制到B8到E8,然后从B14到E14。我如何指定要复制到这些特定行的数据并跳过其他行?

在VB中,我能够将数据复制过去,但无法指定确切的单元格。

Sub CopyData()
  ' 定义要复制的数据范围
  Dim dataRange As Range
  Set dataRange = Range("A1:E1")

  ' 定义要复制数据的起始行和间隔
  Dim startRow As Integer
  Dim interval As Integer
  startRow = 2
  interval = 6

  ' 循环遍历每一行
  For i = startRow To 1000
    ' 检查当前行是否是要复制数据的行
    If i Mod interval = 0 Then
      ' 将数据从数据范围复制到当前行
      dataRange.Copy Cells(i, 2)
    End If
  Next i
End Sub
英文:

I would like the data from one excel sheet to copy over every other row into another separate sheet. For instance, the first line to be copied into would be cell B2 all the upto E2. The next line needs to be copied into B8 up to E8 and then after that from B14 to E14. How can I specify the data to copy into those specific rows and skip the others?

In VB I was able to get the data to copy over but cannot specify the exact cells.

Sub CopyData()
  ' Define the range of data that you want to copy
  Dim dataRange As Range
  Set dataRange = Range("A1:E1")

  ' Define the starting row and the interval at which you want to copy data
  Dim startRow As Integer
  Dim interval As Integer
  startRow = 2
  interval = 6

  ' Loop through each row
  For i = startRow To 1000
    ' Check if the current row is a row that you want to copy data into
    If i Mod interval = 0 Then
      ' Copy the data from the data range to the current row
      dataRange.Copy Cells(i, 2)
    End If
  Next i
End Sub

答案1

得分: 2

复制单行范围多次并带有偏移:

<!-- 语言: lang-vb -->

    Sub CopyRow()
        
        Const SRC_ROW As String = "A1:E1"
        Const DST_FIRST_CELL As String = "B2"
        Const DST_ROW_OFFSET As Long = 6
        Const DST_COPY_COUNT As Long = 3 ' 166
        
        Dim ws As Worksheet: Set ws = ActiveSheet ' 改进!
        
        Dim srg As Range: Set srg = ws.Range(SRC_ROW)
        Dim dCell As Range: Set dCell = ws.Range(DST_FIRST_CELL)
        
        Dim n As Long
        
        For n = 1 To DST_COPY_COUNT
            srg.Copy dCell
            Set dCell = dCell.Offset(DST_ROW_OFFSET)
        Next n
          
    End Sub
英文:

Copy a Single-Row Range Multiple Times With an Offset

如何指定要将数据复制到不同Excel工作表中的行。

<!-- language: lang-vb -->

Sub CopyRow()
    
    Const SRC_ROW As String = &quot;A1:E1&quot;
    Const DST_FIRST_CELL As String = &quot;B2&quot;
    Const DST_ROW_OFFSET As Long = 6
    Const DST_COPY_COUNT As Long = 3 &#39; 166
    
    Dim ws As Worksheet: Set ws = ActiveSheet &#39; improve!
    
    Dim srg As Range: Set srg = ws.Range(SRC_ROW)
    Dim dCell As Range: Set dCell = ws.Range(DST_FIRST_CELL)
    
    Dim n As Long
    
    For n = 1 To DST_COPY_COUNT
        srg.Copy dCell
        Set dCell = dCell.Offset(DST_ROW_OFFSET)
    Next n
  
End Sub

答案2

得分: 0

当你使用For...Next循环时,你可以指定变量在循环之间递增的数量。默认值为1,但如果你想让循环跳到第6个下一行,你可以简单地指定For i = a to b Step 6

Sub CopyData()
  ' 定义要复制的数据范围
  Dim dataRange As Range
  Set dataRange = Range("A1:E1")

  ' 定义起始行和复制数据的间隔
  Dim startRow As Integer
  Dim interval As Integer
  startRow = 2
  interval = 6

  ' 循环遍历每一行
  For i = startRow To 1000 Step interval
      ' 从数据范围复制数据到当前行
      dataRange.Copy Cells(i, 2)
  Next i
End Sub
英文:

When you do a For...Next loop you can specify how much the variable will increment between loops. The default is 1, but if you want your loop to skip to the 6th next row, you can simply specify For i = a to b Step 6

Sub CopyData()
  &#39; Define the range of data that you want to copy
  Dim dataRange As Range
  Set dataRange = Range(&quot;A1:E1&quot;)

  &#39; Define the starting row and the interval at which you want to copy data
  Dim startRow As Integer
  Dim interval As Integer
  startRow = 2
  interval = 6

  &#39; Loop through each row
  For i = startRow To 1000 Step interval
      &#39; Copy the data from the data range to the current row
      dataRange.Copy Cells(i, 2)
  Next i
End Sub

huangapple
  • 本文由 发表于 2023年6月13日 03:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定