vba 在其他工作表上的其他文本框中复制文本框中的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2907095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:40:00  来源:igfitidea点击:

Duplicate what is in the Textbox in other Textboxes on other sheets

excelvbatextboxexcel-vba

提问by Daniel

I have four sheets in a spreadsheet.

我在电子表格中有四张纸。

On each sheet there is a textbox.

每张纸上都有一个文本框。

When I type in the textbox on sheet1, I want the textboxes on sheet2, sheet3, and sheet4 to populate with the same value.

当我在 sheet1 上的文本框中键入时,我希望 sheet2、sheet3 和 sheet4 上的文本框填充相同的值。

回答by Fink

Here is another way:

这是另一种方式:

Create a module, and place this code there. This needs to be configured for your own specific textbox names and sheet names

创建一个模块,并将此代码放在那里。这需要为您自己的特定文本框名称和工作表名称进行配置

Public Sub UpdateTextBoxes(str As String)

    Sheets("sheet1").TextBox1.Text = str
    Sheets("sheet2").TextBox1.Text = str
    Sheets("sheet3").TextBox1.Text = str
End Sub

Then for each textbox object you wish to replicate, use this for its change event (Sheet Class Module) (Change the paramaters to suite your needs)

然后对于您希望复制的每个文本框对象,将其用于其更改事件(工作表类模块)(更改参数以满足您的需求)

Private Sub TextBox1_Change()
    UpdateTextBoxes Me.TextBox1.Text
End Sub

This way you can name your controls however you see fit, and you can update the text from any of the textboxes, and they will always be consistant.

通过这种方式,您可以按照自己认为合适的方式命名控件,并且可以更新任何文本框中的文本,并且它们将始终保持一致。

回答by Dick Kusleika

Here's one way: Set the LinkedCell property of the first textbox to A1. Set the LinkedCell property of every other textbox to A1 of its own sheet. Then in sheets 2-4, cell A1, put

这是一种方法:将第一个文本框的 LinkedCell 属性设置为 A1。将每个其他文本框的 LinkedCell 属性设置为其自己工作表的 A1。然后在第 2-4 页的单元格 A1 中,输入

=Sheet1!A1

Now whatever you type in the textbox on Sheet1 will be in the textboxes on sheets 2-4. If you type anything in the textboxes in 2-4, you'll break the link. But from your question, it looks like you only want to type in the first one.

现在,无论您在 Sheet1 的文本框中键入什么,都将出现在第 2-4 页的文本框中。如果您在 2-4 中的文本框中键入任何内容,则会断开链接。但是从您的问题来看,您似乎只想输入第一个。

Here's another way.

这是另一种方式。

Private Sub TextBox1_Change()

    Dim ws As Worksheet

    For Each ws In Me.Parent.Worksheets
        ws.OLEObjects("TextBox1").Object.Text = Me.TextBox1.Text
    Next ws

End Sub

Put that in the class module for the sheet. It assumes all your textboxes are named TextBox1. Whenever you change the one on Sheet1, the ones on the other sheet get changed.

把它放在工作表的类模块中。它假定您所有的文本框都命名为 TextBox1。每当您更改 Sheet1 上的一个时,另一张纸上的那些就会更改。

回答by JYelton

Do you want the other textboxes to be editable, or just always show the contents of the first? What happens to the other textboxes when a user edits the 2nd, 3rd, and so on (in the first case)? These questions will affect the solution... But if you just want the boxes to mimic the first, then use SheetName!CellAddresslike this:

您希望其他文本框可编辑,还是始终显示第一个文本框的内容?当用户编辑第二个、第三个等等(在第一种情况下)时,其他文本框会发生什么?这些问题会影响解决方案......但如果你只是想让盒子模仿第一个,那么SheetName!CellAddress像这样使用:

=WorksheetName!A1

Just substitute the name of the worksheet and the appropriate cell.

只需替换工作表的名称和相应的单元格即可。

回答by dendarii

Create a named range somewhere in your workbook, then type that name into the textbox linkedcell property.

在工作簿中的某处创建一个命名范围,然后将该名称输入到文本框链接单元属性中。