返回列表 上一主題 發帖

同樣巨集另一檔案卻執行錯誤

同樣巨集另一檔案卻執行錯誤

本帖最後由 ycchenzd 於 2010-8-5 22:02 編輯

小弟之前在舊論壇發問的 一個問題
http://gb.twbts.com/index.php?topic=12801.0

sheet1內容不固定,但會依此排序
sheet2內容固定,比對sheet1後,將符合的列複製於sheet3
sheet3為結果

使用下列程式碼
Sub nn()
Dim Rng As Range, A As Range, Cell As Range
With Sheet2
Set Rng = .Range(.[A1], .[A65536].End(xlUp))
End With
With Sheet1
For Each A In .Range(.[D1], .[D65536].End(xlUp))
  If Not Rng.Find(A, lookat:=xlWhole) Is Nothing Then
    If Cell Is Nothing Then Set Cell = A Else Set Cell = Union(Cell, A)
  End If
Next
End With
Sheet3.Cells = ""
Cell.EntireRow.Copy Sheet3.[A1]   
End Sub

但是在原本檔案執行ok,
要在另一個以資料庫抓取後的資料檔執行時卻會在Cell.EntireRow.Copy Sheet3.[A1] 出現錯誤
類似Class copy 的方法錯誤,
但是執行後的資料是正確的
不知問題出在哪裡,資料內容是一樣的啊
懇請解答

會是跟欄位格式有關嗎?

已補上檔案

book1.zip (52.51 KB)

回復 1# ycchenzd
沒有錯誤發生啊?
請附檔上來看看

TOP

已補上檔案
出現的錯誤是Class Range 的Copy方法失敗
懇請解答..謝謝..

TOP

以你上傳的檔案測試並未產生錯誤
加個判斷式試試看
If Not Cell Is Nothing Then Cell.EntireRow.Copy Sheet8.[A1]
可能你Rng.Find(A, lookat:=xlWhole) 一直是找不到吻合的儲存格
學海無涯_不恥下問

TOP

試試這個:
  1. Option Explicit
  2. Sub nn()
  3. Dim Rng As Range, A As Range, cell As Range
  4. With Sheet6
  5. Set Rng = .Range(.[A1], .[A65536].End(xlUp))
  6. End With
  7. With Sheet7
  8. For Each A In .Range(.[D1], .[D65536].End(xlUp))
  9.     If Not Rng.Find(A, lookat:=xlWhole) Is Nothing Then
  10.        If cell Is Nothing Then Set cell = A Else Set cell = Union(cell, A)
  11.     End If
  12. Next
  13. End With

  14. Sheet8.Cells.Clear
  15. cell.EntireRow.Copy
  16. Sheet8.Range("A1").PasteSpecial Paste:=xlPasteValues

  17. End Sub
複製代碼
給其他大大參考:
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

以你上傳的檔案測試並未產生錯誤
加個判斷式試試看
If Not Cell Is Nothing Then Cell.EntireRow.Copy Sh ...
Hsieh 發表於 2010-8-5 23:04


加了If Not Cell Is Nothing Then Cell.EntireRow.Copy Sheet8.[A1]
還是一樣錯誤耶

在您那執行正確嗎?

TOP

本帖最後由 kimbal 於 2010-8-6 01:10 編輯

回復 6# ycchenzd

版本問題,07上運是沒問題的, 03的會出問題

請試試把
  1. Cell.EntireRow.Copy Sheet3.[A1] 
複製代碼
換成
  1. cell.EntireRow.Copy
  2. Sheet3.Range("A1").PasteSpecial Paste:=xlPasteValues
複製代碼
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

以你上傳的檔案測試並未產生錯誤
加個判斷式試試看If Not Cell Is Nothing Then Cell.EntireRow.Copy Sh ... 可能你Rng.Find(A, lookat:=xlWhole) 一直是找不到吻合的儲存格
Hsieh 發表於 2010-8-5 23:04

試試看修改成 Rng.Find(A, LookIn:=xlValues, lookat:=xlWhole)

TOP

回復  ycchenzd

版本問題,07上運是沒問題的, 03的會出問題

請試試把換成
kimbal 發表於 2010-8-6 01:07



測試OK,無錯誤了
但是同樣電腦,另一個檔案以原程式也是OK,差別只在資料來源不一樣
不知何因?

不過還是OK了
謝謝了

TOP

回復 8# GBKEE


改天再來試試這個
檔案在公司電腦
謝謝了

TOP

        靜思自在 : 盡多少本份,就得多少本事。
返回列表 上一主題