1

根据在下拉菜单中所做的选择,尝试编写代码以使用下拉菜单填充与单元格相邻的单元格。

我需要这个仅适用于活动工作表,而不一定适用于整个工作簿。

在 VBA 中编写代码相对较新,可能是一个新手错误,但仍然需要一些帮助!

获取运行时错误对象“范围”的方法“值”失败。

这是我拥有的代码,它是整个工作簿中唯一的代码......

Private Sub Worksheet_Change(ByVal Target As Range)


Select Case Range("F18")

Case "Made Margin/Made Sales $"
    Worksheets("Store #").Range("G18").value = 
    "Made margin by $XXX GIG at XX.XX%. Shrink  at XX.XX% or $XXX. QTD Margin at 
    XX.XX%"

Case "Made Margin/Missed Sales $"
    Worksheets("Store #").Range("G18").value = 
    "Made margin by $XXX GIG at XX.XX%. Shrink at XX.XX% or $XXX. QTD Margin at XX.XX%   
    Missed sales by $XXX. Begin explaining Sales $ miss here"

Case "Missed Margin/Made Sales$"
    Worksheets("Store #").Range("G18").value = "Missed margin by $XXX GIG at XX.XX%.   
    Shrink at XX.XX% or $XXX. QTD Margin at XX.XX% Made sales by $XXX. Begin explaining   
    Margin $ miss here"

Case "Missed Margin/Missed Sales"
    Worksheets("Store #").Range("G18").value = "Missed margin by $XXX GIG at XX.XX%.  
    Shrink at XX.XX% or $XXX. QTD Margin at XX.XX% Begin explaining Margin $ miss here,  
    followed by explaining Sales $ miss"

End Select

End Sub

任何帮助将不胜感激!

4

1 回答 1

0

您需要添加一个仅在 F18 更改时触发事件的条件。那,并将字符串放入一行或使用“_”键表示多行代码。我还要确保每个案例值只有一组引号,否则会弄乱代码。

我试过了,代码效果很好。

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("F18")) Is Nothing Then

Select Case Range("F18")

Case "Made Margin/Made Sales $"
    Worksheets("Store #").Range("G18").Value = "Made margin by $XXX GIG at XX.XX%.     Shrink  at XX.XX% or $XXX. QTD Margin at"

 Case "Made Margin/Missed Sales $"
    Worksheets("Store #").Range("G18").Value = "Made margin by $XXX GIG at XX.XX%. Shrink at XX.XX% or $XXX. QTD Margin at XX.XX%. Missed sales by $XXX. Begin explaining Sales $ miss here"

Case "Missed Margin/Made Sales$"
    Worksheets("Store #").Range("G18").Value = "Missed margin by $XXX GIG at XX.XX%. Shrink at XX.XX% or $XXX. QTD Margin at XX.XX% Made sales by $XXX. Begin explaining Margin $ miss here"

Case "Missed Margin/Missed Sales"
    Worksheets("Store #").Range("G18").Value = "Missed margin by $XXX GIG at XX.XX%. Shrink at XX.XX% or $XXX. QTD Margin at XX.XX% Begin explaining Margin $ miss here, followed by explaining Sales $ miss"

End Select

End If

End Sub
于 2013-03-08T02:21:30.947 回答