用于将列的一部分复制到行的VBA代码

时间:2016-03-04 05:56:09

标签: vba excel-vba excel

我是VBA的新手,在从一个列复制数据时需要一些帮助,在同一列中的数据之间使用均匀间隔的分区并粘贴为行。

我有一张包含300张名片的Excel表格,如图片1所示。

每个名片都是一个突出显示的块,如下例所示:

picture

我需要一个VBA代码来复制C列中的数据,并将其放在标题A,B,C,D,E,F下的行中。

是否有可以执行此类操作的VBA代码?

非常感谢任何帮助!!!

1 个答案:

答案 0 :(得分:1)

这应该有用。

 Option Explicit
 Sub TransposeBusinessCardData()

      Dim BusinessCardDataSheet As Worksheet
      'Replace BusinessCardSheet with the sheet name of your sheet
      Set BusinessCardDataSheet = ThisWorkbook.Sheets("BusinessCardSheet")
      Dim ResultSheet As Worksheet
      'Replace ResultSheet with the sheet name of the sheet you want to paste the data in
      Set ResultSheet = ThisWorkbook.Sheets("ResultSheet")

      Dim LastRow As Long
      LastRow = BusinessCardDataSheet.Cells(BusinessCardDataSheet.Rows.Count, "C").End(xlUp).Row

      Dim RowReference As Long
      Dim BusinessCardData As Range
      Dim ResultRowRef As Long

      'To paste from Row 2 on the ResultSheet
      ResultRowRef = 2

      'Step 7 Because there is 7 Rows between the start of each Business card
      For RowReference = 2 To LastRow Step 7

           BusinessCardDataSheet.Activate
           Set BusinessCardData = BusinessCardDataSheet.Range(Cells(RowReference, "C"), Cells(RowReference + 5, "C"))
           BusinessCardData.Copy

           ResultSheet.Cells(ResultRowRef, "B").PasteSpecial Paste:=xlPasteAll, _
                                                                    Operation:=xlNone, SkipBlanks:=False, _
                                                                    Transpose:=True
           ResultRowRef = ResultRowRef + 1

      Next RowReference


 End Sub