将单元格的值复制到文本框中
copy value of cell into textbox
这就是我想做的事情:
每次单击复选框时,
- 它旁边的单元格将被复制
-找到这个文本框
- 在文本框中查找字符串的结尾(如果有)
- 并将复制的单元格(值)粘贴到此文本框
我想出了如何复制选中的单元格,但是我试图将其粘贴到文本框中的所有代码都不起作用。
以下是我目前得到的
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
shp.TopLeftCell.Offset(1, 1).Select ' 1 COL below checkbox
Selection.Copy 'copy the cell next to the checkbox
Call UpdateTextBox
Set shp = Nothing
End Sub
Sub UpdateTextBox()
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
strPaste = DataObj.GetText(1) ' get ext from clipboard
End Sub
试试这个:
Sub checkBoxHandler()
Dim Shp As Shape
Dim sTxt As String
Set Shp = ActiveSheet.Shapes(Application.Caller)
sTxt = Shp.TopLeftCell.Offset(1, 1).Value2 'Verify this line against the cell you want to copy as the cell below is .Offset(1,0)
Call UpdateTextBox(ActiveSheet, sTxt)
Set Shp = Nothing
End Sub
Sub UpdateTextBox(WshTrg As Worksheet, sTxt As String)
Dim oOleObj As OLEObject
Set oOleObj = WshTrg.OLEObjects("TextBox1") 'replace TextBox1 with the name of your Text Box
With oOleObj.Object
.Value = .Value & " " & sTxt
End With
End Sub
这对我有用,请确保工作表和文本框名称与您的匹配。
Sub ChangeText()
Dim r As Range
Dim sh As Shape
Set sh = Sheets("Sheet1").Shapes("Textbox 1")
Set r = ActiveSheet.Shapes(Application.Caller).TopLeftCell 'find the range of the button clicked.
sh.TextFrame.Characters.Text = r.Offset(1,1)
End Sub
这就是我想做的事情:
每次单击复选框时, - 它旁边的单元格将被复制 -找到这个文本框 - 在文本框中查找字符串的结尾(如果有) - 并将复制的单元格(值)粘贴到此文本框
我想出了如何复制选中的单元格,但是我试图将其粘贴到文本框中的所有代码都不起作用。
以下是我目前得到的
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
shp.TopLeftCell.Offset(1, 1).Select ' 1 COL below checkbox
Selection.Copy 'copy the cell next to the checkbox
Call UpdateTextBox
Set shp = Nothing
End Sub
Sub UpdateTextBox()
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
strPaste = DataObj.GetText(1) ' get ext from clipboard
End Sub
试试这个:
Sub checkBoxHandler()
Dim Shp As Shape
Dim sTxt As String
Set Shp = ActiveSheet.Shapes(Application.Caller)
sTxt = Shp.TopLeftCell.Offset(1, 1).Value2 'Verify this line against the cell you want to copy as the cell below is .Offset(1,0)
Call UpdateTextBox(ActiveSheet, sTxt)
Set Shp = Nothing
End Sub
Sub UpdateTextBox(WshTrg As Worksheet, sTxt As String)
Dim oOleObj As OLEObject
Set oOleObj = WshTrg.OLEObjects("TextBox1") 'replace TextBox1 with the name of your Text Box
With oOleObj.Object
.Value = .Value & " " & sTxt
End With
End Sub
这对我有用,请确保工作表和文本框名称与您的匹配。
Sub ChangeText()
Dim r As Range
Dim sh As Shape
Set sh = Sheets("Sheet1").Shapes("Textbox 1")
Set r = ActiveSheet.Shapes(Application.Caller).TopLeftCell 'find the range of the button clicked.
sh.TextFrame.Characters.Text = r.Offset(1,1)
End Sub