iT邦幫忙

1

VBA 重複查找 詢問

如題
https://ithelp.ithome.com.tw/upload/images/20180503/201030795dU50W1HvY.png
有幾個問題想請教各位大大

Windows("Source.xls").Activate
ActiveSheet.Range("I:I").Find("1").EntireRow.Select
Selection.Copy
Windows("Reprot.xlsx").Activate
I = ActiveCell.SpecialCells(xlLastCell).Row + 1
Cells(I, 1).PasteSpecial xlPasteAll
  1. 我在執行之後為什麼是複製到A4那一列而並非A1
    https://ithelp.ithome.com.tw/upload/images/20180503/20103079G8reQeySmJ.png
  2. 要如何才能把剩餘I行內有含"1"的字串整列複製並貼上到Report裡面呢?(不重複)
    2-1.要是沒有I行內都沒有"1"的話,就略過錯誤訊息繼續執行後面指令?
    (我有嘗試把I行的"1"都拿掉結果執行就跳錯誤了)/images/emoticon/emoticon04.gif
  3. 若是要反向搜尋的話(I排內非"1"的都要)

因程式方面比較生疏,故懇請大大們賜教/images/emoticon/emoticon16.gif/images/emoticon/emoticon16.gif

看更多先前的討論...收起先前的討論...
q00153 iT邦新手 3 級 ‧ 2018-05-03 18:16:55 檢舉
我有一個想法,
step1. 把所有資料都複製到 Reprot.xlsx 的 sheet
step2. 把 I 欄不符合保留條件的資料那行刪除 (使用迴圈)
step3. 剩下的就是你需要的

這樣的優點有
1. 速度比一筆一筆搜尋、複製、貼上還快
2.可以自訂要保留的條件 array 方便變更條件
3.如果要更快可以把資料依照 I 欄排序後,再判斷刪除資料
1. 到 `檢視` ,開啟 `區域變數視窗` 。
2. 用 `F8` 逐條執行你的程式。
3. 觀察 `區域變數視窗` 中,各個變數的值是否符合你的預期。
蟹老闆 iT邦大師 1 級 ‧ 2018-05-04 15:06:11 檢舉
用篩選後複製會不會比較好?
q00153 iT邦新手 3 級 ‧ 2018-05-04 15:55:55 檢舉
XD~對ㄝ
怎麼就忘記篩選了
我這腦袋.......
意思是我在I欄內先針對我要的資料篩選後在複製貼上嗎?
那另外請問若是篩選後,每個儲存列之間的空隙是也會照著貼上嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2018-05-04 11:52:58
最佳解答

該認真一點
學個迴圈寫法了...
(程式沒有測過,請先使用測試資料測試)

Sub main()
   Windows("Reprot.xlsx").Activate
   Range("A1").Select
   Windows("Source.xls").Activate
   Range("A1").Select

   For Each r In Range("I1:I15")
      If ActiveCell.Value = "1" Then
         Range(ActiveCell.Offset(0, -8), ActiveCell.Offset(0, -4)).Select
         Selection.Copy
         Windows("Reprot.xlsx").Activate
         ActiveSheet.Paste
         ActiveCell.Offset(1, 0).Select
         Windows("Source.xls").Activate
      End If
      ActiveCell.Offset(1, 0).Select
   Next
End Sub 
看更多先前的回應...收起先前的回應...

海綿大抱歉那麼晚回~
我看得懂大致上的意思是從I1~I15之前尋找"1"的值
並且複製該儲存格左邊隔壁4~8格並在"Report"內貼上
然後向下一格重複尋找

測試的結果是甚麼都沒有貼過去,我有用逐步執行去看結果如下
https://ithelp.ithome.com.tw/upload/images/20180514/20103079JgOQZqlreE.png
ActiveCell是回覆是"A"欄而並非是I欄,故沒有執行下面的複製貼上
但我不確定是哪個部分可能需要修改
(因為我覺得For Each r In Range("I1:I15")這段就是指I1~I15列的尋找)
再懇請大大協助/images/emoticon/emoticon41.gif

沒想到你還會回來看
我只好測了一下並修正錯誤
/images/emoticon/emoticon25.gif

Sub main()
   Windows("Report.xlsx").Activate
   Range("A1").Select
   Windows("Source.xlsx").Activate
   Range("A1").Select

   For Each r In Range("I1:I15")
      If r.Value = "1" Then
         Range(r.Offset(0, -8), r.Offset(0, -4)).Select
         Selection.Copy
         Windows("Report.xlsx").Activate
         ActiveSheet.Paste
         ActiveCell.Offset(1, 0).Select
         Windows("Source.xlsx").Activate
      End If
   Next
End Sub

可以請問一下 "r"指的是甚麼?
變數的宣告嗎?

For Each r In Range("I1:I15")
r 就代表「從I1,I2,...一直到 I15」
第一次 For r 就是 I1
第二次 For r 就是 I2
以此類推

感謝海綿大相助及解釋!
受益良多!
/images/emoticon/emoticon24.gif/images/emoticon/emoticon12.gif/images/emoticon/emoticon41.gif

我要發表回答

立即登入回答