EXCEL的VBA公式,求修改下
PrivateSubWorksheet_SelectionChange(ByValTargetAsRange)DimrIfTarget.Column<>17OrTarge...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r
If Target.Column <> 17 Or Target.Count > 1 Then Exit Sub
If Target.Offset(0, 5) <= 0 And Target.Offset(0, 0) = "千足" And Target.Offset(0, -10) <> "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("首饰").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("首饰").Rows(r)
ElseIf Target.Offset(0, 5) <= 0 And Target.Offset(0, 0) = "足" And Target.Offset(0, -10) <> "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("首饰").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("首饰").Rows(r)
ElseIf Target.Offset(0, 5) <= 0 And Target.Offset(0, 0) = "3D" And Target.Offset(0, -10) <> "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("黄金首饰").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("首饰").Rows(r)
ElseIf Target.Offset(0, 5) <= 0 And Target.Offset(0, -10) = "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("配件").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("配件").Rows(r)
End If
End Sub
加多一个条件,就是写入“珠宝行.xlsm”的时候,也写入“珠宝行.xlsx”这两个EXCEL中去,第二个是用来打印机打印的,打印机识别不了第一个,还有就是能不能在这两个EXCEL不打开的情况下写入,我关闭的时候触发事件,会出现错误,我记得是可以的 展开
Dim r
If Target.Column <> 17 Or Target.Count > 1 Then Exit Sub
If Target.Offset(0, 5) <= 0 And Target.Offset(0, 0) = "千足" And Target.Offset(0, -10) <> "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("首饰").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("首饰").Rows(r)
ElseIf Target.Offset(0, 5) <= 0 And Target.Offset(0, 0) = "足" And Target.Offset(0, -10) <> "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("首饰").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("首饰").Rows(r)
ElseIf Target.Offset(0, 5) <= 0 And Target.Offset(0, 0) = "3D" And Target.Offset(0, -10) <> "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("黄金首饰").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("首饰").Rows(r)
ElseIf Target.Offset(0, 5) <= 0 And Target.Offset(0, -10) = "配件" Then
Target.Offset(0, 5) = Target.Offset(0, 5) + 1
r = Workbooks("珠宝行.xlsm").Sheets("配件").UsedRange.Rows.Count + 1
Target.EntireRow.Copy Workbooks("珠宝行.xlsm").Sheets("配件").Rows(r)
End If
End Sub
加多一个条件,就是写入“珠宝行.xlsm”的时候,也写入“珠宝行.xlsx”这两个EXCEL中去,第二个是用来打印机打印的,打印机识别不了第一个,还有就是能不能在这两个EXCEL不打开的情况下写入,我关闭的时候触发事件,会出现错误,我记得是可以的 展开
2个回答
展开全部
两个文件没有打开是肯定写不进去的,可以不让报错,但是那样就无法插入,你需要如何处理呢?
我建议在前面添加代码,文件不存在的时候就立即打开,代码添加在下面一行之后:
If Target.Column <> 17 Or Target.Count > 1 Then Exit Sub
添加的代码:
Dim wb
On Error Resume Next
Set wb = Workbooks("珠宝行.xlsm")
If wb Is Nothing Then
MsgBox "请打开【珠宝行.xlsm】文件!"
If Not Application.FindFile Then Exit Sub
End If
On Error GoTo 0
更多追问追答
追问
能让它在后台打开吗?就是不让我看见或者操作的前提下,这样的话我就不用专门去打开,不知道行不行;还有能同时写入两个不同的EXCEL吗?再定义一个r能实现吗?
追答
EXCEL新打开的文件肯定会在最前面。同时插入不可能,语句必须一句一句执行,可以先后插入多个表。
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询