对有连字符的字母数字数据进行排序

2024-01-03

我的工作簿中有两张表,每张表都有自己的电子邮件地址列以及其他数据。我将引用 Sheet1 中的 Column1 和 Sheet2 中的 Column2,其中只有 Column1 可能列出了重复的电子邮件地址。

我需要确定 Column1 中的电子邮件地址是否在 Column2 中找到,并且每次都是这样时,必须运行某些代码。

我用两个嵌套的 Do While 循环解决了这个问题,其中外部循环从上到下遍历 Column1 中名为 Cell1 的每个单元格,内部循环也从上到下将 Cell1 与 Column2 中名为 Cell2 的每个单元格进行比较,如果发现相同的值,则提前退出内部循环。

为了提高效率,我想到按升序对每一列进行排序,并且让每个Cell1仅查看Column2,直到Cell2中的字符串值大于Cell1中的字符串值,并且当迭代下一个Cell1时它将从上次循环停止的 Cell2 继续,因为较早的 Cell2 值都小于 Cell1 并且不能具有相等的值。

我想出的代码是一个遍历 Column1 中每个单元格的外部循环,以及一个内部循环,如下所示:

'x1 is the row number of Cell1
'x2 is the row number of Cell2
'below is the code for the internal loop looking through Column2

Do While Sheets(2).Cells(x2, 1).Value <> 0
    If LCase(Sheets(1).Cells(x1, 1).Value) < LCase(Sheets(2).Cells(x2, 1).Value) Then
        Exit Do
    ElseIf LCase(Sheets(1).Cells(x1, 1).Value) = LCase(Sheets(2).Cells(x2, 1).Value) Then

        '... code is run

        Exit Do
    End If
    x2 = x2 + 1
Loop

