[英]excel vba copy data from query to other sheet
我通常不會看這個,因為到目前為止你還沒有展示你嘗試過的代碼。 話雖如此,這是一個有趣的小問題,我將其視為編碼練習。 以下使用字典從D
列獲取唯一值,並使用鍵來創建新工作表並設置過濾器以復制數據。 讓我知道事情的后續。
Option Explicit
Sub test()
Dim d As Object, x, LngRow As Long, i As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("tempoary_csvData")
Set d = CreateObject("scripting.dictionary")
x = Application.Transpose(Range("D5", Cells(Rows.Count, "D").End(xlUp)))
For LngRow = 1 To UBound(x, 1)
d(x(LngRow)) = 1
Next
For Each x In d.Keys
If x <> "" Then
With ThisWorkbook
Set ws2 = .Sheets.Add(After:=.Sheets(.Sheets.Count))
For i = 1 To Worksheets.Count
If Worksheets(i).Name = x Then
MsgBox "The sheet " & x & " already exists - macro stopped"
Exit Sub
End If
Next i
ws2.Name = x
End With
With ws1.Cells(4, 1).CurrentRegion
.AutoFilter 4, x
.Copy Sheets(x).Cells(1, 1)
End With
End If
Next x
ws1.AutoFilter.ShowAllData
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.