我有一个用于交货单的工作簿,sheet1是交货单模板,sheet2是客户名称和地址数据。sheet1上有一个ActiveX ComboBox,其中填充了客户名称,因此当我选择一个客户时,将在交货单模板单元格C5:C11中调出他们的名称和地址。我有组合框的代码,这可以很好地工作,但在超过70%的发送中,我需要编辑地址详细信息,因为不同的地区,部门或建筑物等,所以我现在使用的宏是浪费时间,因为我不能编辑数据。我不想结束与300个地址的数据库,当可能60个地址将完成这项工作。因此,我认为解决方案是复制和粘贴数据,然后允许编辑。我已经尝试编辑我的现有代码,使其复制和粘贴,但不断得到各种错误,所以感谢任何帮助或新的想法。
Private Sub ComboBox1_Change()
' CopyNameAddress Macro
Dim strCustomer As String
Dim iRow As String
Dim rngDetails As Range
strCustomer = ComboBox1.List(ComboBox1.ListIndex)
With ThisWorkbook
With .Sheets("Sheet2").Select
iRow = Application.Match(strCustomer, .Range("B3:B" & .Range("B" & .Cells.Rows.Count).End(xlUp).Row), 0) + 2
Set rngDetails = .Range("C" & iRow & ":H" & iRow).Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.Cop
End With
With .Sheets("Sheet1").Select
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=True
End With
End With
End Sub发布于 2015-02-22 19:10:19
With不是可嵌套的(至少更高的不是活动的)。一次只能有一个处于活动状态。
对于如此小的脚本,with没有任何优势。它在循环中和在对象上设置许多属性时都很好用。
错误消息文本为"Expected With“
发布于 2015-02-23 09:32:00
试试这个:
Private Sub ComboBox1_Change()
' CopyNameAddress Macro
Dim strCustomer As String
Dim iRow As String
Dim rngDetails As Range
strCustomer = ComboBox1.List(ComboBox1.ListIndex)
With ThisWorkbook
With .Sheets("Sheet2") '<< no .Select
iRow = Application.Match(strCustomer, _
.Range("B3:B" & .Range("B" & .Cells.Rows.Count).End(xlUp).Row), 0) + 2
Set rngDetails = .Range("C" & iRow & ":H" & iRow) '<< no .Select
'you can copy without selecting...
rngDetails.SpecialCells(xlCellTypeConstants, 23).Copy '<< you had ".Cop" here
End With
With .Sheets("Sheet1") 'no .Select
'missed the leading period on the next line (and there's no need to Select)
.Range("C5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=True, Transpose:=True
End With
End With
End Subhttps://stackoverflow.com/questions/28656784
复制相似问题