我正在开发一个项目,在excel中有大量数据。每个数据都有行和列的编号。我想按条件将这些数据移到相应的表格中。
Excel工作簿:
第一个工作表名称为Shortage Report
,它具有应按州方式移动到相应工作表的所有数据。
示例:
Shortage Report
列A
包含城市名称:
Mumbai
Bangalore
Hyderabad
Kochi
Chennai
..... so on.
和列D
有短名称。即
Mumbai - MU
Bangalore - BU
etc.
我想使用条件公式移动数据:
A = Mumbai
和列D = MU
然后移至工作表MU
。A = Bangalore
和列D = BU
然后移至工作表BU
。A = Chennai
和列D = CH
然后移至工作表CH
。请你帮我创建VB脚本,将这些数据在运行中移动到相应的工作表中。
这对我很有帮助。我花了将近3个小时来完成这项任务。
答案 0 :(得分:0)
如果你有这样的数据:
A B C
1 Mumbai MU Some1
2 Bangalore BU Some2
3 Hyderabad HY Some3
4 Kochi KO Some4
5 Chennai CH Some5
6 Mumbai MU Some6
7 Bangalore BU Some7
8 Chennai CH Some8
9 Hyderabad HY Some9
10 Mumbai MU Some10
11 Mumbai MU Some11
12 Chennai CH Some12
13 Mumbai MU Some13
14 Bangalore BU Some14
15 Hyderabad HY Some15
16 Bangalore BU Some16
17 Chennai CH Some17
18 Bangalore BU Some18
19 Kochi KO Some19
20 Kochi KO Some20
21 Bangalore BU Some21
在名为Shortage Report
的工作表上。然后你可以使用这段代码:
Sub qwerty()
Dim i, Lastrow, j
Dim SheetName As String
Dim wb As Workbook
Set sr = Worksheets("Shortage Report")
Lastrow = sr.Range("B" & Rows.Count).End(xlUp).Row
If wb Is Nothing Then Set wb = ThisWorkbook
For j = 1 To Lastrow
SheetName = sr.Range("B" & j).Value
Application.DisplayAlerts = False
On Error Resume Next
If wb.Sheets(SheetName) Is Nothing Then
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).name = SheetName
End With
End If
Application.DisplayAlerts = True
sr.Range("A" & j & ":C" & j).Copy
i = wb.Sheets(SheetName).Range("A" & Rows.Count).End(xlUp).Row + 1
wb.Sheets(SheetName).Range("A" & i & ":C" & i).PasteSpecial Paste:=xlValues
Next j
End Sub
使用以下数据制作5张纸:
A B C
1 Mumbai MU Some1
2 Mumbai MU Some6
3 Mumbai MU Some10
4 Mumbai MU Some11
5 Mumbai MU Some13
在名为MU
A B C
1 Bangalore BU Some2
2 Bangalore BU Some7
3 Bangalore BU Some14
4 Bangalore BU Some16
5 Bangalore BU Some18
6 Bangalore BU Some21
在名为BU
等