首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel大学VBA

Excel大学VBA
EN

Stack Overflow用户
提问于 2016-09-27 11:10:11
回答 1查看 70关注 0票数 0

我正在寻找唯一的格式给出在右侧。我在一个论坛网站上找到了VBA代码,但这个并不适合我。有没有办法修改代码或者写一些更好的东西。我确实有一个公式,但公式是相当资源密集的,一个非常大的excel加载非常慢。

代码语言:javascript
复制
Sub FindDistinctValues()
Dim LastRowFrom As Long
Dim LastRowTo As Long
Dim i As Long, j As Long
Dim temp As Integer
Dim found As Boolean
'determines the last row that contains data in column A
LastRowFrom = Range("A" & Rows.Count).End(xlUp).Row
'Loop for each entry in column A
For i = 2 To LastRowFrom
'get the next value from column A
temp = Range("A" & i).Value

'Determine the last row with data in column B
LastRowTo = Range("B" & Rows.Count).End(xlUp).Row

'initialize j and found
j = 1
 found = False

     'Loop through "To List" until a match is found or the list has been    searched
      Do
      'check if the value exists in B column
      If temp = Range("B" & j).Value Then
     found = True
     End If
     'increment j
    j = j + 1
     Loop Until found Or j = LastRowTo + 1

    'if the value is not already in column B
    If Not found Then
   Range("B" & j).Value = temp
  End If
Next i
End Sub

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-27 14:28:07

我没有测试它,但是像这样的东西:

代码语言:javascript
复制
Sub FindDistinctValues()
    Dim dict As Object, cell As Range
    Set dict = CreateObject("Scripting.Dictionary")

    For Each cell in Range("A1").CurrentRegion.Resize(, 1)
        If Not dict.Exists(cell & "")
            cell(, 2) = "Unique"
            dict.Add cell & "", 0
        End If
    Next
End Sub
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39723145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档