Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Vba Excel宏-将多列合并为一列_Vba_Excel_Merge - Fatal编程技术网

Vba Excel宏-将多列合并为一列

Vba Excel宏-将多列合并为一列,vba,excel,merge,Vba,Excel,Merge,我有一份excel 2007工作表,有12列(每列对应一个月),每列包含+/-30000行每日降雨量数据。我需要做的是将这些数据列组合成一个新列(一个连续降雨系列),如下所示: 将第1列的前31行(1月天数)“A1:A31”复制到新列 从第2列复制前28行(2月的天数),并将其放置在新列中以前的值下面,以此类推。…。[第3列的前31行(3月),第4列的30行,第5列的31行,第6列的30行,第7列的31行,第8列的31行,第9列的30行,第10列的31行,第11列的30行,第12列的31行] 然

我有一份excel 2007工作表,有12列(每列对应一个月),每列包含+/-30000行每日降雨量数据。我需要做的是将这些数据列组合成一个新列(一个连续降雨系列),如下所示:

  • 将第1列的前31行(1月天数)“A1:A31”复制到新列

  • 从第2列复制前28行(2月的天数),并将其放置在新列中以前的值下面,以此类推。…。[第3列的前31行(3月),第4列的30行,第5列的31行,第6列的30行,第7列的31行,第8列的31行,第9列的30行,第10列的31行,第11列的30行,第12列的31行]

  • 然后,对下一年执行相同操作,即从第1列复制第二个31个值“A32:A62”,并将其置于新列中上一年(步骤1和步骤2)的下方

  • 总的来说,结果将是一个连续的日降雨量序列
  • 我已经尽了最大的努力去完成这件事,但是我一事无成

    拜托,有人能帮我吗

    非常感谢

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

    更多解释 数年来,数据按月分为几列,如下所示:

    年月日一月二月三月

    1990 12515

    1990年2月20日12

    1990年3月22日

    1990年4月26日

    因此,根据每个月的天数(例如,1月有31天),每个列的长度每个月都不同。现在,我需要将所有条目合并到一个长列中。所以看起来是这样的:

    二十五

    二十

    二十二

    二十六

    十五

    十二,


    任何帮助都将不胜感激

    如果要合并单元格,则应创建宏并使用函数来完成此任务。请尝试以下代码:

    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
    

    如果要合并单元格,则应创建宏并使用函数来完成此任务。请尝试以下代码:

    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
    

    此外,以下方法可能对您有所帮助:

    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
    

    此外,以下方法可能对您有所帮助:

    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
    

    谢谢你的回复。我会试试的。谢谢你的回复。我会试试看。@Muamar如果你觉得这个答案有用/有帮助,你应该接受它,这样别人将来会更容易找到它。@Muamar如果你觉得这个答案有用/有帮助,你应该接受它,这样别人将来会更容易找到它。