VBA压扁一个文件

我正在寻找VBA代码来清理分组数据的Excel文件。

与许多客户,许多不同的帐户,许多不同的细节。

需要完成100个文件,才能创build报告。

我有这样的数据:

#client A# ##Account number | Account Type# ##many rows of details## ###Ticker | Quantity | Value# ###many rows of details### 

我需要这个:

客户端| 帐号| 帐户types| Ticker | 数量| 值

我有一些代码几乎可以工作,但是已经读过这不是最佳实践,所以不要试图弄清楚如何使它工作,我想请求帮助。

这是我有:

 Sub crk() Dim inv As Long, name As Long, tipe As Long Dim assname As Long, ticker As Long, broad As Long Dim assid As Long, value As Long Dim i As Long, j As Long, clname As Long inv = 2 name = 2 acno = 2 tipe = 2 assname = 2 ticker = 2 broad = 2 assid = 2 value = 2 clname = 2 For i = 1 To 50 For j = 1 To 9 Sheets("by-investor-raw").Select x = Cells(i, j).value If x = "Acct Name" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(name, 2).Select ActiveSheet.Paste name = name + 1 ElseIf x = "Acct No" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(acno, 3).Select ActiveSheet.Paste acno = acno + 1 ElseIf x = "Acct Type" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(tipe, 4).Select ActiveSheet.Paste tipe = tipe + 1 ElseIf x = "Asset Name" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(assname, 5).Select ActiveSheet.Paste assname = assname + 1 ElseIf x = "Ticker" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(ticker, 6).Select ActiveSheet.Paste ticker = ticker + 1 ElseIf x = "Broad" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(broad, 7).Select ActiveSheet.Paste broad = broad + 1 ElseIf x = "Asset ID" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(assid, 8).Select ActiveSheet.Paste assid = assid + 1 ElseIf x = "Value" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(value, 9).Select ActiveSheet.Paste value = value + 1 ElseIf x = "Investor" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(assid, 1).Select ActiveSheet.Paste ElseIf x = "Investor's Name" Then Cells(i + 1, j).Select Selection.Copy Sheets("portfolio-clean").Select Cells(clname, 10).Select TargetSheet.Paste ActiveSheet.Paste clname = clname + 1 End If Next j Next i Sheets("portfolio-clean").Select End Sub 

此代码在单元格(clname,10)处抛出运行时1004错误。select

谢谢你的帮助

试试这个,variablesacno也没有定义,还有x。

 Option Explicit Sub crk() Dim inv As Long, name As Long, tipe As Long Dim assname As Long, ticker As Long, broad As Long Dim assid As Long, value As Long Dim i As Long, j As Long, clname As Long Dim acno, x inv = 2 name = 2 acno = 2 tipe = 2 assname = 2 ticker = 2 broad = 2 assid = 2 value = 2 clname = 2 For i = 1 To 50 For j = 1 To 9 x = Cells(i, j).value If x = "Acct Name" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(name, 2) name = name + 1 ElseIf x = "Acct No" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(acno, 3) acno = acno + 1 ElseIf x = "Acct Type" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(tipe, 4) tipe = tipe + 1 ElseIf x = "Asset Name" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(assname, 5) assname = assname + 1 ElseIf x = "Ticker" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(ticker, 6) ticker = ticker + 1 ElseIf x = "Broad" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(broad, 7) broad = broad + 1 ElseIf x = "Asset ID" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(assid, 8) assid = assid + 1 ElseIf x = "Value" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(value, 9) value = value + 1 ElseIf x = "Investor" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(assid, 1) ElseIf x = "Investor's Name" Then Sheets("by-investor-raw").Cells(i + 1, j).Copy Destination:=Sheets("portfolio-clean").Cells(clname, 10) clname = clname + 1 End If Next j Next i Sheets("portfolio-clean").Select End Sub