Vba Excel宏-将多列合并为一列
我有一份excel 2007工作表,有12列(每列对应一个月),每列包含+/-30000行每日降雨量数据。我需要做的是将这些数据列组合成一个新列(一个连续降雨系列),如下所示: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行] 然
任何帮助都将不胜感激 如果要合并单元格,则应创建宏并使用函数来完成此任务。请尝试以下代码:
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如果你觉得这个答案有用/有帮助,你应该接受它,这样别人将来会更容易找到它。