我有2个工作表-“ Venn Diagram Helper”(由一系列命名的数据范围组成)-“ Venn Diagram”(维恩图),在Venn Diagram图像上带有多个文本框。
我在C5单元格的维恩图工作表上有一个下拉列表,希望用于触发将特定范围复制和粘贴到特定文本框中。
我希望用户从下拉列表中进行选择-选项为“第1阶段结束”,“第2阶段结束”或“年末”,以及相应命名范围内的个人名称复制并粘贴到带有';'的文本框中分隔每个名称。
我已经开始编写代码,但是,由于我是VBA的新手,并且我一直在尝试改编我在网上找到的示例,所以它不起作用。你们中的任何人都可以抽出时间来帮助我阅读“阅读”文本框吗?我相信,一旦有了模型代码,我将能够对其进行调整以填充其余的文本框。
这是我到目前为止所拥有的:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("C5")) Is Nothing Then
Dim Helper As Worksheet
Dim Venn As Worksheet
Dim EoP1Rdg As Range
Dim EoP2Rdg As Range
Dim EoYRdg As Range
Dim Rdg_TxtBox As OLEObject
Set Helper = ThisWorkbook.Worksheets("RWM Venn Diagram Helper")
Set Venn = ThisWorkbook.Worksheets("RWM Venn Diagram")
Set Rdg_TxtBox = Venn.OLEObjects("Rdg_TxtBox")
With Helper
Set EoP1Rdg = .Range("AQ4:AQ103")
Set EoP2Rdg = .Range("AX4:AX103")
Set EoYRdg = .Range("BE4:BE103")
End With
If Intersect(Target, Range("C5")) = "End of Phase 1" Then
With Helper
.Select ("EoP1Rdg")
.Copy
End With
With Venn
.Select Rdg_TxtBox
.Paste
End With
End If
End If
End Sub
提前谢谢!
编辑:25/9 5pm
我已将数据验证的下拉列表更改为组合框,如下所示。我已经重写了代码,但仍然无法正常工作(没有文本复制到文本框中)。有人可以进一步建议我吗?
Sub DropDown5_Change()
Dim Helper As Worksheet
Dim Venn As Worksheet
Dim EoP1Rdg As Range
Set Helper = Sheets("RWM Venn Diagram Helper")
Set Venn = Sheets("RWM Venn Diagram")
Set EoP1Rdg = Range("EoP1Rdg")
If DropDown5 = "End of Phase 1" Then
Sheets("RWM Venn Diagram Helper").Select
Range("AQ4:AQ103").Select
Selection.Copy
Sheets("RWM Venn Diagram").Select
ActiveSheet.Shapes.Range(Array("TextBox 7")).Select
Application.CutCopyMode = False
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
"Name 15" & Chr(13) & "Name 16"
End If
End Sub
非常感谢
答案 0 :(得分:0)
我猜您正在使用“数据”标签->“数据验证”中的单元内下拉列表。您选择了一个工作表事件,该事件将在您每次单击一个单元格或选择一组单元格(一个范围)时触发。在“数据验证”下拉列表中选择一个值时,该事件不会触发。
您要转到“开发人员”选项卡,然后单击“插入”按钮,然后插入一个组合框。参见下图:
ComboBox是“表单控件”部分下第一行中的第二个按钮。您将要避免使用Active X控件,直到您精通VBA。
您应该查看Microsoft的Overview of forms, Form controls, and ActiveX controls on a worksheet。
一旦拖放组合框并调整其大小,就可以单击“插入”右侧按钮栏上的“查看代码”。这将打开VBE代码编辑器,这是您要编写用于处理change事件的代码的地方。
我没有您的工作簿,因此无法为您显示可能需要更改的所有内容。
在下面的代码中,我试图显示“录制宏”与在VBA中编程之间的区别。最大的变化是在End If
之前的语句,您在此处将文本字符串分配给Shape。
If DropDown5 = "End of Phase 1" Then
'This piece of code shows you clicking on the RWM Venn Diagram Helper sheet
'Which if you're writing the code, you need to do the following
'Declare and set an object variable to the Workbook
Dim wb As Workbook
Set wb = ThisWorkbook
'Declare and set an object variable to the Helper worksheet
'in this case source (srcWS)
Dim srcWS As Worksheet
Set srcWS = wb.Sheets("RWM Venn Diagram Helper")
'Declare and set an object variable to the Digram worksheet
'in this case target (tarWS)
Dim tarWS As Worksheet
Set tarWS = wb.Sheets("RWM Venn Diagram")
'Sheets("RWM Venn Diagram Helper").Select
srcWS.Activate
Range("AQ4:AQ103").Select
Selection.Copy
'Sheets("RWM Venn Diagram").Select
tarWS.Activate
'ActiveSheet.Shapes.Range(Array("TextBox 7")).Select
tarWS.Shapes.Range(Array("TextBox 7")).Select
'This statement is used by the macro generator to clear the
'clip board when you complete the paste operation
'Application.CutCopyMode = False
'I haven't seen any paste function at this point.
'Change this to the statement that follows
'Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
"Name 15" & Chr(13) & "Name 16"
'This assumes that the Venn Diagram is on target sheet
'That it is shape 1 - if there's only one shape, probably
tarWS.Shapes(1).TextFrame2.TextRange.Text = "Name 15" & Chr(13) & "Name 16"
End If