iT邦幫忙

0

(已解決)excel vba 檢查 excel 工作表一的 A 欄和工作表二的 A 欄

小斑 2017-09-04 09:25:0019723 瀏覽

大家好
我是 VBA 新手
我想在 excel 工作表一的 A 欄,和工作表二的 A 欄做檢查
在點選進入工作表二時,檢查工作表二的 A 欄內容,有無存在工作表一的 A 欄內容
如果不存在,將工作表二的 A 欄不存在的內容直接 delete

有想過可用 VLookup 函數
但還是不太會寫
請高手給點建議和幫忙
感激不盡
/images/emoticon/emoticon41.gif

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
g921925
iT邦新手 5 級 ‧ 2017-09-05 08:52:20
最佳解答

你如果工作表2原本存在只是空白,把值是貼上去
你可以插一個圖型當按鈕,指定巨集給按鈕,當按下按鈕在執行
如果原本工作表2不存在就把按鈕放在工作表1

VBA的寫法我是利用迴圈

Sub 檢查並刪除()
Dim Eng as Range
Dim i as Integer, r as Integer
Application.ScreenUpdating = False

'取工作表2的值
    sheets(2).Select
    For i = 1 to Sheets(2).Range("A1048576").End(xlup).Row
        '有標題就改 i = 2  
        Eng = Cells(i, "A")
 
        '跟工作表1比對
        sheets(1).Select
        For r = 1 to Sheets(1).Range("A1048576").End(xlup).Row
            '有標題就改 r = 2
            
            '如果有就跳下一個
            If cells(r, "A")=Eng then
                goto 25
            End If
        next r
        
        '沒有就刪掉
        sheets(2).Select
        Cells(i, "A") = ""
25:
    next i
Application.ScreenUpdating = True
End Sub

不過我的應該比較吃資源,自學VBA僅供參考

※因為不清楚版主的資料是什麼型態?
不知道是否會完全相同?
所以不敢用Vlookup來寫函數檢查
因為有可能發生判別錯誤 例如 '1 <> 1

小斑 iT邦新手 3 級 ‧ 2017-09-05 09:04:52 檢舉

好的,我試試看,謝謝/images/emoticon/emoticon41.gif

g921925 iT邦新手 5 級 ‧ 2017-09-05 09:17:00 檢舉

有問題可以跟我說我再修正

小斑 iT邦新手 3 級 ‧ 2017-09-07 11:02:18 檢舉

謝謝,解決問題了/images/emoticon/emoticon41.gif

0
做工仔人!
iT邦大師 1 級 ‧ 2017-09-04 11:10:50

在vba中不是用vlookup 函數.
要用 Selection.Find
範例:
Set iti = Selection.Find(What:=ItemCode, After:=ActiveCell, LookIn:=xlFormulas,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, MatchByte:=False, SearchFormat:=False)

ItemCode : 搜尋值

小斑 iT邦新手 3 級 ‧ 2017-09-04 12:02:52 檢舉

好的,我試試看,謝謝

0
cws0827
iT邦新手 5 級 ‧ 2017-09-05 08:58:11

Vlookup很好用,我當初也是爬文自己摸了一天才學會(資質愚鈍),後來知道訣竅時候只花了10分鐘就教會同事

我要發表回答

立即登入回答