-1

我有一个 Excel 文件,B 列中有发票编号,( B2:B14987),CI 列有项目 ID,DI 列有销售价值,EI 列有发票折扣值。

我需要一个宏来合并基于发票编号列的发票折扣值单元格,发票编号重复,因为一张发票中有不同的项目 ID。

例如:B1:B3是相同的发票编号,E1是 中发票的常见折扣值B1:B3E2:E3是空白单元格。所以我想E1:E3E1.

4

1 回答 1

2

以下代码可以满足我的要求;一如既往,如果我误解了,请澄清问题,我们会到达那里......

在电子表格中创建一个模块,然后粘贴以下代码:

Private Sub mergeAndAlign(r As Range)
    With r
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .MergeCells = True
    End With
End Sub

Sub mergeAll()
' step through column E
' merge all cells that have the same invoice number
' they are already sorted - and the value we need is in the first cell
' of the block to be merged
Dim r As Range
Dim prevItem As Range
Dim nextItem As Range
Dim lastRow, thisRow, rCount As Integer

lastRow = [B2].End(xlDown).Row

Set prevItem = [E2]
Set nextItem = prevItem.End(xlDown)

While nextItem.Row <= lastRow
  Set r = Range(prevItem, nextItem.Offset(-1, 0))
  mergeAndAlign r
  Set prevItem = nextItem
  Set nextItem = nextItem.End(xlDown)
Wend

' do the last item:
Set nextItem = Cells(lastRow, 5) ' last valid cell in column E
Set r = Range(prevItem, nextItem)
mergeAndAlign r

End Sub

从感兴趣的工作表中运行代码。单击 Alt-F8 以调出“宏”对话框 - 您应该会在列表中看到“MergeAll”项(可能是唯一一项)。它会带你去:

原始电子表格

对此:

合并后的电子表格

于 2013-05-02T13:53:15.243 回答