问题是电子邮件地址可以包含连字符 (-) 和撇号 (')。虽然 Excel 在对列进行排序时会忽略它们,但 VBA 在比较字母数字值时不会忽略它们。

如果我有:

     A           B
1  Noemi      Noemi
2  no-reply   no-reply
3  notify     notify

代码将 A1 与 B1 进行比较并查看A1=B1,然后 A2 与 B1 并看到A2<B1然后跳到A3。

我的第一个问题是,我可以强制 Excel 对字母数字文本(包括连字符和撇号)进行排序吗?

如果没有,到目前为止,我只想通过查看 Cell1 和 Cell2 中是否有 - 或 ' 来解决方法,如果其中任何一个为 TRUE,则使用新变量从 Cell1 和 Cell2 中提取不带连字符的文本,撇号,并继续在内部循环中使用这些新值。

我的第二个问题是,如何以更有效的方式解决这个问题?

EDIT:

Microsoft 认识到 Excel 在排序时会忽略破折号和撇号:

http://office.microsoft.com/en-001/excel-help/default-sort-orders-HP005199669.aspx http://office.microsoft.com/en-001/excel-help/default-sort-orders-HP005199669.aspx http://support.microsoft.com/kb/322067 http://support.microsoft.com/kb/322067


如果昨天有人问我,我会同意 David 对 Excel 排序的预期结果的看法。然而,经过实验,我不得不同意德克的观点。值得注意的是:

撇号 (') 和连字符 (-) 会被忽略,但有一个例外:如果两个文本字符串除了连字符之外都相同,则带有连字符的文本将排在最后。 source http://dmcritchie.mvps.org/excel/sorting.htm

A 列包含我用于测试 Dirk 声明的未排序值。

B 列已进行常规 Excel 排序。正如您所看到的,该列不是 ASCII/Unicode 序列,因为“单引号”应该出现在“连字符”之前,而“连字符”应该出现在“字母 a”之前。

Excel 使用波形符 (~) 作为“查找”的转义字符,因此我想知道它是否也会对“排序”执行相同的操作。AdjustedSort1将“单引号”替换为“波形符单引号”,将“连字符”替换为“波形符连字符”,排序然后恢复“单引号”和“连字符”。结果显示在 C 列中。该序列更好,但不是 ASCII/Unicode,因为“aa-b”位于“aa'c”之前。

D 列使用我几年前编写的 VBA 希尔排序例程。如果您的列表非常大,您可能最好在网上搜索“VBA 快速排序”,但我的排序应该为合理大小的列表提供可接受的性能。

Sub AdjustedSort1()

  With Worksheets("Sheet2").Columns("C")

    .Replace What:="'", Replacement:="~'", LookAt:=xlPart, _
             SearchOrder:=xlByRows, MatchCase:=False, _
             SearchFormat:=False, ReplaceFormat:=False
    .Replace What:="-", Replacement:="~-", LookAt:=xlPart, _
             SearchOrder:=xlByRows, MatchCase:=False, _
             SearchFormat:=False, ReplaceFormat:=False
    .Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlYes, _
          OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    .Replace What:="~~-", Replacement:="-", LookAt:=xlPart, _
          SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
    .Replace What:="~~'", Replacement:="'", LookAt:=xlPart, _
          SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False

  End With

End Sub
Sub AdjustedSort2()

  Dim Inx As Long
  Dim RngValue As Variant
  Dim RowLast As Long
  Dim ColValue() As String

  With Worksheets("Sheet2")

    RowLast = .Cells(Rows.Count, "D").End(xlUp).Row

    ' Load values from column D excluding header
    RngValue = .Range(.Cells(2, "D"), .Cells(RowLast, "D")).Value

    ' Copy values from 2D array to 1D array
    ReDim ColValue(LBound(RngValue, 1) To UBound(RngValue, 1))
    For Inx = LBound(RngValue, 1) To UBound(RngValue, 1)
      ColValue(Inx) = RngValue(Inx, 1)
    Next

    ' Sort array
    Call ShellSort(ColValue, UBound(ColValue))

    ' Copy values back to 2D array
    For Inx = LBound(ColValue) To UBound(ColValue)
      RngValue(Inx, 1) = ColValue(Inx)
    Next

    ' Copy values back to column D
    .Range(.Cells(2, "D"), .Cells(RowLast, "D")).Value = RngValue

  End With

End Sub
Public Sub ShellSort(arrstgTgt() As String, inxLastToSort As Integer)

  ' Coded 2 March 07
  ' Algorithm and text from Algorithms (Second edition) by Robert Sedgewick

  '   The most basic sort is the insertion sort in which adjacent elements are compared
  ' and swapped as necessary.  This can be very slow if the smallest elements are at
  ' end.  ShellSort is a simple extension which gains speed by allowing exchange of
  ' elements that are far apart.
  '   The idea is to rearrange the file to give it the property that taking every h-th
  ' element (starting anywhere) yields a sorted file.  Such a file is said to be
  ' h-sorted.  Put another way, an h-sorted file is h independent sorted files,
  ' interleaved together.  By h-sorting for large value of H, we can move elements
  ' in the array long distances and thus make it easier to h-sort for smaller values of
  ' h.  Using such a procedure for any sequence of values of h which ends in 1 will
  ' produce a sorted file.
  '   This program uses the increment sequence: ..., 1093, 364, 121, 40, 13, 4, 1.  This
  ' is known to be a good sequence but cannot be proved to be the best.
  '   The code looks faulty but it is not.  The inner loop compares an
  ' entry with the previous in the sequence and if necessary moves it back down the
  ' sequence to its correct position.  It does not continue with the rest of the sequence
  ' giving the impression it only partially sorts a sequence.  However, the code is not
  ' sorting one sequence then the next and so on.  It examines the entries in element
  ' number order.  Having compared an entry against the previous in its sequence, it will
  ' be intH loops before the next entry in the sequence in compared against it.

  Dim intNumRowsToSort          As Integer
  Dim intLBoundAdjust           As Integer
  Dim intH                      As Integer
  Dim inxRowA                   As Integer
  Dim inxRowB                   As Integer
  Dim inxRowC                   As Integer
  Dim stgTemp                   As String

  intNumRowsToSort = inxLastToSort - LBound(arrstgTgt) + 1
  intLBoundAdjust = LBound(arrstgTgt) - 1

  ' Set intH to 1, 4, 13, 40, 121, ..., 3n+1, ... until intH > intNumRowsToSort
  intH = 1
  Do While intH <= intNumRowsToSort
    intH = 3 * intH + 1
  Loop

  Do While True
    If intH = 1 Then Exit Do
    ' The minimum value on entry to this do-loop will be 4 so there is at least
    ' one repeat of the loop.
    intH = intH \ 3
    For inxRowA = intH + 1 To intNumRowsToSort
      stgTemp = arrstgTgt(inxRowA + intLBoundAdjust)
      inxRowB = inxRowA
      Do While True
        ' The value of element inxRowA has been saved.  Now move the element intH back
        ' from row inxRowA into this row if it is smaller than the saved value.  Repeat
        ' this for earlier elements until one is found that is larger than the saved
        ' value which is placed in the gap.
        inxRowC = inxRowB - intH
        If arrstgTgt(inxRowC + intLBoundAdjust) <= stgTemp Then Exit Do
        arrstgTgt(inxRowB + intLBoundAdjust) = arrstgTgt(inxRowC + intLBoundAdjust)
        inxRowB = inxRowC
        If inxRowB <= intH Then Exit Do
      Loop
      arrstgTgt(inxRowB + intLBoundAdjust) = stgTemp
    Next
  Loop

End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对有连字符的字母数字数据进行排序 的相关文章

  • 从VBA中的数组批量插入到sql中

    我正在尝试在 Excel 中构建一个按钮 将所选区域上传到 SQL Server 中的表中 第一行将自动视为列标题 这件事该怎么继续下去呢 我想要的是简单和超快的上传 这是我的想法 我将选择选定的区域 然后将其保存为 txt 文件 然后对其
  • 在 Drupal 中对视图进行排序时忽略“The”

    当用户在 Drupal 站点中对视图进行排序时 如何忽略 The 你有没有尝试过调查视图自然排序模块 http drupal org project views natural sort 取自上面链接的模块页面 提供排序的视图过滤器 以更自
  • 消除多个 Elseif 语句

    我试图保持我的代码干净 特别是在用户表单中使用组合框 可能会有很多 if Elseif 语句 应该有一种更简单的方法 让一个组合框不再需要多页代码 是吗 现在如何完成的示例 Sub Example Dim Variable as Strin
  • 基于数组对列表进行排序

    我正在尝试根据字符串数组对自定义列表进行排序 但我失败得很惨 例如它根本没有对列表进行排序 Public class CrateOrder public int Id get set public string Name get set p
  • 正在使用的 VBA 监视文件

    我正在寻找一些东西 Win API 调用或其他 来在文件可供编辑 即不再使用 时通知我 我应该设置一个计时器来按一定时间间隔检查文件还是有一个好方法对文件设置监视 FileSystemWatcher 没有帮助 Win32 FindFirst
  • matlab 中的 for 或 while 循环

    我刚刚开始在编程课的 matlab 中使用 for 循环 基本的东西对我来说很好 但是我被要求 使用循环创建一个 3 x 5 矩阵 其中每个元素的值是其行号其列号除以行号和列号之和的幂 例如元素 2 3 的值为 2 3 2 3 1 6 那么
  • 按升序对数字字符串列表进行排序

    我创建了一个SQLite https en wikipedia org wiki SQLite数据库有一个存储温度值的表 第一次将温度值按升序写入数据库 然后 我将数据库中的温度值读入列表中 然后将该列表添加到组合框中以选择温度 效果很好
  • AS3 - for (... in ...) 与 for every (... in ...)

    以下代码执行完全相同的操作 之间有区别吗for each and for in var bar Array new Array 1 2 3 for var foo in bar trace foo for each var foo2 in
  • 使用 C# 在 Excel 中查找和替换文本

    我想使用 C 在 Excel 中查找并替换一组文本 而且我希望此替换仅发生在第一行中的文本 我已经使用Google并找到了一些付费资源 例如Aspose API Spire Xls等 但我正在寻找开源资源或任何其他有效的方法来实现这一目标
  • 对列表进行排序,同时保持一些元素始终位于顶部

    我们有一个List
  • 如何获取 Word 应用程序的 Hwnd/进程 ID,并将其设置为前台窗口

    我希望我的 Word 应用程序在自动化完成后进入前台 Excel 中的等效项很简单 Excel Application 对象有一个 Hwnd 属性 您可以将其与 Windows API 结合使用 SetForegroundWindow In
  • 使用 PHP 代码和 HTML 表单将 Excel (.csv) 导入 MySQL

    我知道还有其他类似的帖子 但每个人都建议直接在 PHPMyAdmin 中将其导入 MySQL 这工作完美 但我需要通过 HTML 表单导入 PHP 到 MySQL 我想要一个收集文件的 HTML 表单 然后将该文件传递给 PHP 脚本 我想
  • 在php中对带有特殊字符的多维数组进行排序

    我有一个多维数组 我按字母顺序排序 但问题是 带有丹麦语特殊字符 它们应该按该顺序排序 但不会按该顺序返回 这是我的数组 部分已删除 Array 0 gt Array Name gt John 1 gt Array Name gt Pate
  • Excel Add In - console.log 在哪里输出它的消息 - NodeJS

    我正在尝试使用 JavaScript API 创建 Excel 插件 但我不明白 console log 在哪里输出它们的消息 所有 Microsoft 文档都包含 console log 示例 但没有解释 console log 输出消息
  • 通过 Excel VBA 保存并关闭 powerpoint

    下面的代码根据定义的名称创建多个图表 然后打开具有这些定义的名称的 powerpoint 文件并转储到图表中 除了最后一部分之外 一切都正常 保存并关闭文件 我已将尝试保存和关闭文件的尝试标记为绿色 任何帮助表示赞赏 Sub Slide19
  • 对数字和字母元素的数组进行排序(自然排序)

    假设我有一个数组 var arr 1 5 ahsldk 10 55 3 2 7 8 1 2 75 abc huds 我尝试对其进行排序 我得到了类似的东西 1 1 10 2 2 3 5 55 7 75 8 abc ahsldk huds 注
  • 自动计算Excel VBA UDF与单元格属性相关

    我编写了一个 UDF 来计算特定颜色和特定线条样式的单元格 我发布了整个函数 Function CountTime rData As Range cellRefColor As Range As Variant Dim indRefColo
  • 在适用于 Windows 和 Mac 的 VBA 中指定用户文件夹的路径

    我正在编写一个使用 VBA 从 Excel 生成文件的工具 生成的文件将写入用户的 Documents 文件夹中的文件夹 如果存在 e g C Users
  • 按字典顺序对整数数组进行排序 C++

    我想按字典顺序对一个大整数数组 例如 100 万个元素 进行排序 Example input 100 21 22 99 1 927 sorted 1 100 21 22 927 99 我用最简单的方法做到了 将所有数字转换为字符串 非常昂贵
  • 以编程方式将参数传递到访问报告中

    我有一个现有的 Access MDB 我正在向运行现有报表的现有表单添加一个命令按钮 所做的更改是 此按钮需要传入一个包含正在报告的记录 ID 的参数 当前报告在 MDB 中的每条记录上运行 我已经更改了报告运行的查询 以使用 ID 值参数

随机推荐

  • Flutter 中中间有文本的水平分隔线?

    Flutter 中是否有内置小部件来创建中间有文本的分隔线 有关于如何做的指南吗 像这样 水平线中间的 OR 文字 这是我想要实现的屏幕截图 https i stack imgur com VyJXx png 您可以尝试使用Row http
  • NSScanner 循环问题

    我有一个NSScanner扫描 HTML 文档中段落标签的对象 扫描仪似乎在找到的第一个结果处停止 但我需要数组中的所有结果 如何改进我的代码以浏览整个文档 NSArray getParagraphs NSString html NSSca
  • 如何在 YouCompleteMe 弹出菜单中使用 展开 UltiSnips 代码段?

    这个问题确实触动了我的神经 我两个都有你使我完整 https github com ycm core YouCompleteMe and 多功能剪 https github com sirver ultisnips安装在我的 vim 8 0
  • 为什么提交作业失败并显示“NoSuchMethodError: scala.runtime.VolatileObjectRef.zero()Lscala/runtime/VolatileObjectRef;”?

    我正在尝试提交 Spark 作业 它是这样开始的 import javax xml parsers SAXParser SAXParserFactory import org apache spark import org apache s
  • 如何在phpmyadmin中将分隔数据导入mysql

    这是使用中的数据示例 24701 37 279788 81 229023 WV BLUEFIELD MERCER 正如您所看到的 它是由双管道分隔的 phpmyadmin 要求我提供以下信息 Fields Terminated By Fie
  • C中如何使用函数uname

    我应该编写一个函数来获取有关系统的一些信息 最重要的信息是体系结构 我找到了这个功能uname可以使用的包括系统 utsname h 好吧 虽然我用谷歌搜索并阅读了文档 但我找不到该函数的任何示例 而且我不明白如何使用 uname 任何人都
  • 我如何在cmd行中运行maven项目

    我编写 Maven 项目并在 Eclipse 中运行它 但我想使用命令行运行 Maven 项目 所以我写 java jar Dapple awt UIElement true target myproject 1 0 SNAPSHOT ja
  • 使用curl --data测试REST路由,返回404

    我正在尝试 MEAN 堆栈教程 并且我位于 测试初始路由 https thinkster io mean stack tutorial opening rest routes testing the initial routes 步骤 我们
  • Apache CXF 2.2.7 Spring 3 Web 服务解组错误:意外元素

    我使用 Apache CXF 2 2 7 和 Spring 3 开发了一个简单的 Web 服务应用程序 并将其作为 WAR 文件部署到 Tomcat 上 但收到以下错误消息 2010 年 4 月 12 日 15 56 12 org apac
  • 函数代理 .toString() 错误

    我试图在函数代理上调用 toString 简单地创建一个函数代理并调用 toString 会导致 TypeError Function prototype toString is not generic 将 toString 设置为返回原始
  • 如何取消特定用户的会话?

    我有一个这样的会议 SESSION login 当它等于1 这意味着使用已登录到我的网站 if SESSION login 1 You are logged else login register 我还有另一个包含用户 ID 的会话 像这样
  • Moqui 部署到 Elastic Beanstalk Tomcat 实例上的 AWS

    背景 我们已经使用 Java 环境在 Elastic Beanstalk 上运行带有嵌入式 Jetty 服务器的 Moqui 2 0 大约一年了 出于安全原因 我们让应用程序与 Postgres 数据库一起在私有子网中运行 并通过 VPN
  • Mosquitto套接字读取错误Arduino客户端

    我刚刚从 Github 下载了最新的 Arduino 库代码 它破坏了我的 MQTT 客户端程序 我在 Arduino 上使用 PubSubClient 1 91 在 Mac OSX 上使用 Mosquitto 1 1 2 Build 20
  • data.table 1.8.x Mean() 函数自动删除 NA?

    今天我发现我的程序中存在一个错误 原因是data table自动删除NA for mean 例如 gt a lt data table a c NA NA FALSE FALSE b c 1 1 2 2 gt a gt a list mea
  • 监听短信删除android

    我正在寻找用户从收件箱中删除短信的侦听器 Android 中用于删除短信的任何侦听器 https stackoverflow com questions 5025372 any listener for delete sms in andr
  • 经典 ADO.NET 还在使用吗?

    经典的 ADO NET 是否仍然广泛使用并被许多开发人员用于插入 读取数据等 尽管我们现在有 LINQ 和 EF 是的 在某些情况下仍然使用它 在我的日常工作中 我们有几个使用 SQL 大容量复制的情况 这需要良好的连接和命令 此外 SQL
  • 有没有办法刷新 POSIX 套接字?

    是否有一个标准调用可以将 POSIX 套接字的发送端一直刷新到远程端 或者是否需要将其作为用户级协议的一部分来实现 我查看了常见的标题 但找不到任何东西 设置 TCP NODELAY 然后将其重置回来怎么样 也许可以在发送重要数据之前或完成
  • 使用未知分隔符进行 Unix 排序(最后一列)

    我的数据如下所示 Adelaide Crows 5 2 3 0 450 455 460 67 8 Essendon 5 5 0 0 622 352 955 88 20 Fremantle 5 3 2 0 439 428 598 50 12
  • 使用 ffmpeg 添加不透明度叠加

    我的 ffmpeg 有问题 我尝试在视频上添加 png 文件 我发现如何添加 只是我希望这个 png 文件有一些不透明度 我尝试了这条线 ffmpeg n i video mp4 i logo png filter complex sets
  • 对有连字符的字母数字数据进行排序

    我的工作簿中有两张表 每张表都有自己的电子邮件地址列以及其他数据 我将引用 Sheet1 中的 Column1 和 Sheet2 中的 Column2 其中只有 Column1 可能列出了重复的电子邮件地址 我需要确定 Column1 中的