excel - 按数据类型隔离数据

标签 excel vba

我有一些数据(A 列中的混合数据类型)。如何将每种数据类型拆分为另一列?
我的意思是数字在列中,字符串在列中,日期在列中等等
这是我到目前为止的尝试,但我没有得到预期的所有结果

Sub Test()
    Dim a, b(), dic As Object, i As Long, k As Long, ii As Long, n As Long
    a = Range("A1:A10").Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(a) To UBound(a)
        If Not dic.Exists(VarType(a(i, 1))) Then
            dic.Item(VarType(a(i, 1))) = Empty
            ReDim Preserve b(UBound(a, 1), k)
            k = k + 1
        End If
        n = 0
        Do Until b(i - 1, k - 1) <> Empty
            b(i - 1, k - 1) = a(i, 1)
        Loop
    Next i
    Range("J1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
enter image description here

最佳答案

在您的代码中,我会做很多不同的事情

  • 为变量使用有意义的名称
  • 指定工作表而不是依赖于隐含的 ActiveSheet
  • 清除工作表上的结果区域
  • 字典对象的早期绑定(bind)
  • 动态确定要处理的范围

  • 但是下面的代码会根据您的屏幕截图对您的原始代码进行最低限度的修改,以获得我认为您想要的输出
    Sub Test()
        Dim a, b(), dic As Object, i As Long, k As Long, ii As Long, n As Long, v
        Dim dataType As String
    
    a = Range("A1:A10").Value
    ReDim b(1 To UBound(a))
    
    'first create the dictionary with the datatypes
    'since you are maintaining the entries in the same rows,
    '   add an empty array as the item
    Set dic = CreateObject("Scripting.Dictionary")
        dic.Add Key:="number", Item:=b
        dic.Add Key:="date", Item:=b
        dic.Add Key:="string", Item:=b
        dic.Add Key:="logical", Item:=b
        
    'Add the values to the correct dictionary item
    ' at the correct spot in the array
    For i = LBound(a) To UBound(a)
        Select Case VarType(a(i, 1))
            Case 2 To 6
                dataType = "number"
            Case 7
                dataType = "date"
            Case 8
                dataType = "string"
            Case 11
                dataType = "logical"
            Case Else
                dataType = ""
        End Select
        
        If dataType <> "" Then
            v = dic(dataType)
            v(i) = a(i, 1)
            dic(dataType) = v
        End If
    Next i
    
    'Next create output array
    ReDim b(1 To UBound(a), 1 To dic.Count)
    k = 0
    For Each v In dic.Keys
        k = k + 1
        For i = 1 To UBound(dic(v))
            b(i, k) = dic(v)(i)
        Next i
    Next v
    
    Range("J1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    
    End Sub
    
    编辑:
    如果正如您在评论中指出的那样,您最初不想设置数据类型,您也可以在创建字典对象时执行此操作。使用相同的算法将项目存储为与数据库中的行数相同大小的数组:
    Sub Test()
        Dim a, b(), dic As Object, i As Long, k As Long, ii As Long, n As Long, v
        Dim dataType As Long
    a = Range("A1:A10").Value
    ReDim b(1 To UBound(a))
    
    Set dic = CreateObject("Scripting.Dictionary")
    
    'Add the values to the correct dictionary item
    ' at the correct spot in the array
    For i = LBound(a) To UBound(a)
        dataType = VarType(a(i, 1))
        If a(i, 1) <> "" Then
            If Not dic.Exists(dataType) Then
                ReDim b(UBound(a))
                b(i) = a(i, 1)
                dic.Add Key:=dataType, Item:=b
            Else
                b = dic(dataType)
                b(i) = a(i, 1)
                dic(dataType) = b
            End If
        End If
                
    Next i
    
    'Next create output array
    ReDim b(1 To UBound(a), 1 To dic.Count)
    k = 0
    For Each v In dic.Keys
        k = k + 1
        For i = 1 To UBound(dic(v))
            b(i, k) = dic(v)(i)
        Next i
    Next v
    
    Range("J1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    
    End Sub
    
    enter image description here

    关于excel - 按数据类型隔离数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71005114/

    相关文章:

    excel - 根据包含日期的另一行查找两行的加权平均值

    Excel VBA 隐藏表单控制按钮

    vba - 如何在 VBA 过程中使用 optional 数组参数?

    excel - 从excel VBA代码中的电子邮件正文中提取表格

    excel - VBA CopyPicture 在锁定屏幕上失败 ("error 1004 cannot empy clipboard")

    VBA VLOOKUP 转换为值给出 #N/A

    excel - 如何在使用 Excel VBA 搜索后清除搜索条件和单词

    vba - 使用vba更改单词表中特定单元格的颜色和字体

    excel - 随机化一组值而不重复值索引

    excel - 如何创建输出一系列单元格内容的 Excel 函数