为列中的每一行创build文本框

我试图find一个VBA代码,这将遍历表中的特定列中的行,并为每个迭代行创build一个文本框,并将其设置为该单元格的值。

从这里的代码插入一个文本框,并设置公式的工作,以创build一个文本框,但我需要修改它,以便它遍历整个列。

我试图做这样一个循环:

Sub addTextBox() Dim newshp As Shape Dim newtb As TextBox Dim i As Long For i = 1 To Range("F" & Rows.Count).End(xlUp).Row Set newshp = ActiveSheet.Shapes.addTextBox _ (msoTextOrientationHorizontal, 100, 100, 200, 80) Set newtb = newshp.OLEFormat.Object newtb.Formula = "Cells.Item(i, 6)" Next i End Sub 

所以, newtb.Formula = "Cells.Item(i, 6)" – 这是我有问题的地方; 显然。 .Formula只能引用静态单元格。

我真的很感激有关如何使这个循环正确的任何提示。

Formula应该是一个Excel公式。 所以你需要将"Cells.Item(i, 6)"改为"=F" & i

 Sub addTextBox() Dim newshp As Shape Dim newtb As TextBox Dim i As Long For i = 1 To Range("F" & Rows.Count).End(xlUp).Row Set newshp = ActiveSheet.Shapes.addTextBox _ (msoTextOrientationHorizontal, 100, 100, 200, 80) Set newtb = newshp.OLEFormat.Object newtb.Formula = "=F" & i Next i End Sub 

这样可以解决你眼前的问题,但是你需要找出你想放置文本框的位置 – 这时你将它们全部放在一起。

如果你想把它们放在G列中,并完全适应单元格,你可以尝试这样的事情…

 Sub addTextBox() Dim newshp As Shape Dim newtb As TextBox Dim i As Long Dim cell As Range Dim vLeft, vTop, vWidth, vHeight For i = 1 To Range("F" & Rows.Count).End(xlUp).Row Set cell = Range("G" & i) With cell vLeft = .Left vTop = .Top vWidth = .Width vHeight = .Height End With Set newshp = ActiveSheet.Shapes.addTextBox _ (msoTextOrientationHorizontal, vLeft, vTop, vWidth, vHeight) Set newtb = newshp.OLEFormat.Object newtb.Formula = "=F" & i Next i End Sub