今天第一次嘗試使用Excel宏。
要實現的功能是:1個xls文件中,有2個工作表。判斷工作表是否篩選,如果篩選清除篩選。然后將一個工作表中的數據,粘貼到另一個工作表下方。
嘗試了錄制宏,然后個修改其中的代碼。整個過程中,遇到了一些問題,在網上找,並試驗,解決了部分問題。
1)判斷工作表是否處於篩選模式
If Worksheets("Sheet1").FilterMode = True Then
2)向下移動一個單元格
Selection.End(xlDown).Offset(1, 0).Select
沒有找到明確的方法,受到一個答案的啟發,嘗試着加入offset,沒想到成功了。
3)關閉表格並自動選擇是否保存
ActiveWindow.Close savechanges:=False
這是關閉表格,並且不保存內容。
4)清理剪切板
Application.CutCopyMode = xlCut
這個是在網上找到的有效的方法。
我想實現的是,關閉文件,但是保留剪貼板中的內容。可惜的是,想實現的功能,目前實現不了。
Sub MR() ' ' MR 宏 ' 取MR環境中的數據 ' ' 快捷鍵: Ctrl+m ' If Worksheets("Sheet1").FilterMode = True Then Sheets("Sheet1").Select ActiveSheet.ShowAllData End If If Worksheets("Sheet2").FilterMode = True Then Sheets("Sheet2").Select ActiveSheet.ShowAllData End If Sheets("Sheet2").Select Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy Sheets("Sheet1").Select Range("A2").Select Selection.End(xlDown).Offset(1, 0).Select ActiveSheet.Paste Rows("2:2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy Application.CutCopyMode = xlCut ActiveWindow.Close savechanges:=False 'Application.CutCopyMode = True 'Application.DisplayAlerts = False End Sub