Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba CDbl()的问题和类型不匹配_Vba_Excel - Fatal编程技术网

Vba CDbl()的问题和类型不匹配

Vba CDbl()的问题和类型不匹配,vba,excel,Vba,Excel,我是VBA的新手,在编写脚本时遇到了一些问题。它应该做的是在单个单元格中使用分号分隔的数据(数据包含括号内的名称和数字),并找到数字最大的名称,然后将其分离到不同的单元格中 要从字符串中取出数字,我使用ozgrid开发的自定义函数ExtractNumber: 我一直在 错误424 在尝试调用该函数时,恰好在其最后一行: ExtractNumber = CDbl(lNum) 正如另一位用户所建议的,我尝试在此处切换变量类型: ExtractNumber(rCell As String, 但这

我是VBA的新手,在编写脚本时遇到了一些问题。它应该做的是在单个单元格中使用分号分隔的数据(数据包含括号内的名称和数字),并找到数字最大的名称,然后将其分离到不同的单元格中

要从字符串中取出数字,我使用ozgrid开发的自定义函数ExtractNumber:

我一直在

错误424

在尝试调用该函数时,恰好在其最后一行:

ExtractNumber = CDbl(lNum)
正如另一位用户所建议的,我尝试在此处切换变量类型:

ExtractNumber(rCell As String, 
但这毫无帮助。我还尝试过将CDbl()命令切换到任何其他C..()类型的命令,并将其完全删除。不走运。我感觉自己就像撞上了一堵巨大的砖墙,我的时间正在慢慢流逝。我可以再次请求帮助吗?这是我的全部代码:

Option Explicit

Sub Divide()

Dim txt As String
Dim i As Integer
Dim j As Integer
Dim Full As Variant
Dim a As Integer
Dim b As Integer
Dim stored() As Integer

txt = (CStr(ActiveCell.Value))
Full = Split(txt, ";")
a = UBound(Full)
b = a - 1

ReDim stored(b)

For i = 0 To a
    stored(i) = ExtractNumber((Full(i)))
Next i

Dim primary_index As Integer
Dim primary_no As Integer
Dim primary_name As String
primary_index = Application.Match(Application.Max(stored), stored, 0)
primary_no = stored(primary_index)
primary_name = Full(primary_index)
stored(primary_index) = 0

If UBound(stored) > 1 Then
    Dim secondary_index As Integer
    Dim secondary_no As Integer
    Dim secondary_name As String
    secondary_index = Application.Match(Application.Max(stored), stored, 0)
    secondary_no = stored(secondary_index)
    secondary_name = Full(secondary_index)
End If

For i = 0 To 6
    ActiveCell.EntireColumn.Offset(0, 1).Insert
Next i

If UBound(stored) > 2 Then
    Dim names() As String
    ReDim names(0 To a)
    For j = 0 To a
        If Not (j = primary_index Or j = secondary_index) Then
            names(j) = Full(j)
        End If
    Next j

    ActiveCell.Offset(0, 1).Value = primary_name
    ActiveCell.Offset(0, 2).Value = primary_no
    ActiveCell.Offset(0, 3).Value = secondary_name
    ActiveCell.Offset(0, 4).Value = secondary_no
    ActiveCell.Offset(0, 5).Value = names
    ActiveCell.Offset(0, 6).Value = (ActiveCell.Offset(0, 8).Value - primary_no             - secondary_no)

ElseIf UBound(stored) = 2 Then
    ActiveCell.Offset(0, 1).Value = primary_name
    ActiveCell.Offset(0, 2).Value = primary_no
    ActiveCell.Offset(0, 3).Value = secondary_name
    ActiveCell.Offset(0, 4).Value = secondary_no
End

Else
    ActiveCell.Offset(0, 1).Value = primary_name
    ActiveCell.Offset(0, 2).Value = primary_no
End

End If
End Sub
以下是我的数据示例:


单元格内容示例:
a&W全美国食品(1);美国派咖啡馆(1);阿比氏(53);安妮阿姨家(13);安妮阿姨的手卷软椒盐卷饼(1);巴斯金·罗宾斯(1);牛扒牛肉(1);大牛餐厅;比尔·埃利斯烧烤店(1);早餐/汤吧(116);百老汇餐厅(4);汉堡王(4);切斯特鸡(2只);朱砂(126);农村市场(1);乡村煎锅(1);古巴美食餐厅(1);奶业皇后(23),;丹氏大片比萨饼(1);破晓咖啡馆(1);熟食店(17);丹尼学院(97);邓肯甜甜圈(6);家庭餐厅(1);全方位服务(4);金栏(2);醋栗农场(1);马克斯奶奶(3);哈代(1);热菜和比萨饼(44);热馅饼(3);拥挤的房子(1);IHOP餐厅(1);J’s炒锅和烤架(1);约翰尼熏牛肉(1);朱尼餐厅(1);肯德基(3);Krispy Krunchy鸡(1);长约翰·西尔弗(1);马克斯公路餐车(1);麦当劳(39家);墨西哥烤肉(1);里程碑餐厅(3);教育部西南烤架(6);内森著名酒店(1);诺布尔罗马比萨饼(1);佩恩80烤架(1);必胜客(4);比萨饼店(一);;Q(1);奎兹诺斯(2);山姆巴斯牛排馆(1);斯巴罗(1);银煎锅(1);地铁(231);阳光咖啡厅(1);塔可钟(8);塔可钟/肯德基(1);塔可约翰餐厅(1);Wendy的老式汉堡包(72)

我想问题出在用于浮点数的十进制分隔符上。 你能这样试试吗

ExtractNumber(change_commas(Full(i)))

Public Function change_commas(ByVal myValue As Variant) As String
   Dim str_temp as String
   str_temp = CStr(myValue)
   change_commas = Replace(str_temp, ",", ".")
End Function

也许像这样的事情可以奏效

这些值被拆分并放置在临时工作表上进行排序。VBA中的数组排序可能更快

这将前两行视为“主要”和“次要”品牌,即使它们的得分相同。所有其他商店都只是“其他商店”

如果一个商店没有一个数字,它会被指定一个默认的数字0

Sub Test()

    With ThisWorkbook.Worksheets("Arkusz1")
        Divide .Range("G1:G7")
    End With

End Sub


Sub Divide(TextRange As Range)

    Dim rCell As Range, rCell1 As Range
    Dim vSplit As Variant
    Dim wrkShtTmp As Worksheet
    Dim sOthers As String

    'The parent of the range is the worksheet.
    'The parent of the worksheet is the workbook - we want to add a worksheet to the workbook.
    Set wrkShtTmp = TextRange.Parent.Parent.Worksheets.Add

    For Each rCell In TextRange
        With wrkShtTmp
            vSplit = Split(rCell, ";")
            'Place the values on the temporary sheet.
            .Range("A1").Resize(UBound(vSplit)) = Application.Transpose(vSplit)

            'Remove number & brackets from name.
            '=TRIM(LEFT(A1,FIND("(",A1)-1))
            .Range("B1").Resize(UBound(vSplit)).FormulaR1C1 = _
                "=IFERROR(TRIM(LEFT(RC[-1],FIND(""("",RC[-1])-1)),RC[-1])"

            'Place numbers in column C.
            '=IFERROR(VALUE(SUBSTITUTE(MID(A1,FIND("(",A1)+1,LEN(A1)),")","")),0)
            .Range("C1").Resize(UBound(vSplit)).FormulaR1C1 = _
                "=IFERROR(VALUE(SUBSTITUTE(MID(RC[-2],FIND(""("",RC[-2])+1,LEN(RC[-2])),"")"","""")),0)"

            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=wrkShtTmp.Range("C1").Resize(UBound(vSplit)), _
                    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                .SetRange wrkShtTmp.Range("A1").Resize(UBound(vSplit), 3)
                .Header = xlNo
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With

            'Place the top two in columns 22 & 23 (V & W)
            TextRange.Parent.Cells(rCell.Row, 22) = .Cells(1, 2)
            TextRange.Parent.Cells(rCell.Row, 23) = .Cells(2, 2)

            'Stick all the other stores together in a string.
            For Each rCell1 In .Range("B3").Resize(UBound(vSplit) - 2)
                sOthers = sOthers & rCell1 & ", "
            Next rCell1
            sOthers = Left(sOthers, Len(sOthers) - 2)
            'Place the other stores in column 24 (X)
            TextRange.Parent.Cells(rCell.Row, 24) = sOthers

            'Clear the temporary sheet and Other stores string.
            .Range("A1").Resize(UBound(vSplit), 3).ClearContents
            sOthers = ""

        End With
    Next rCell

    'Delete the temporary sheet.
    Application.DisplayAlerts = False
    wrkShtTmp.Delete
    Application.DisplayAlerts = True

End Sub

为了重现您的问题,您需要一个数据样本。请阅读,如果您的列表被拆分为带有括号中数字的名称(
Person1(12);Person2(10)
),则
ExtractNumber
函数有点冗长。你只需要找到括号。如果全名和数字在括号中(
(Person1 12);(Person2 10)
),那么您可能会在数字前寻找空格。正如罗恩所说,你的数据样本会有所帮助。@DarrenBartrup Cook我已经包括了我的数据样本,我希望它足够了!编号最大的名字是什么意思?都是最高的吗?所有的两个或三个最高的@RonRosenfeld这两个最高的品牌分别成为“一级品牌”和“二级品牌”,其数量在不同的字段中表示。其余部分放在另一个字段中,称为“其他”。