5,172
社区成员




Sub Import()
Dim Path As String
Dim ObjApplication As Application
Dim WB As Workbook
Dim Sht As Worksheet
Dim StrtgyID As String, Length As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.clear
.Filters.Add "Excel Files", "*.csv"
.Filters.Add "All Files", "*.*"
If .Show <> -1 Then Exit Sub
Path = .SelectedItems(1)
End With
Set ObjApplication = New Application
With ObjApplication
.Visible = False
.DisplayAlerts = False
End With
Set WB = ObjApplication.Workbooks.Open(Path, , True)
Set Sht = WB.Worksheets(1)
Dim HoldInfo As Variant
Dim Cnt As Integer, i As Integer
If Sht.Cells(1, 2) <> "代码" Then
MsgBox ("导入的数据格式有误,请确认后重新导入!")
Exit Sub
End If
Cnt = 0
For i = 1 To Sht.UsedRange.Rows.Count
If IsNumeric(Sht.Cells(i, 2)) Then
Cnt = Cnt + 1
End If
Next i
If Cnt = 0 Then
MsgBox "Imported Files Are Empty."
Exit Sub
End If
Sheets("持仓").Range("A1" & ":" & "J" & CStr(Sheets("持仓").UsedRange.Rows.Count)).clear
Sheets("持仓").Range("L2").Value = Path
Dim j As Integer, Idx As Integer
Sheets("当前持仓").Cells(1, 1) = "名称"
Sheets("当前持仓").Cells(1, 2) = "账户"
Sheets("当前持仓").Cells(1, 3) = "代码"
Sheets("当前持仓").Cells(1, 4) = "简称"
Sheets("当前持仓").Cells(1, 5) = "持仓"
Sheets("当前持仓").Cells(1, 6) = "市值"
Sheets("当前持仓").Cells(1, 7) = "成本"
Sheets("当前持仓").Cells(1, 8) = "盈亏"
Length = UBound(Split(Path, "\"))
StrtgyID = CStr(Split(Split(Path, "\")(Length), ".")(0))
Idx = 2
For j = 2 To Sht.UsedRange.Rows.Count
If Sht.Cells(j, 2) <> "" And Val(Split(Sht.Cells(j, 1), "(")(0)) = Val(Sheets("持仓").Range("P5").Value) Then
Sheets("持仓").Cells(Idx, 1) = StrtgyID
Sheets("持仓").Cells(Idx, 2) = Val(Split(Sht.Cells(j, 1), "(")(0))
Sheets("持仓").Cells(Idx, 3) = Sht.Cells(j, 2)
Sheets("持仓").Cells(Idx, 5) = Sht.Cells(j, 5)
Sheets("持仓").Cells(Idx, 6) = Sht.Cells(j, 13)
Sheets("持仓").Cells(Idx, 7) = Sht.Cells(j, 5) * Sht.Cells(j, 10)
Sheets("持仓").Cells(Idx, 8) = Sht.Cells(j, 13) - Sht.Cells(j, 5) * Sht.Cells(j, 10)
Idx = Idx + 1
End If
Next j
Sheets("持仓").Range("C:C").NumberFormatLocal = "000000"
Sheets("持仓").Range("E:H").NumberFormatLocal = "#,##0.00"
WB.Close False
Set Sht = Nothing
Set WB = Nothing
Set ObjApplication = Nothing
End Sub