VBA压扁文件

时间:2017-07-09 11:58:41

标签: excel vba excel-vba

我正在寻找vba代码来清理带有分组数据的excel文件。

有许多客户,许多不同的账户,许多不同的细节。

需要根据创建报告的需要完成100个文件。

我的数据如下:

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

我需要这个:

客户端|帐号|帐户类型|自动收报机|数量|值

我有一些代码几乎可以运作,但已经知道它不是最佳做法,所以不要试图弄清楚如何让它工作,以为我会寻求帮助。

以下是我所拥有的:

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

此代码在Cells(clname,10)处抛出运行时1004错误。选择

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

试试这个,也没有定义变量acno,以及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