简体   繁体   English

Excel宏-将多列合并为一个

[英]Excel Macro - combine Multiple Columns Into One

I have an excel 2007 worksheet with 12 columns (each column is corresponding to a month) and every column includes +/-30000 rows of daily rainfall data. 我有一个Excel 2007工作表,其中有12列(每列对应一个月),每列包括+/- 30000行的每日降雨量数据。 What I need to do is combine these columns of data into one new column (one continuous rainfall series) as follows: 我需要做的是将这些数据列合并为一个新列(一个连续降雨序列),如下所示:

  1. Copy the first 31 (the number of days of January) rows “A1:A31” from column 1 to the new column 将第1列的前31行(一月的天数)“ A1:A31”复制到新列

  2. Copy the first 28 (the number of days of February) rows from column 2 and place it beneath the previous values in the new column, and, etc.…. 复制第2列中的前28行(2月的天数),并将其放在新列中以前的值下方,等等。 [The first 31 rows (March) from column 3, 30 from column 4, 31 from column 5, 30 from column 6, 31 from column 7, 31 from column 8, 30 from column 9, 31 from column 10, 30 from column 11 and 31 from column 12] [第3列的前31行(3月),第4列的30,第5列的30,第6列的31,第7列的31,第8列的31,第10列的30,第10列的30,第30列的[第12栏的11和31]

  3. Then, do the same for the next year, ie copy the second 31 values “A32:A62” from column 1 and place it beneath the previous year (Step 1 & 2) in the new column. 然后,对下一年进行同样的操作,即从第1列复制第二31个值“ A32:A62”,并将其放在新列中上一年的下方(步骤1和2)。

  4. In total, the result will be a continuous daily rainfall series. 总的来说,结果将是连续的每日降雨序列。

I have tried my best to accomplish this, but I have got nowhere! 我尽了最大的努力来做到这一点,但是我无处可去!

Please, could someone help me with this? 拜托,有人可以帮我吗?

Thanks a lot 非常感谢

================== ==================

More explanation 更多说明

The data are sorted into several columns by month, for several years, and it looks something like this: 几年来,这些数据按月分为几列,看起来像这样:

Year Day Jan Feb March 年1月2月3日

1990 1 25 15 1990年1 25 15

1990 2 20 12 1990年2 20 12

1990 3 22 1990年3月22日

1990 4 26 1990 4 26

So every column has a different length from month to month according to the number of days in each month (eg, January has 31 days). 因此,根据每个月中的天数,每个列的长度在每个月都有不同的长度(例如,一月有31天)。 Now, I need to combine all the entries into one long column. 现在,我需要将所有条目合并到一个长列中。 So it would look like this: 所以它看起来像这样:

25 25

20 20

22 22

26 26

15 15

12 12

Any help would be appreciated! 任何帮助,将不胜感激!

Also the following methods could be helpful for you: 另外,以下方法可能对您有所帮助:

Function xlsRangeCopyConditionalFormat(ByRef r1 As Excel.Range, _
                                       ByRef r2 As Excel.Range)
    Dim i As Integer
    For i = 1 To r1.FormatConditions.Count
        r2.FormatConditions.Delete
    Next    
    For i = 1 To r1.FormatConditions.Count
            r2.FormatConditions.Add _
                                type:=r1.FormatConditions(i).type, _
                                Operator:=r1.FormatConditions(i).Operator, _
                                Formula1:=r1.FormatConditions(i).Formula1

        xlsRangeCopyFont r1.FormatConditions(i).Font, r2.FormatConditions(i).Font
        xlsRangeCopyInterior r1.FormatConditions(i).Interior, r2.FormatConditions(i).Interior        
    Next
End Function

Public Function xlsRangeCopyInterior(ByRef i1 As Excel.Interior, _
                                     ByRef i2 As Excel.Interior)
    With i2
        .Pattern = i1.Pattern
        .ColorIndex = i1.ColorIndex
    End With
End Function

Public Sub xlsRepeatValueInCell(ByRef xlSheet As Excel.Worksheet, _
                             ByRef sColumn As String, _
                             ByVal irow As Integer, _
                             ByRef sValue As String)                              
    xlsSetValueInCell xlSheet, sColumn, irow, sValue
    xlSheet.Range(sfxls_RA1(sColumn, irow)).Borders(xlEdgeTop).color = RGB(255, 255, 255)
    xlSheet.Range(sfxls_RA1(sColumn, irow)).Font.ColorIndex = 15
End Sub

Public Sub xlsSetCellInterior(ByRef xlSheet As Excel.Worksheet, _
                              ByRef sColumn As String, _
                              ByRef irow As Integer, _
                              ByRef iColorIndex As Integer, _
                              Optional ByRef bSetCellValue As Boolean = False, _
                              Optional ByRef iCellValueColor = Null)
    ' Set cells interior based on the passed arguments

    Dim iPattern As Integer, iColorIndex As Integer, sValue As String

    iPattern = xlSolid 'iPattern = xlGray16
    xlSheet.Range(sfxls_RA1(sColumn, irow)).Interior.Pattern = iPattern
    xlSheet.Range(sfxls_RA1(sColumn, irow)).Interior.ColorIndex = iColorIndex
    If bSetCellValue = True Then
        xlSheet.Range(sfxls_RA1(sColumn, irow)).FormulaR1C1 = sValue
    End If
    If Not IsNull(iCellValueColor) Then
        xlSheet.Range(sfxls_RA1(sColumn, irow)).Font.ColorIndex = iCellValueColor
    Else
        xlSheet.Range(sfxls_RA1(sColumn, irow)).Font.ColorIndex = iColorIndex
    End If

End Sub

If what you want is to merge cells you should create a Macro and the use a function to achieve such task. 如果要合并单元格,则应创建一个宏,然后使用一个函数来完成此任务。 Try this code: 试试这个代码:

Public Sub xlsSetMsgAndCombineCells(xlSheet As Excel.Worksheet, _
                                  sCol1 As String, _
                                  sCol2 As String, _
                                  irow As Integer, _
                                  sValue As String)
    ' Combine specified cells and set a message

    Dim xlRange As Excel.Range
    Set xlRange = xlSheet.Range(sfxls_RA1(sCol1, irow), sfxls_RA1(sCol2, irow))

    With xlRange
        .Merge
        .FormulaR1C1 = sValue
        .Font.Bold = True
        .VerticalAlignment = xlVAlignCenter
        .HorizontalAlignment = xlVAlignCenter
    End With

    Set xlRange = Nothing

End Sub

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

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