簡體   English   中英

Excel宏-將多列合並為一個

[英]Excel Macro - combine Multiple Columns Into One

我有一個Excel 2007工作表,其中有12列(每列對應一個月),每列包括+/- 30000行的每日降雨量數據。 我需要做的是將這些數據列合並為一個新列(一個連續降雨序列),如下所示:

  1. 將第1列的前31行(一月的天數)“ A1:A31”復制到新列

  2. 復制第2列中的前28行(2月的天數),並將其放在新列中以前的值下方,等等。 [第3列的前31行(3月),第4列的30,第5列的30,第6列的31,第7列的31,第8列的31,第10列的30,第10列的30,第30列的[第12欄的11和31]

  3. 然后,對下一年進行同樣的操作,即從第1列復制第二31個值“ A32:A62”,並將其放在新列中上一年的下方(步驟1和2)。

  4. 總的來說,結果將是連續的每日降雨序列。

我盡了最大的努力來做到這一點,但是我無處可去!

拜托,有人可以幫我嗎?

非常感謝

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

更多說明

幾年來,這些數據按月分為幾列,看起來像這樣:

年1月2月3日

1990年1 25 15

1990年2 20 12

1990年3月22日

1990 4 26

因此,根據每個月中的天數,每個列的長度在每個月都有不同的長度(例如,一月有31天)。 現在,我需要將所有條目合並到一個長列中。 所以它看起來像這樣:

25

20

22

26

15

12

任何幫助,將不勝感激!

另外,以下方法可能對您有所幫助:

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

如果要合並單元格,則應創建一個宏,然后使用一個函數來完成此任務。 試試這個代碼:

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