【问题标题】:vba expected end subvba 预期结束子
【发布时间】:2014-06-11 13:51:45
【问题描述】:

此程序旨在从包含数组“名称”中包含的值的数据中选择行。然后将选定的行剪切粘贴到不同的工作表中。

当我运行此代码时,我不断收到错误“编译错误:预期结束子”。我环顾了几个地方,似乎没有任何效果。

编辑:使用下面的函数将行数组更改为字符串数组。同样的错误。

编辑:从函数中删除了“设置”。同样的错误=/

Function IsInArray(ToBeFound, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, ToBeFound)) > -1)
End Function


Function RangeToStringArray(myRange as range) as String() 'Credit to JA Daling
    ReDim strArray(myRange.Cells.Count - 1) As String
    Dim idx As Long
    Dim c As Range

    For Each c In myRange
        strArray(idx) = c.Text
        idx = idx + 1
    Next c
    RangeToStringArray = strArray
End Function

Sub Removedata()
    Dim i As Integer: i = 12
    Dim names: names = RangeToStringArray(Sheets("Pivot 1").Range("F18:F44"))
    While i < 554
        If IsInArray(Sheets("Raw data test").Cells(i, 16).Value, names) = True Then
            Sheets("Raw data test").Range(Sheets("Raw data test").Cells(1, 16), Sheets("Raw data test").Cells(26, 16)).Cut
            Sheets("Raw Data Out of Scope").Select
            ActiveSheet.Paste
        End If
        i = i + 1
    Wend
End Sub

【问题讨论】:

  • (Filter(arr, ToBeFound) 这是一个类型不匹配的开始。第二个参数必须是 String 而不是数组
  • 这是模块中的所有代码吗?不要使用: 方法来声明变量,那也不是最易读的代码。
  • 这是模块中的所有代码,是的。
  • 无法测试代码,但只要您从Set strArray(idx) = c.Text中删除Set,它确实编译。
  • 注意:Filter 可能不会给你你想要的,根据我返回部分匹配的经验(例如,如果你搜索“50”,如果数组包含“500”,它将返回 true等)

标签: function loops excel vba


【解决方案1】:

您正在尝试 Set 一个 String 变量。

将此行:Set strArray(idx) = c.Text 更改为以下内容

strArray(idx) = c.Text

我是通过单击 VBA 窗口菜单中的Debug &gt; Compile VBA Project 找到的。

【讨论】:

    【解决方案2】:

    好吧,我想通了!无论出于何种原因,将代码复制粘贴到一个新模块中就可以了。谢谢大家帮忙解决这个问题!

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      相关资源
      最近更新 更多