我从文本文件中提取了一些数据。每天格式相同,但数据不同
这会填充到实时报告中,供我的团队查看和监视。
报告中的最后一列为“允许”。目前,我只是删除任何带有“允许”的行
我想做的是要么在数据填充之前将其删除,要么在导入后立即删除行
注意,“允许”的选项为“是”和空白
在输入数据时是否可以停止输入行?
我的代码是:
Option Explicit
Private Sub Import()
Dim ws As Worksheet, lastRowC As Long
Set ws = Worksheets("Report")
lastRowC = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row + 1 ' bottom populated cell of Column "C", plus 1
With ws.QueryTables.Add(Connection:= _
"TEXT;N:\Operations\001 Daily Management\Shop Goods\FMSQRY.CSV", Destination:= _
ws.Cells(lastRowC, 2))
.Name = "FMSQRY"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
With ActiveWorkbook
.Connections("FMSQRY").Delete
With ws
.Names("FMSQRY").Delete
End With
End With
End Sub
Private Sub TodaysDate()
Dim ws As Worksheet, lastRowC As Long, lastRowH As Long
Set ws = Worksheets("Report")
lastRowH = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1 ' bottom populated cell of Column "A", plus 1
lastRowC = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row ' bottom populated cell of Column "B"
With ws.Range(ws.Cells(lastRowH, 1), ws.Cells(lastRowC, 1))
.FormulaR1C1 = "=TODAY()"
.Value = .Value
End With
End Sub
答案 0 :(得分:1)
如果可以设法拥有适当的标头,则可以使用ADO连接和SQL语句进行过滤。但是,由于CSV数据中没有适当的标题,因此最简单的解决方案是在导入后立即删除行。
想象一下导入后的以下数据:
以下代码将删除列F中包含class ProfileUser extends Component {
state = {
user: {}
};
componentDidMount() {
// When first time i load component it works fine, i call database and get info
this.props.getUserById(this.props.match.params.id);
}
componentWillReceiveProps(nextProps) {
// Here I get user information
this.setState({user: nextProps.profile.user})
// when I switch to another page with another ID this.props.match.params.id, this method works but I can't call this.props.getUserById(this.props.match.params.id); from here because an infinite loop appears
// this.props.getUserById(this.props.match.params.id);
}
render() {
console.log(this.state.user)
return (<div></div>)
}
的所有行。您需要将列名调整为您使用的列名:
allowed
答案 1 :(得分:0)
要求:
每天导入CSV文件
CSV文件没有标题
排除CSV文件的字段编号12中标记为YES
的所有记录
字段12中的值为:YES
和Null
(即空白)
将经过过滤的CSV数据追加到名为Report
的现有工作表中
新数据应发布在现有数据的末尾,从第2列开始
1在第1列中,新数据应具有导入数据的日期。
解决方案:
此解决方案使用ADODB.Connection
,ADODB.RecordSet
和SQL
语句来过滤数据并添加包含处理日期的Date
字段(在第一个位置)。
是的,我们都听到过很多关于这个神话的说法,即ADODB连接需要标头…并非如此!
让我们使用以下Connection
属性:
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & sPath & ";" & _
"Extended Properties='text;HDR=No;FMT=Delimited(,)'"`
其中:
sPath
:包含文件位置
HDR=No
:表示没有标题
FMT=Delimited(,)
:表示一个CSV文件
此SQL command
提取所需的数据,同时添加字段Date
:
"SELECT #" & Date & "# As [DATE], * FROM [" & sFile & "] Where [F12] Is Null"
此函数根据提供的recordset
,使用从CSV文件中提取的数据来创建SQL statement
。
它具有recordset
(输出对象),path
,filename
和SQL statement
作为参数,以提供灵活性。
它返回一个布尔值,指示过程的结果。 True
:找到并提取与SQL command
匹配的记录,False
:没有与SQL command
匹配的记录。
Public Function SQL_ƒCsv_ToRecordSet(oOutput As Object, _
sPath As String, sFile As String, sSql As String) As Boolean
Dim oAdCn As Object, oAdRs As Object
Rem Set Objects
Set oAdCn = CreateObject("ADODB.Connection")
Set oAdRs = CreateObject("ADODB.Recordset")
Rem Open Connection
With oAdCn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & sPath & ";" & _
"Extended Properties='text;HDR=No;FMT=Delimited(,)'"
.Open
End With
Rem Apply SQL to Source
With oAdRs
.LockType = 1 'adLockReadOnly
.CursorType = 3 'adOpenStatic
.ActiveConnection = oAdCn
.Open Source:=sSql, Options:=1 'adCmdText
Rem Validate Results
If .RecordCount = 0 Then Exit Function
End With
Rem Set Results
Set oOutput = oAdRs
SQL_ƒCsv_ToRecordSet = True
End Function
使用此过程调用函数:
Private Sub Csv_Import()
Dim oAdRs As Object, ws As Worksheet
Dim sPath As String, sFile As String, sSql As String
Dim lRow As Long, sMsgBdy As String
Rem Set Variables & Objects
sFile = "FMSQRY.CSV"
sPath = "N:\Operations\001 Daily Management\Shop Goods" 'No separator at the end!
sSql = "SELECT #" & Date & "# As [DATE], * FROM [" & sFile & "] Where [F12] Is Null" 'Update as required
Set ws = ThisWorkbook.Worksheets("Report") 'Update as required
Rem Extract filtered data from csv file
If SQL_ƒCsv_ToRecordSet(oAdRs, sPath, sFile, sSql) Then
Rem Post extracted records (with the date of extraction in Field 1)
With ws
lRow = 1 + .Cells(.Rows.Count, 3).End(xlUp).Row
.Cells(lRow, 1).CopyFromRecordset oAdRs
End With
sMsgBdy = "Records added successfully…"
Else
Rem No Records Filtered
sMsgBdy = "No records found in: " & vbCrLf _
& vbTab & sFile & vbCrLf _
& vbTab & sPath
End If
MsgBox sMsgBdy, vbInformation
End Sub