VBA

Rows(1).Insert '在第1行前插入1行

Private Sub Worksheet_Activate()
    myrow = ActiveWorkbook.ActiveSheet.UsedRange.Rows.Count
    Dim idx As Integer
    Dim j As Integer
    idx = 4
    For j = 3 To myrow
        Debug.Print Worksheets("sheet2").Cells(j, 1).Value
        idx = processValue(idx, Worksheets("sheet2").Cells(j, 1))
    Next j
End Sub

Function processValue(idx As Integer, val As Range)
    Dim i As Integer
    Dim b As Boolean
    For i = 1 To idx
        b = Worksheets("sheet1").Cells(i, 1).Value = val.Value
        If b Then
            Worksheets("sheet1").Cells(i, 2).Value = Worksheets("sheet1").Cells(i, 2).Value + 1
            Exit For
        End If
    Next i
    If b Then
        processValue = idx
    Else
        Worksheets("sheet1").Cells(idx, 1).Value = val.Value
        Worksheets("sheet1").Cells(idx, 2).Value = 1
        processValue = idx + 1
    End If
End Function

Private Sub Worksheet_Activate()
    Application.ScreenUpdating = False
    Dim buffer As New Collection
    Dim index As Long
    Dim first As Long
    Dim last As Long
    Dim counter As Long
    Dim current As Range
    Dim ws As Worksheet
   
    first = 4
    Set ws = Application.Worksheets("基础数据表")
    last = ws.UsedRange.Rows.Count
       
    For counter = first To last
        Set current = ws.Range("D" & counter)
        Call analyze(buffer, current, counter)
    Next counter
    Application.ScreenUpdating = True
End Sub
Function analyze(ByRef buffer As Collection, ByRef cluster As Range, rownum As Long)
    Dim exist As Boolean
    Dim rowset As Collection
   
    exist = False
   
    If Not (IsError(cluster.value) Or IsEmpty(cluster.value) Or cluster.value = "") Then
    On Error GoTo nextStep
        Set rowset = buffer.Item(CStr(cluster.value))
        exist = True
nextStep:
    If Not exist Then
        Set rowset = New Collection
        buffer.Add rowset, CStr(cluster.value)
    End If
    rowset.Add rownum
End If
End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值