我在xlsx中提取了数据库。我编写了遍历工作表的代码,并将各个单元格/字段写入管道分隔的文本文件中。首先,存在一个问题,该代码忽略以“空白”值开头的行之后的所有行。我使用UsedRange进行了更正。然后,另一个问题开始了-文件具有“固定”的列数,但有时最后3个单元格为空白。在这种情况下,它不起作用-最终会忽略空白单元格。
我认为问题在于每个循环的第二个问题(与myField有关);我花了大约5个小时的时间在Google上进行搜索,使用帮助,使用Range,单元格,End(xxx)尝试不同的组合,但无法解决。有人可以帮忙吗?
最后,代码必须遍历整个矩阵,但第一列和最后一列可能会有空白值。
我在下面粘贴部分代码。
For Each myRecord In myWkSheet.Range("A1:A" & myWkSheet.UsedRange.Rows.Count)
With myRecord
For Each myField In myWkSheet.Range(.Cells, myWkSheet.Cells(.Row, myWkSheet.Columns.Count).End(xlToLeft))
RplcText = myField.Text
RplcText = Replace(RplcText, "|", "/") 'Replaces pipes from input file to slashes to avoid mismatches during ETL
sOut = sOut & RplcText & DELIMITER
Next myField
OUTFILE_WRITELINE sOut
sOut = Empty
End With
Next myRecord`
如果第一行有一组标题标签,则.CurrentRegion
可以使用。请参见Range.CurrentRegion属性(Excel)
dim totalRows as long, totalColumns as long, dataRange as range
with sheets("sheet1").cells(1, 1).currentregion
totalRows = .rows.count
totalColumns = .columns.count
set dataRange = .offset(1, 0).resize(.rows.count -1, .columns.count) 'no header row
end with
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句