Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Microsoft Excel 2010复制/粘贴编辑理念&;关于次要细节的帮助请求_Excel_Vba - Fatal编程技术网

Microsoft Excel 2010复制/粘贴编辑理念&;关于次要细节的帮助请求

Microsoft Excel 2010复制/粘贴编辑理念&;关于次要细节的帮助请求,excel,vba,Excel,Vba,是的,我在其他人的帮助下为Microsoft Excel(2010,如果有帮助的话)编写了这个宏。我想知道是否有人有办法缩短它,使它更有效。然而,还是得到了与以前相同的结果?我必须使用的CSV格式的示例可以在这里找到。。。是的,不幸的是,它们必须被放入那些列和单元格中 我唯一真正困惑的问题是: 例如,在.Cell(2,3)上。。。如果您注意到,它将复制并粘贴的每个部分都有一个新行。。我想让它那样做。。我是这方面的新手,无法找到一种方法让它只将每一行粘贴到下一行。。所以我的解决方案是做2,3,4,

是的,我在其他人的帮助下为Microsoft Excel(2010,如果有帮助的话)编写了这个宏。我想知道是否有人有办法缩短它,使它更有效。然而,还是得到了与以前相同的结果?我必须使用的CSV格式的示例可以在这里找到。。。是的,不幸的是,它们必须被放入那些列和单元格中

我唯一真正困惑的问题是: 例如,在
.Cell(2,3)
上。。。如果您注意到,它将复制并粘贴的每个部分都有一个新行。。我想让它那样做。。我是这方面的新手,无法找到一种方法让它只将每一行粘贴到下一行。。所以我的解决方案是做2,3,4,5。。等等如果有人知道如何改变这一点,也可以使这一点。。环照我说,那将是很大的帮助。不过,只需循环复制的数据量,而不是重复

这里有一个例子:它是干净的,我保证。谢谢你抽出时间

宏代码,用于将某些列/行数据单元格从一张图纸复制到另一张图纸,并复制到特定单元格

Sub FormatData()
Dim col As Integer

For col = 1 To 1
    With Worksheets(2)
       .Cells(2, 2) = Cells(1, col)
       .Cells(2, 3) = Cells(2, col) & ". " & Cells(3, col) & ". " & Cells(4, col) & ". " & Cells(5, col) & "."
       .Cells(2, 4) = Cells(7, col)
       .Cells(2, 5) = Cells(10, col)
    End With
Next col
    For col = 2 To 2
    With Worksheets(2)
       .Cells(3, 2) = Cells(1, col)
       .Cells(3, 3) = Cells(2, col) & ". " & Cells(3, col) & ". " & Cells(4, col) & ". " & Cells(5, col) & "."
       .Cells(3, 4) = Cells(7, col)
       .Cells(3, 5) = Cells(10, col)
    End With
Next col
    For col = 3 To 3
    With Worksheets(2)
       .Cells(4, 2) = Cells(1, col)
       .Cells(4, 3) = Cells(2, col) & ". " & Cells(3, col) & ". " & Cells(4, col) & ". " & Cells(5, col) & "."
       .Cells(4, 4) = Cells(7, col)
       .Cells(4, 5) = Cells(10, col)
    End With
Next col
    For col = 4 To 4
    With Worksheets(2)
       .Cells(5, 2) = Cells(1, col)
       .Cells(5, 3) = Cells(2, col) & ". " & Cells(3, col) & ". " & Cells(4, col) & ". " & Cells(5, col) & "."
       .Cells(5, 4) = Cells(7, col)
       .Cells(5, 5) = Cells(10, col)
    End With
