我对VBA和Google Sheets应用程序脚本都相当不熟悉。
话虽如此,我昨天能够谷歌足够弄清楚我想在VBA中实现什么。为了让分享我的电子表格更容易,我想试着把它转换成谷歌表单。我已经猜出了大部分,除了这一部分。
这是主表(只有一个人需要看到)。在Excel中,人们可以将数据(以正确的格式)粘贴到B列的橙子输入字段中,然后将文本拆分到其下方的黄色字段中。
C列中的字段将使用B列中的键根据多个参考工作表的vlookup自动更新。
从这里,我可以编辑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
1条答案
按热度按时间jdgnovmf1#
只是为了有一个答案,为其他任何人谷歌,我确实有它的工作。当然可以改进,但它以相同方式工作的第一次迭代如下。不像excel那样即时,但功能相同