这是我第一次发帖,但是我在很短的时间和新手职业生涯中使用过这个网站,在我工作中使用的excel文件中写了一些非常基本的宏。它非常方便,所以我非常感谢!
我目前的问题...... 我一直在使用“按钮”宏将数据从一个页面填充到另一个页面。这对我来说非常容易,对于我们这些自我教学的人(通过谷歌),我觉得这是一种“安全”的方法。
这一次,我想基本上做同样的事情,但有一个下拉列表,而不是按钮。但是,我不确定如何根据从下拉列表中选择的内容将宏编写为条件。
下拉列表有三个选项。 每个选择都将从工作簿内的不同工作表中复制数据。
例如(我在时装界工作)
我有三个尺寸范围可供选择,从下拉列表中选择。 6-18 XXS-XXL XS / S-L / XL
基于选择其中一个大小范围,它们都对应于工作簿中的工作表,我将从中提取信息。按照上面的顺序,它们对应于“G1”,“G2”和G3。根据下拉选择,例如6-18,将数据从工作表“G1”复制到另一个名为“参数”的工作表。我还需要能够定义单元格范围,因为我只是将表格G1中的一些数据复制到参数页面。
请查看我到目前为止的内容......
Sub DropDown1_Change()
'DropDown Select. 6-18
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'sbClearCells()
Range("C10:P67").Clear
'Copy the data
Sheets("G1").Range("C10:P67").Copy
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
'DropDown Select. XS/S-L/XL
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'sbClearCells()
Range("C10:P67").Clear
'Copy the data
Sheets("G2").Range("C10:P67").Copy
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
'DropDown Select. XXS-XXL
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'sbClearCells()
Range("C10:P67").Clear
'Copy the data
Sheets("G3").Range("C10:P67").Copy
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
目前它正在运行代码,但它只运行脚本的最后一段。我需要一些帮助才能让它识别出不同的选择。
非常感谢任何帮助。
非常感谢, 兆
答案 0 :(得分:0)
您使用的是什么形式的下拉列表?您遇到的问题是它贯穿整个代码,您只看到粘贴在该范围内的最终内容。
对于excel中的下拉列表,我通常默认使用列表控件对其进行数据验证的单元格。这将告诉您应该运行哪部分代码。这可以使用if语句来实现,例如,如果验证单元格在“A1”中,您的代码将被修改为此。
Sub CopyRanges()
Dim selectedValue
'Change the range to where you end up putting the dropdown
selectedValue = Sheets("Parameter").Range("A1").value
If selectedValue = "6-18" Then
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'sbClearCells()
Range("C10:P67").Clear
'Copy the data
Sheets("G1").Range("C10:P67").Copy
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End If
If selectedValue = "XS/S-L/XL" Then
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'sbClearCells()
Range("C10:P67").Clear
'Copy the data
Sheets("G2").Range("C10:P67").Copy
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End If
If selectedValue = "XS/S-L/XL" Then
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'sbClearCells()
Range("C10:P67").Clear
'Copy the data
Sheets("G3").Range("C10:P67").Copy
'Activate the destination worksheet
Sheets("PARAMETER").Activate
'Select the target range
Range("C10:P67").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
现在,您仍然需要按一个按钮来调用该功能。如果您希望每次更改单元格时都运行它,我会查看此post。
注意:使用组合框可以实现类似的效果。
答案 1 :(得分:0)
编辑。
此代码使用Select Case,因为我不知道您的ComboBox在哪个单元格中,我使用的是B2。就像我在每次选择尺寸时在评论中说的那样,您的数据将被覆盖在Sheets("PARAMETER"). Range("C10:P67")
中。我还有你的额外代码片段。
Private Sub ComboBox1_Click()
Select Case ComboBox1.Value
Select Case Range("C4")
Case "6-18": Size_6_18 'selecting will call the macro Size_6_18 etc. etc. for the other 2 cases
Case "XS/S-L/XL": Size_XS_XL
Case "XXS-XXL": Size_XXS_XXL
End Select
End If
End Sub
Sub Size_6_18()
Sheets("PARAMETER").Range("C10:P67").Clear
Sheets("PARAMETER").Range("C10:P67").Value(11) = Sheets("G1").Range("C10:P67").Value(11) '(11) copies formats etc.
End Sub
Sub Size_XS_XL()
Sheets("PARAMETER").Range("C10:P67").Clear
Sheets("PARAMETER").Range("C10:P67").Value(11) = Sheets("G2").Range("C10:P67").Value(11)
End Sub
Sub Size_XXS_XXL()
Sheets("PARAMETER").Range("C10:P67").Clear
Sheets("PARAMETER").Range("C10:P67").Value(11) = Sheets("G3").Range("C10:P67").Value(11)
End Sub