You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Calc自定义单元格拆分函数存在括号不匹配问题,请求排查修复

Fixing Syntax & Logic Issues in Your Calc VBA Split Function

Let's walk through fixing your function step by step. You mentioned a parentheses mismatch, but there are a few other VBA-specific quirks (leftover from your Google Sheets JS port) that were preventing the function from running correctly.

That line with Indexof was the main culprit. A couple issues here:

  • VBA doesn't use Indexof (that's JavaScript/Google Apps Script syntax) — we need InStr, VBA's built-in function for checking if a character exists in a string.
  • > is an HTML escape code for >; VBA won't recognize that, so we need to use the actual > operator.
  • The parentheses structure was messed up from mixing JS and VBA syntax.

Original problematic line:

If ("’aeiou".Indexof( inputstr(i) ) > 0) Then

Fixed version:

If InStr("’aeiou", inputstr(i)) > 0 Then

2. Fix Variable Type for last_space_idx

You declared last_space_idx as a String, but it's storing a numeric index — this would cause a type mismatch error every time you tried to assign a number to it. Switch it to Integer:

Dim last_space_idx As Integer

3. Fix VBA's 1-Based String Indexing

Unlike JavaScript, VBA strings use 1-based indexing (the first character is at position 1, not 0). Your original loop started at 0, which would throw an error when trying to access inputstr(0). Adjust the loop setup:

Dim l As Integer
l = Len(inputstr) ' No need to subtract 1 now that we're looping from 1 to l
For i = 1 To l

4. Refine Logic to Match Your Goal

Your original logic set sidx to the last space index every time it hit a target character, then overwrote it again at the end. I tweaked it to either capture the first occurrence of a target character (and exit early to save processing) or fall back to the last space if no target characters are found — which aligns with your goal of splitting before the word containing specific characters.

Full Corrected Function

Here's the fully working version with all fixes applied:

Function sidx(inputstr As String) As Integer
    Dim newString As String
    newString = ""
    Dim last_space_idx As Integer
    last_space_idx = 0
    Dim l As Integer
    l = Len(inputstr)
    
    For i = 1 To l
        If inputstr(i) = " " Then
            last_space_idx = i
        End If
        ' Check if current character is in our target set (’aeiou)
        If InStr("’aeiou", inputstr(i)) > 0 Then
            sidx = last_space_idx
            ' Exit loop early once we find the first match (remove this line if you want the LAST match)
            Exit For
        End If
    Next i
    
    ' Fallback: if no target characters found, return the last space index
    If sidx = 0 Then
        sidx = last_space_idx
    End If
End Function

Quick Testing Tip

To verify it works, try calling it in a Calc cell like =sidx("Hello world’s example") — it should return 6 (the space before "world’s", which contains the ’ character).

内容的提问来源于stack exchange,提问作者Oren Bochman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:18:23