倒数查找功能-帮助从 VBA 转换为谷歌表格应用程序脚本

问题描述 投票:0回答:0

我对 VBA 和尤其是 google sheets App Scripts 都不熟悉。

话虽这么说,但我昨天能够通过谷歌搜索来弄清楚我想在 VBA 中实现什么。为了更轻松地共享我的电子表格,我想尝试将其转换为谷歌表格。我已经弄明白了大部分,除了这个特定的部分。

这是主表(只有一个人需要看)。在 Excel 中,人们可以将数据(以正确的格式)粘贴到 B 列的橙色输入字段中,并将文本拆分为下方的黄色字段。

Main Sheet

C 列中的字段基于使用 B 列中的键对多个参考表的 vlookup 自动更新。

Example reference sheet

从这里我可以编辑 B 列或 C 列,并更新另一列以保持同步。我不得不将 A 列复制到参考表中的 C 列,因为 vlookup 只能从左到右工作,我无法让 xlookup 工作。

这是我正在使用的草率的 VBA 代码。我敢肯定这是一团糟,但它非常适合我需要的东西

Private Sub Worksheet_change(ByVal Target As Range)
If Target.Address = "$B$2" Then
    If Len("B2") > 1 Then
        Dim regex As Object
        Dim X As Variant
        Dim Y As Variant
        Set regex = CreateObject("VBScript.RegExp")
        regex.Pattern = "font[0-9]{1,2}="
        Y = Replace(Range("B2").Value, "(", "")
        Y = Replace(Y, ")", "")
        Y = regex.Replace(Y, "")
        X = Split(Y, ",")
        Range("B3").Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
        Range("C6").Value = Application.WorksheetFunction.VLookup(Range("B6"), Sheets("A (Hum)").Range("A:B"), 2, 0)
        Range("C7").Value = Application.WorksheetFunction.VLookup(Range("B7"), Sheets("B (Blaster)").Range("A:B"), 2, 0)
        Range("C8").Value = Application.WorksheetFunction.VLookup(Range("B8"), Sheets("C (Force)").Range("A:B"), 2, 0)
        Range("C9").Value = Application.WorksheetFunction.VLookup(Range("B9"), Sheets("D (Lockup)").Range("A:B"), 2, 0)
        Range("C10").Value = Application.WorksheetFunction.VLookup(Range("B10"), Sheets("E (FoC)").Range("A:B"), 2, 0)
        Range("C11").Value = Application.WorksheetFunction.VLookup(Range("B11"), Sheets("F (Ignition)").Range("A:B"), 2, 0)
    End If
End If

'Stop the reciprocal vlookups causing an error
Application.EnableEvents = False

' IF Column B is selected, changed column C
If Target.Address = "$B$6" Then
    Range("C6").Value = Application.WorksheetFunction.VLookup(Range("B6"), Sheets("A (Hum)").Range("A:B"), 2, 0)
End If
If Target.Address = "$B$7" Then
    Range("C7").Value = Application.WorksheetFunction.VLookup(Range("B7"), Sheets("B (Blaster)").Range("A:B"), 2, 0)
End If
If Target.Address = "$B$8" Then
    Range("C8").Value = Application.WorksheetFunction.VLookup(Range("B8"), Sheets("C (Force)").Range("A:B"), 2, 0)
End If
If Target.Address = "$B$9" Then
    Range("C9").Value = Application.WorksheetFunction.VLookup(Range("B9"), Sheets("D (Lockup)").Range("A:B"), 2, 0)
End If
If Target.Address = "$B$10" Then
    Range("C10").Value = Application.WorksheetFunction.VLookup(Range("B10"), Sheets("E (FoC)").Range("A:B"), 2, 0)
End If
If Target.Address = "$B$11" Then
    Range("C11").Value = Application.WorksheetFunction.VLookup(Range("B11"), Sheets("F (Ignition)").Range("A:B"), 2, 0)
End If

' And Vice Versa
If Target.Address = "$C$6" Then
    Range("B6").Value = Application.WorksheetFunction.VLookup(Range("C6"), Sheets("A (Hum)").Range("B:C"), 2, 0)
End If
If Target.Address = "$C$7" Then
    Range("B7").Value = Application.WorksheetFunction.VLookup(Range("C7"), Sheets("B (Blaster)").Range("B:C"), 2, 0)
End If
If Target.Address = "$C$8" Then
    Range("B8").Value = Application.WorksheetFunction.VLookup(Range("C8"), Sheets("C (Force)").Range("B:C"), 2, 0)
End If
If Target.Address = "$C$9" Then
    Range("B9").Value = Application.WorksheetFunction.VLookup(Range("C9"), Sheets("D (Lockup)").Range("B:C"), 2, 0)
End If
If Target.Address = "$C$10" Then
    Range("B10").Value = Application.WorksheetFunction.VLookup(Range("C10"), Sheets("E (FoC)").Range("B:C"), 2, 0)
End If
If Target.Address = "$C$11" Then
    Range("B11").Value = Application.WorksheetFunction.VLookup(Range("C11"), Sheets("F (Ignition)").Range("B:C"), 2, 0)
End If

'Re-enable events after updates
Application.EnableEvents = True

End Sub

excel vba google-apps-script google-sheets vlookup
© www.soinside.com 2019 - 2024. All rights reserved.