简体   繁体   English

将多列数据导出到多张工作表中

[英]Export data in multiple columns into multiple sheets

I would like to export some column data into separate sheets which I will then export into individual ASCII text files. 我想将某些列数据导出到单独的工作表中,然后再将其导出到单独的ASCII文本文件中。 The specific data are as shown whereby I would like to copy the first two columns (x, y coordinates) and each individual column thereafter into its own sheet. 特定数据如图所示,我想将前两列(x,y坐标)和此后的每一列复制到自己的工作表中。

 x     y    Comp1   Comp2   Comp3   Comp4    …  Comp23
-40  -20    55.29   0       0       73       …  105.67
-40  -19.9  56.79   0       33      72       …  112.5
-40  -19.8  69.29   0       31      89       …  114
-40  -19.7  70.29   0       58.14   108      …  125
 …    …     …       …       …       …        …  …
 40   55    72.29   0       49      117      …  132

I'm still getting to grips with writing macros so right now I am basically trying to adapt a recorded macro for one of the iterations to work for the entire sheet which looks as follows: 我仍在努力编写宏,所以现在我基本上是在尝试为一个迭代改编一个记录的宏,以使其适用于整个工作表,如下所示:

Sub CopyColData()
    ActiveCell.Range("A1:B1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Sheets("Sheet1").Select
    Sheets("Sheet1").Name = "Comp1"
    Sheets("SUM").Select
    Application.CutCopyMode = False
    ActiveCell.Offset(0, 2).Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Comp1").Select
    ActiveCell.Offset(0, 2).Range("A1").Select
    ActiveSheet.Paste
End Sub 

Ideally I would like it to create a new worksheet for each column with the coordinate data in the first two columns, label the worksheet according to the column title and then copy the column data onto the third column. 理想情况下,我希望它为前两列中的坐标数据为每列创建一个新的工作表,根据列标题标记工作表,然后将列数据复制到第三列。 Afterwards I'll export the multiple sheets into individual ASCII files using a different macro. 之后,我将使用不同的宏将多张图纸导出到单独的ASCII文件中。 Thanks! 谢谢!

All thanks to Jerry Beaucaire (again!) but I added a counter for sheet naming: 非常感谢Jerry Beaucaire (再次!),但我为工作表命名添加了一个计数器:

Option Explicit

Sub ColumnsToSheets()
'Author:    Jerry Beaucaire
'Date:      8/7/2011
'Summary:   Create separate sheets from the columns of a data sheet

Dim wsData   As Worksheet   'Sheet with data to parse
Dim FirstCol As Long        'This is the first column to transfer
Dim ColCnt   As Long        'This is how many columns in a group to transfer
Dim LastCol  As Long        'check row1 to see how many columns of data there are
Dim NewSht   As Long        'how many new sheets will be created
Dim inti As Integer         'counter for sheet naming

FirstCol = Application.InputBox("Which column is the first 'data column' to transfer?" _
    & vbLf & "(A=1, B=2, C=3, etc...)" _
    & vbLf & "(All columns to the left will appear on every sheet)", _
    "First Data Column", 2, Type:=1)
If FirstCol = 0 Then Exit Sub

ColCnt = Application.InputBox("How many data columns are in each group?", _
    "Groups of Columns", 1, Type:=1)
If ColCnt = 0 Then Exit Sub
inti = 1
Set wsData = ActiveWorkbook.Sheets("Sheet1")
Application.ScreenUpdating = False

  With wsData
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For NewSht = FirstCol To LastCol Step ColCnt
        Sheets.Add , After:=Sheets(Sheets.Count)
        .Columns(1).Resize(, FirstCol - 1).Copy Range("A1")
        .Columns(NewSht).Resize(, ColCnt).Copy Cells(1, FirstCol)
        ActiveSheet.Name = "Comp" & inti
        inti = inti + 1
    Next NewSht
  End With
Application.ScreenUpdating = True
End Sub  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM