優(yōu)化Excel數(shù)據(jù)整理:實(shí)現(xiàn)分散數(shù)據(jù)單元格列向左移合并
修法布施得聰明智慧,多分享讓生活更美好。上回分享了《Excel怎樣快速根據(jù)重復(fù)字段合并其它列數(shù)據(jù)》,介紹了行相同數(shù)據(jù)其不同數(shù)據(jù)合并至一行中處理方法。今天我們將探討如何利用Excel將分散的數(shù)據(jù)單元格列
修法布施得聰明智慧,多分享讓生活更美好。上回分享了《Excel怎樣快速根據(jù)重復(fù)字段合并其它列數(shù)據(jù)》,介紹了行相同數(shù)據(jù)其不同數(shù)據(jù)合并至一行中處理方法。今天我們將探討如何利用Excel將分散的數(shù)據(jù)單元格列條向左移到一起,以提高數(shù)據(jù)整理效率。
步驟1:準(zhǔn)備工作
首先打開(kāi)Microsoft Office Excel,新建文檔并保存文件名為《Excel如何把分散的數(shù)據(jù)單元格列條向左移到一起.xlsm》。接著按下快捷鍵ALT F11打開(kāi)VBE(宏)編輯界面,在菜單欄【插入】下拉列表中選擇【模塊(M)】。
步驟2:插入宏代碼
在新插入的模塊1中,復(fù)制粘貼以下代碼:
```vba
Option Base 1
Sub 數(shù)據(jù)向左移動(dòng)()
Dim myr As Range, i As Long, j As Long, r As Long, c As Long
Dim k As Long, n As Long, x As Long, mb, m
Set myr Selection
m
r
c
k 0: n 0: x 0
ReDim mb(r, 2)
For i 2 To c
For j 1 To r
If j 1 Then
If myr.Cells(j, i).Value <> "" Then
x x 1
mb(x, 1) j
k 1
Else
n 1
End If
Else
If myr.Cells(j, i).Value <> "" Then
k k 1
If k 1 Then
x x 1
mb(x, 1) j
k 1
n 0
Else
n 1
End If
Else
n n 1
End If
End If
If j r And myr.Cells(j, i).Value <> "" Then
mb(x, 2) j
End If
Next j
For j 1 To x
If mb(j, 1) mb(j, 2) Then
If myr.Cells(mb(j, 1), i - 1).Value "" Then
myr.Cells(mb(j, 1), i).Cut Destination:myr.Cells(mb(j, 1), i - 1)
Else
n 0
End If
Else
For k mb(j, 1) To mb(j, 2)
If myr.Cells(k, i - 1).Value <> "" Then
n 1
Exit For
End If
Next k
If n 0 Then
For k mb(j, 1) To mb(j, 2)
myr.Cells(k, i).Cut Destination:myr.Cells(k, i - 1)
Next k
End If
End If
Next j
Next i
Set myr Range(m)
k 0: n 0: x 0
Erase mb
ReDim mb(r, 2)
End Sub
```
步驟3:應(yīng)用宏代碼
回到工作表窗口,選中原數(shù)據(jù)表,運(yùn)行【數(shù)據(jù)向左移動(dòng)】宏。在菜單欄中選擇【視圖】,再?gòu)南吕斜碇羞x擇【宏】,打開(kāi)宏對(duì)話框,選擇該宏名稱,執(zhí)行。這將使右邊的數(shù)據(jù)單元格向左移動(dòng),有些數(shù)據(jù)可能需要多次運(yùn)行才能得到最終結(jié)果。
結(jié)語(yǔ)
如果這篇經(jīng)驗(yàn)對(duì)您有所幫助,請(qǐng)點(diǎn)擊“投票點(diǎn)贊”或者“收藏”以支持我!若仍有疑問(wèn),請(qǐng)隨時(shí)點(diǎn)擊“我有疑問(wèn)”。感謝閱讀!