【问题标题】:Create one dimensional array by splitting each cell value in multiple integers delimited by a character, ignoring other characters通过将每个单元格值拆分为由字符分隔的多个整数来创建一维数组,忽略其他字符
【发布时间】:2020-06-07 00:16:42
【问题描述】:

从多列范围我想在一个过程中通过将每个单元格值(如果需要)拆分为多个字符串来创建一维数组?,转换?为整数。值将由特定字符分隔,也必须忽略其他字符

这... ..会导致 1、2、3、4、7、9、11、13、54、67

我现在正在使用的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False

Dim arr As Variant
arr = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Value   'Convert to 2-dim and use non numerical values from adjacent column, see "Excha.."

Dim varr As Variant
varr = Range("C1:E" & Range("A" & Rows.Count).End(xlUp).Row).Value  'Split if some of the cells contains multiple values delimited by character??

Dim M As Integer
M = Application.WorksheetFunction.Max(r.Offset(, 2))                'Exchange cell ref to Max(len("x2"

TextBox1.Text = ""

Dim x, y, match As Boolean, i As Integer, L As Integer

i = 1

For Each x In arr
    match = False
    For Each y In varr
        If x = y Then
            match = True
            Exit For
        End If
    Next y
    If Not match And Len(x) <> "" Then
        If i = 1 Then
            TextBox1.Text = x & ". " & Worksheets(1).Cells(x, 2).Value                                  'Exchange cell ref to "x2"
        Else
            TextBox1.Text = TextBox1.Text & String(L, " ") & x & ". " & Worksheets(1).Cells(x, 2).Value 'Exchange cell ref to "x2"
        End If
        L = M + 5 - (Len(Worksheets(1).Cells(x, 1).Value) + Len(Worksheets(1).Cells(x, 2).Value))       'Exchange cell ref to len(x)& len("x2")
    End If
        i = i + 1
    End If
Next

Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 你能简单解释一下你的代码在哪里失败了吗?问题是什么?一旦您解释我们将能够提供帮助
  • 为什么这是 Worksheet_Change?
  • 您的意思是 1 个逗号分隔的字符串,其中包含该范围内的所有数值吗?如果是这种情况,我会考虑正则表达式。
  • @Pierre44 抱歉,该代码仅适用于包含整数的单元格,将其发布为上下文
  • @Jeeped 它不断用可用资源更新文本框

标签: arrays excel vba


【解决方案1】:

您可以使用正则表达式轻松完成此操作

Option Explicit

Sub TestExtract()
    Dim Arr As Variant
    Arr = ExtractNumbers(Worksheets("Sheet1").Range("A1:F10")) 'specify which range to analyze

    Debug.Print Join(Arr, "; ") 'just to visualize the array
End Sub

Public Function ExtractNumbers(Target As Range) As Variant
    Dim regEx As Object
    Set regEx = CreateObject("vbscript.regexp")

    Dim regExMatches As Object, regExMatch As Object
    Dim Result As String

    Dim Cell As Range
    For Each Cell In Target 'loop through each cell
        If Cell.Value <> vbNullString Then
            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = "[0-9]+"
            End With

            'find all integers in each cell
            Set regExMatches = regEx.Execute(Cell.Value)
            For Each regExMatch In regExMatches
                Result = Result & regExMatch & ";"
            Next regExMatch
        End If
    Next Cell

    ExtractNumbers = Split(Left$(Result, Len(Result) - 1), ";") 'convert into array
    'sort array here if needed
End Function

请注意,我没有展示数组排序,因为已经有 100 万个教程。

【讨论】:

  • 谢谢!对此不太熟悉 :) n 本身它工作得很好,我会尝试以某种方式在我的代码中实现它。由于有一百万个教程,您是否碰巧知道一个适用于此的教程,以及如何避免其中的重复值
  • Sort ArrayRemove Duplicates from Array 应该可以帮到你。
  • 哈哈,好吧!认为会有像“If Not .exists()”这样的方式来添加循环和类似“.Sort”的东西。不过谢谢!这会让我忙一阵子:)
  • 这两个似乎没有可比性 varr = ExtractNumbers(Range(nm.Name)) & arr = r.Value 任何想法?两者都显示为变体
  • 好的,我无法在评论中澄清这一点。您的问题是 “通过将每个单元格值拆分为由字符分隔的多个整数,忽略其他字符来创建一维数组” 并且函数 ExtractNumbers 确实返回给定中所有整数的一维数组范围。如果您现在有一个新问题,请提出一个新问题并在那里显示您的实际代码。新问题无法在 cmets 中回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多