我有100K的Excel文件,其中有許多員工信息,我想將所有存在的數據移至此員工的第一行,下面的圖片會比我的文字更響亮,可以使用VBA代碼做這個?或有Excel中的一個把戲,我不知道的將所有行移至第一個行
1
A
回答
2
試試下面的代碼。
Sub Demo()
Dim ws As Worksheet
Dim cel As Range, rng As Range
Dim lastRow As Long, lastCol As Long, i As Long
Dim fOccur As Long, lOccur As Long, colIndex As Long
Dim dict As Object, c1
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Sheets("Sheet1") 'change Sheet1 to your data range
Set dict = CreateObject("Scripting.Dictionary")
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row with data in Column A
lastCol = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column 'last column with data in Sheet1
Set rng = .Range("A1:A" & lastRow) 'set range in Column A
c1 = .Range("A2:A" & lastRow)
For i = 1 To UBound(c1, 1) 'using dictionary to get uniques values from Column A
dict(c1(i, 1)) = 1
Next i
colIndex = 16 'colIndex+1 is column number where data will be displayed from
For Each k In dict.keys 'loopthrough all unique values in Column A
fOccur = Application.WorksheetFunction.Match(k, rng, 0) 'get row no. of first occurrence
lOccur = Application.WorksheetFunction.CountIf(rng, k) 'get row no. of last occurrence
lOccur = lOccur + fOccur - 1
'copy range from left to right
.Range(.Cells(fOccur, 1 + colIndex), .Cells(lOccur, lastCol + colIndex)).Value = .Range(.Cells(fOccur, 1), .Cells(lOccur, lastCol)).Value
'delete blanks in range at right
.Range(.Cells(fOccur, 1 + colIndex), .Cells(lOccur, lastCol + colIndex)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp 'delte blank rows
Next k
End With
Application.ScreenUpdating = True
End Sub
0
嘗試以下。您可以修改以下代碼以匹配您要移動範圍的位置:
Dim oW As Worksheet: Set oW = ThisWorkbook.Worksheets("Sheet8")
With oW.UsedRange
.Cut .Offset(0, .Columns.Count + 2)
End With
相關問題
- 1. DELPHI將第二行移至第一行備註
- 2. 一組中選擇的所有行至第一個0
- 3. 如何將行鼠標移至行中的所有單元格
- 4. 將焦點移至DataGrid中新行的第一列(WPF)
- 5. SED將文字移至下一行
- 6. SplFileObject將指針移至上一行
- 7. 移至第4列的新行
- 8. 移行至左
- 9. 如何將第一行文件重複到所有第一列?
- 10. 將行的一部分移動到第二行,將行的一部分移動到第三行
- 11. 選擇至少有一個功能列表的所有行
- 12. SQL - 返回所有行其中至少一個具有值「Y」
- 13. 將行的最後一個字符移至特定列 - sed? AWK?
- 14. 將MySQL表的所有行轉移到另一個表
- 15. 對錶中所有第一行的鼠標移動效果
- 16. SQL第二張表中所有行旁邊的一個表中的所有行
- 17. 加入表中第一個匹配的行中的所有行
- 18. Sed將文本移至行首?
- 19. Jquery Isotope Library將選擇移至第一個位置?
- 20. 如何將特定節點移至第一個索引?
- 21. 字符串將第一個單詞移至字符串末尾
- 22. 如何將光標移至引號內的第一個單詞?
- 23. 返回值每個組至少出現一次的所有行
- 24. 如何將一行數據框移到第一行?
- 25. 將所有內容刪除至第3個「」。「在Excel中
- 26. TextMate問題:如何將脫字符號移至下一個/上一個空行
- 27. 將所有excel行復制到一行
- 28. 獲取從第一個和最後一個行開始的所有行
- 29. 在HREF中斷以移至下一行
- 30. UPDATE第二排以後的所有行,加入1至這些行
是的,VBA代碼可以做到這一點。我不認爲你可以用Power Query來做到這一點。並且手動方法對您的大型數據庫來說過於繁瑣 –