简体   繁体   English

表单组合框上的 Excel VBA 循环单元格链接

[英]Excel VBA Loop Cell Link on Form Combobox

I'm needing a Function that can change the cell link on form control Combo Boxes that already exist on an active sheet using a loop.我需要一个函数,它可以使用循环更改已存在于活动工作表上的表单控件组合框上的单元格链接。 I can currently achieve this with out a loop as follows我目前可以在没有循环的情况下实现这一点,如下所示

ActiveSheet.Shapes("Combobox 1").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 2).Address
ActiveSheet.Shapes("Combobox 2").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 3).Address
ActiveSheet.Shapes("Combobox 3").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 4).Address
ActiveSheet.Shapes("Combobox 4").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 5).Address
ActiveSheet.Shapes("Combobox 5").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 6).Address
ActiveSheet.Shapes("Combobox 6").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 7).Address
ActiveSheet.Shapes("Combobox 7").OLEFormat.Object.LinkedCell = Worksheets("Test").Cells(2, 8).Address

So I want to use a loop rather than having to do this for every combobox on my sheet, the loop needs to increment the .Cell row number and .Shape("Combobox 1").所以我想使用循环而不是对我的工作表上的每个组合框都这样做,循环需要增加 .Cell 行号和 .Shape("Combobox 1")。 I'm stuck in writing the loop because the combo box is using a name or string value and not an int value.我一直在编写循环,因为组合框使用的是名称或字符串值而不是 int 值。

Any help on this would be much appreciated对此的任何帮助将不胜感激

Use this format:使用这种格式:

Dim ws1 As Worksheet, ws2 As Worksheet, i As Long, os As Long

Set ws1 = ActiveSheet
Set ws2 = Worksheets("Test")

os = 1     'change i Offset as needed

For i = 1 To 7
    ws1.Shapes("Combobox " & i).OLEFormat.Object.LinkedCell = ws2.Cells(2, i + os).Address
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM