如何通过单元格并用边框将相似的单元格分组

时间:2019-10-29 08:00:04

标签: excel vba

我正在尝试使用VBA在报表中相似数字的组周围放置边框。

在函数中,我遍历A列的值以查看是否存在与组相似的值。如果找到它们,则通过并集函数将它们添加到范围中。一旦全部选中,就应该在整个批次周围设置边框格式。似乎只将每行的最后一个单元格接地。

import SwiftUI

struct ContentView: View {
    @State private var checkAmount = ""
    @State private var numberOfPeople = 0
    @State private var tipPercentage = 2
    let tipPercentages = [10, 15, 20, 25, 0]
    var totalPerPerson: Double {
        let peopleCount = Double(numberOfPeople + 2)
        let tipSelection = Double(tipPercentages[tipPercentage])
        let orderAmount = Double(checkAmount) ?? 0

        let tipValue = orderAmount / 100 * tipSelection
        let grandTotal = orderAmount + tipValue
        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }

            var body: some View {
                NavigationView {
                    Form {
                        Section (header: Text("Amount on check")) {
                            TextField("Amount", text: $checkAmount)
                                .keyboardType(.decimalPad)
                            Picker("Number of People", selection: $numberOfPeople){
                                ForEach(2..<100) {
                                    Text("\($0) people")
                                }
                            }
                        }

                     Section(header: Text("How much tip do you want to tip?")) {
                            Picker("Tip percentage", selection: $tipPercentage) {
                                ForEach(0 ..< tipPercentages.count) {
                                    Text("\(self.tipPercentages[$0])%")
                                }
                            }.pickerStyle(SegmentedPickerStyle())
                        }

                        Section (header: Text("Amnount per person")){
                            Text("$ \(totalPerPerson, specifier: "%.2f")")
                        }

                        Section (header: Text("Amnount per person")){
                                Text("$ \(totalPerPerson, specifier: "%.2f")")
                            }

                        .navigationBarTitle("Splitt Bill")
                    }
                }
            }
        }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

我希望A列中的所有类似记录都按照批次周围的边界进行分组。工作表将以降序排序,因此它们都将彼此分组。由于某种原因,它仅将边框应用于集合中的最后一行(A:H)。

谢谢,对即席方法感到抱歉。我肯定有一种有效的方法:)

1 个答案:

答案 0 :(得分:1)

我对您的代码做了一些更改,以存档您想要的内容。我希望这是可以理解的。 它不是那么漂亮,但是可以工作。

Sub OrderFormatting()

    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = Sheets("Report")

    lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
    Dim searchRange As Range
    Dim countMatches As Long
    Set searchRange = Range("A2:A" & lastrow + 1)
    Dim search As String

    For Each cell In searchRange
        If search = "" Then
            search = cell.Text
        ElseIf search = cell.Text Then
            countMatches = countMatches + 1
        Else
            countMatches = countMatches + 1
            Range("A" & cell.Row - countMatches & ":H" & cell.Row - 1).BorderAround ColorIndex:=1, Weight:=xlThin
            countMatches = 0
            search = cell.Text
        End If
    Next cell

End Sub