Next col

    For col = 1 To 1
    With Worksheets(2)
       .Cells(6, 2) = Cells(13, col)
       .Cells(6, 3) = Cells(14, col) & ". " & Cells(15, col) & ". " & Cells(16, col) & ". " & Cells(17, col) & "."
       .Cells(6, 4) = Cells(19, col)
       .Cells(6, 5) = Cells(22, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(7, 2) = Cells(13, col)
       .Cells(7, 3) = Cells(14, col) & ". " & Cells(15, col) & ". " & Cells(16, col) & ". " & Cells(17, col) & "."
       .Cells(7, 4) = Cells(19, col)
       .Cells(7, 5) = Cells(22, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(8, 2) = Cells(13, col)
       .Cells(8, 3) = Cells(14, col) & ". " & Cells(15, col) & ". " & Cells(16, col) & ". " & Cells(17, col) & "."
       .Cells(8, 4) = Cells(19, col)
       .Cells(8, 5) = Cells(22, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(9, 2) = Cells(13, col)
       .Cells(9, 3) = Cells(14, col) & ". " & Cells(15, col) & ". " & Cells(16, col) & ". " & Cells(17, col) & "."
       .Cells(9, 4) = Cells(19, col)
       .Cells(9, 5) = Cells(22, col)
    End With
Next col
        For col = 1 To 1
    With Worksheets(2)
       .Cells(10, 2) = Cells(25, col)
       .Cells(10, 3) = Cells(26, col) & ". " & Cells(27, col) & ". " & Cells(28, col) & ". " & Cells(29, col) & "."
       .Cells(10, 4) = Cells(31, col)
       .Cells(10, 5) = Cells(34, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(11, 2) = Cells(25, col)
       .Cells(11, 3) = Cells(26, col) & ". " & Cells(27, col) & ". " & Cells(28, col) & ". " & Cells(29, col) & "."
       .Cells(11, 4) = Cells(31, col)
       .Cells(11, 5) = Cells(34, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(12, 2) = Cells(25, col)
       .Cells(12, 3) = Cells(26, col) & ". " & Cells(27, col) & ". " & Cells(28, col) & ". " & Cells(29, col) & "."
       .Cells(12, 4) = Cells(31, col)
       .Cells(12, 5) = Cells(34, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(13, 2) = Cells(25, col)
       .Cells(13, 3) = Cells(26, col) & ". " & Cells(27, col) & ". " & Cells(28, col) & ". " & Cells(29, col) & "."
       .Cells(13, 4) = Cells(31, col)
       .Cells(13, 5) = Cells(34, col)
    End With
Next col
        For col = 1 To 1
    With Worksheets(2)
       .Cells(14, 2) = Cells(37, col)
       .Cells(14, 3) = Cells(38, col) & ". " & Cells(39, col) & ". " & Cells(40, col) & ". " & Cells(41, col) & "."
       .Cells(14, 4) = Cells(43, col)
       .Cells(14, 5) = Cells(46, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(15, 2) = Cells(37, col)
       .Cells(15, 3) = Cells(38, col) & ". " & Cells(39, col) & ". " & Cells(40, col) & ". " & Cells(41, col) & "."
       .Cells(15, 4) = Cells(43, col)
       .Cells(15, 5) = Cells(46, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(16, 2) = Cells(37, col)
       .Cells(16, 3) = Cells(38, col) & ". " & Cells(39, col) & ". " & Cells(40, col) & ". " & Cells(41, col) & "."
       .Cells(16, 4) = Cells(43, col)
       .Cells(16, 5) = Cells(46, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(17, 2) = Cells(37, col)
       .Cells(17, 3) = Cells(38, col) & ". " & Cells(39, col) & ". " & Cells(40, col) & ". " & Cells(41, col) & "."
       .Cells(17, 4) = Cells(43, col)
       .Cells(17, 5) = Cells(46, col)
    End With
Next col
        For col = 1 To 1
    With Worksheets(2)
       .Cells(18, 2) = Cells(49, col)
       .Cells(18, 3) = Cells(50, col) & ". " & Cells(51, col) & ". " & Cells(52, col) & ". " & Cells(53, col) & "."
       .Cells(18, 4) = Cells(55, col)
       .Cells(18, 5) = Cells(58, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(19, 2) = Cells(49, col)
       .Cells(19, 3) = Cells(50, col) & ". " & Cells(51, col) & ". " & Cells(52, col) & ". " & Cells(53, col) & "."
       .Cells(19, 4) = Cells(55, col)
       .Cells(19, 5) = Cells(58, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(20, 2) = Cells(49, col)
       .Cells(20, 3) = Cells(50, col) & ". " & Cells(51, col) & ". " & Cells(52, col) & ". " & Cells(53, col) & "."
       .Cells(20, 4) = Cells(55, col)
       .Cells(20, 5) = Cells(58, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(21, 2) = Cells(49, col)
       .Cells(21, 3) = Cells(50, col) & ". " & Cells(51, col) & ". " & Cells(52, col) & ". " & Cells(53, col) & "."
       .Cells(21, 4) = Cells(55, col)
       .Cells(21, 5) = Cells(58, col)
    End With
Next col
        For col = 1 To 1
    With Worksheets(2)
       .Cells(22, 2) = Cells(61, col)
       .Cells(22, 3) = Cells(62, col) & ". " & Cells(63, col) & ". " & Cells(64, col) & ". " & Cells(65, col) & "."
       .Cells(22, 4) = Cells(67, col)
       .Cells(22, 5) = Cells(70, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(23, 2) = Cells(61, col)
       .Cells(23, 3) = Cells(62, col) & ". " & Cells(63, col) & ". " & Cells(64, col) & ". " & Cells(65, col) & "."
       .Cells(23, 4) = Cells(67, col)
       .Cells(23, 5) = Cells(70, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(24, 2) = Cells(61, col)
       .Cells(24, 3) = Cells(62, col) & ". " & Cells(63, col) & ". " & Cells(64, col) & ". " & Cells(65, col) & "."
       .Cells(24, 4) = Cells(67, col)
       .Cells(24, 5) = Cells(70, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(25, 2) = Cells(61, col)
       .Cells(25, 3) = Cells(62, col) & ". " & Cells(63, col) & ". " & Cells(64, col) & ". " & Cells(65, col) & "."
       .Cells(25, 4) = Cells(67, col)
       .Cells(25, 5) = Cells(70, col)
    End With
Next col
        For col = 1 To 1
    With Worksheets(2)
       .Cells(26, 2) = Cells(73, col)
       .Cells(26, 3) = Cells(74, col) & ". " & Cells(75, col) & ". " & Cells(76, col) & ". " & Cells(77, col) & "."
       .Cells(26, 4) = Cells(79, col)
       .Cells(26, 5) = Cells(82, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(27, 2) = Cells(73, col)
       .Cells(27, 3) = Cells(74, col) & ". " & Cells(75, col) & ". " & Cells(76, col) & ". " & Cells(77, col) & "."
       .Cells(27, 4) = Cells(79, col)
       .Cells(27, 5) = Cells(82, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(28, 2) = Cells(73, col)
       .Cells(28, 3) = Cells(74, col) & ". " & Cells(75, col) & ". " & Cells(76, col) & ". " & Cells(77, col) & "."
       .Cells(28, 4) = Cells(79, col)
       .Cells(28, 5) = Cells(82, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(29, 2) = Cells(73, col)
       .Cells(29, 3) = Cells(74, col) & ". " & Cells(75, col) & ". " & Cells(76, col) & ". " & Cells(77, col) & "."
       .Cells(29, 4) = Cells(79, col)
       .Cells(29, 5) = Cells(82, col)
    End With
Next col
        For col = 1 To 1
    With Worksheets(2)
       .Cells(30, 2) = Cells(85, col)
       .Cells(30, 3) = Cells(86, col) & ". " & Cells(87, col) & ". " & Cells(88, col) & ". " & Cells(89, col) & "."
       .Cells(30, 4) = Cells(91, col)
       .Cells(30, 5) = Cells(94, col)
    End With
Next col
        For col = 2 To 2
    With Worksheets(2)
       .Cells(31, 2) = Cells(85, col)
       .Cells(31, 3) = Cells(86, col) & ". " & Cells(87, col) & ". " & Cells(88, col) & ". " & Cells(89, col) & "."
       .Cells(31, 4) = Cells(91, col)
       .Cells(31, 5) = Cells(94, col)
    End With
Next col
        For col = 3 To 3
    With Worksheets(2)
       .Cells(32, 2) = Cells(85, col)
       .Cells(32, 3) = Cells(86, col) & ". " & Cells(87, col) & ". " & Cells(88, col) & ". " & Cells(89, col) & "."
       .Cells(32, 4) = Cells(91, col)
       .Cells(32, 5) = Cells(94, col)
    End With
Next col
        For col = 4 To 4
    With Worksheets(2)
       .Cells(33, 2) = Cells(85, col)
       .Cells(33, 3) = Cells(86, col) & ". " & Cells(87, col) & ". " & Cells(88, col) & ". " & Cells(89, col) & "."
       .Cells(33, 4) = Cells(91, col)
       .Cells(33, 5) = Cells(94, col)
    End With
Next col
End Sub

这里有一种非常简洁的方法,请注意rindex公式中的斜杠是反斜杠:

Sub FormatData()

Dim rw As Integer
Dim rindex As Integer

  With Worksheets(2)
      For rw = 2 To 33
          rindex = (rw - 2) \ 4
          .Cells(rw, 2) = Cells(12 * rindex + 1, (rw - 1) - 4 * rindex)
          .Cells(rw, 3) = Cells(12 * rindex + 2, (rw - 1) - 4 * rindex) & "." & _
                          Cells(12 * rindex + 3, (rw - 1) - 4 * rindex) & "." & _
                          Cells(12 * rindex + 4, (rw - 1) - 4 * rindex) & "." & _
                          Cells(12 * rindex + 5, (rw - 1) - 4 * rindex) & "."
          .Cells(rw, 4) = Cells(12 * rindex + 7, (rw - 1) - 4 * rindex)
          .Cells(rw, 5) = Cells(12 * rindex + 10, (rw - 1) - 4 * rindex)
      Next rw
  End With

End Sub

这里有一个重构的
Sub

注意事项:

  • 使用变量数组进行循环/数据处理,因为循环通过单元格的速度很慢

  • 您可以更改
    srcBlocks
    的值以控制要处理的块数,或从源数据中提取

  • Sub FormatData()
        Dim rw2 As Integer, rwA As Integer, colA As Integer
        Dim vDst() As Variant, vSrc As Variant
        Dim srcBlocks As Integer, srcColumns As Integer
    
        srcBlocks = 8  ' process 8 blocks of 12 rows '
        srcColumns = 5 ' Columns in source data '
    
        vSrc = ActiveSheet.Range( _
                 ActiveSheet.Cells(1, 1), _
                 ActiveSheet.Cells(srcBlocks * 12, srcColumns))
        ReDim vDst(1 To srcBlocks * srcColumns + 1, 1 To 5)
    
        For rwA = 0 To srcBlocks * 12 - 1 Step 12           ' = 0, 12, 24, ... '
            For colA = 1 To srcColumns                      ' srcColumns columns in Source '
                rw2 = (rwA \ 12) * srcColumns + colA + 1    ' srcColumns rows in Destination per Source block '
    
                vDst(rw2, 2) = vSrc(rwA + 1, colA)
                vDst(rw2, 3) = vSrc(rwA + 2, colA) & ". " & _
                             vSrc(rwA + 3, colA) & ". " & _
                             vSrc(rwA + 4, colA) & ". " & _
                             vSrc(rwA + 5, colA) & "."
                vDst(rw2, 4) = vSrc(rwA + 7, colA)
                vDst(rw2, 5) = vSrc(rwA + 10, colA)
            Next colA
        Next rwA
    
        Worksheets(2).Range("A1:E" & CStr(srcBlocks * 4 + 1)) = vDst
    End Sub
    
  • 如果目标工作表被移动,则按索引引用该工作表可能会有问题。按名称
    工作表(“SheetName”)查阅更安全

为源数据中的可变列数添加灵活性的步骤

Sub FormatData()
    Dim rw2 As Integer, rwA As Integer, colA As Integer
    Dim vDst() As Variant, vSrc As Variant
    Dim srcBlocks As Integer, srcColumns As Integer

    srcBlocks = 8  ' process 8 blocks of 12 rows '
    srcColumns = 5 ' Columns in source data '

    vSrc = ActiveSheet.Range( _
             ActiveSheet.Cells(1, 1), _
             ActiveSheet.Cells(srcBlocks * 12, srcColumns))
    ReDim vDst(1 To srcBlocks * srcColumns + 1, 1 To 5)

    For rwA = 0 To srcBlocks * 12 - 1 Step 12           ' = 0, 12, 24, ... '
        For colA = 1 To srcColumns                      ' srcColumns columns in Source '
            rw2 = (rwA \ 12) * srcColumns + colA + 1    ' srcColumns rows in Destination per Source block '

            vDst(rw2, 2) = vSrc(rwA + 1, colA)
            vDst(rw2, 3) = vSrc(rwA + 2, colA) & ". " & _
                         vSrc(rwA + 3, colA) & ". " & _
                         vSrc(rwA + 4, colA) & ". " & _
                         vSrc(rwA + 5, colA) & "."
            vDst(rw2, 4) = vSrc(rwA + 7, colA)
            vDst(rw2, 5) = vSrc(rwA + 10, colA)
        Next colA
    Next rwA

    Worksheets(2).Range("A1:E" & CStr(srcBlocks * 4 + 1)) = vDst
End Sub

如果我需要更改要复制到第二页的行数,以确保不会弄糟。。我该怎么做呢?除此之外,它工作得很好!非常感谢。(我想你是说srcBlocks是这样的,对吗?@Leaum,更新了答案以允许可变的列数。我不确定您对srcBlocks有何疑问?好的,更新了答案,现在可以很好地处理您的数据了。