我有一个要转换的csv文件。问题是这些列包含小于5000的整数。在这些列中还包含标识符,例如IE0034230957和空白值。应用代码时,它们不应更改。
csv应该打开并产生一个新文件。
有人可以帮助我更改有效的代码吗?
我阅读了有关此主题,可能会看到以下代码,但是它不起作用:
Sub RemoveSmallValues()
Dim myfilename As String
Dim myfilepath As String
Dim newfilename As String
Dim N As Long
Dim i As Long
Dim cellvalue As Long
Dim rng As Range, r As Range, lm As Double
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
myfilepath = "Q:\Pre trade"
myfilename = "Snapshot_of_Model.csv"
Workbooks.Open (myfilepath)
Workbooks(myfilename).Activate 'Makes SnapShot.csv the active workbook
Set rng = Range("D:F")
lm = 5000
For Each r In rng
If r.Value < lm Then
r.Clear
Next r
newfilename = "Q:\Snapshot_final.csv" 'new file path and file name without extension.
Workbooks(myfilename).SaveAs newfilename, FileFormat:=xlCSV 'Save the file with extension CSV
ActiveWorkbook.Close False 'Close the workbook without saving, as you have already saved the workbook with line before.
End Sub
答案 0 :(得分:0)
您的问题,我会像这样
Sub t()
Dim r As Excel.Range
Dim c As Excel.Range
Set r = Range("a1").Resize(Cells(Rows.Count, 1).End(xlUp).Row, 1)
For Each c In r.Cells
If IsNumeric(c.Value) Then
If CDbl(c.Value) < 5000 Then c.Clear
End If
Next c
Set r = Nothing
End Sub
答案 1 :(得分:0)
也可以使用SQL和ADO来完成。
Sub x()
Dim cnCSVConnection As ADODB.Connection
Dim rstResults As ADODB.Recordset
Set cnCSVConnection = New ADODB.Connection
cnCSVConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Workspace\SW Pensions\RUFUS\;" & _
"Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
'"C:\Workspace\"
cnCSVConnection.Open
Set rstResults = New ADODB.Recordset
rstResults.Open "Select [Entry-date],[Current value of policy]," & _
"iif([Current value of policy]<100,1,0) from " & _
"[Bonds Claims_WIP_TESTING.csv]", cnCSVConnection, adOpenKeyset
Range("a1").CopyFromRecordset rstResults
rstResults.Close
cnCSVConnection.Close
Set rstResults = Nothing
Set cnCSVConnection = Nothing
End Sub