Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2836|回复: 2

[已解决]清空结果后代码运行错误

[复制链接]
发表于 2014-11-2 23:15 | 显示全部楼层 |阅读模式
本帖最后由 marshall_yang 于 2014-11-3 20:42 编辑

各位高手:
附件里我能正常运行代码,可是当我把工作表里“当天出货明细”的内容清空后,并且鼠标停留在该该工作表中(“当天出货明细”)再次运行该代码,就会报错,不知是什么原因,请大家帮忙看看,谢谢

Sub 当天需出货明细()
Dim i As Integer
Dim rg As Range
j = Sheet1.UsedRange.Rows.Count
For i = 2 To j
If Cells(i, "G") = "2014-6-23" Then
If rg Is Nothing Then
  Set rg = Cells(i, "G")
Else
Set rg = Union(rg, Cells(i, "G"))
End If
End If
Next i
rg.EntireRow.Copy Sheet2.[a2]
Sheet1.Range("1:1").Copy Destination:=Sheet2.[a1]
End Sub


最佳答案
2014-11-3 08:18
  1. Sub 当天需出货明细()
  2.     Dim i As Integer
  3.     Dim rg As Range
  4.     With Sheet1
  5.         j = .UsedRange.Rows.Count
  6.         For i = 2 To j
  7.             If .Cells(i, "G") = "2014-6-23" Then
  8.                 If rg Is Nothing Then
  9.                     Set rg = .Cells(i, "G")
  10.                 Else
  11.                     Set rg = Union(rg, .Cells(i, "G"))
  12.                 End If
  13.             End If
  14.         Next i
  15.         rg.EntireRow.Copy Sheet2.[a2]
  16.     End With
  17.     Sheet1.Range("1:1").Copy Destination:=Sheet2.[a1]
  18. End Sub
复制代码

问题.rar

100.85 KB, 下载次数: 7

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-11-3 08:18 | 显示全部楼层    本楼为最佳答案   
  1. Sub 当天需出货明细()
  2.     Dim i As Integer
  3.     Dim rg As Range
  4.     With Sheet1
  5.         j = .UsedRange.Rows.Count
  6.         For i = 2 To j
  7.             If .Cells(i, "G") = "2014-6-23" Then
  8.                 If rg Is Nothing Then
  9.                     Set rg = .Cells(i, "G")
  10.                 Else
  11.                     Set rg = Union(rg, .Cells(i, "G"))
  12.                 End If
  13.             End If
  14.         Next i
  15.         rg.EntireRow.Copy Sheet2.[a2]
  16.     End With
  17.     Sheet1.Range("1:1").Copy Destination:=Sheet2.[a1]
  18. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-11-3 20:39 | 显示全部楼层
zjdh 发表于 2014-11-3 08:18

明白了,如果不指定工作表,程序会默认当前工作表就是程序所需要运行的工作表,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-3-28 22:11 , Processed in 0.420301 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表