excel - 收到运行时错误 13 将使用 instring 函数
问题描述
当我第二次导入文件时,第一次的数据没有被删除。因此,一旦我导入第二个文件,第二个文件数据就会从第一个数据旁边的列开始。我在下面的代码,在导入部分之后我试图解决这个问题
Sub ImportCMOSLog(Path As String, filename As String)
'Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Data").Select
On Error Resume Next
'ActiveSheet.Name = filename
Range("A1").FormulaR1C1 = filename
Range("A2").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Path & filename, Destination:=Range("$A$1"))
.Name = filename 'change activesheet to sheets("Data")
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = False
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = " "
.Refresh BackgroundQuery:=False
End With
End Sub
Dim strSearch As String
strSearch = "Time/min"
Dim x As String
Dim sheet As String
Dim myrange As String
sheet = "Data"
myrange = "$A$1:$BC$1"
Set x = Worksheets("Data").Range("$A$1:$BC$1") [object required error over here]
For Each Cell In x
If InStr(x.Value, strSearch) > 0 Then [runtime error received over here]
lastrow = ActiveCell.SpecialCells(xlLastCell).Row
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B1").Select
ActiveCell.FormulaR1C1 = "Time/min"
Range(Cells(2, 2), Cells(lastrow, 2)).Select
Selection.FormulaR1C1 = "=(RC[-1]-R3C1)*24*60"
Selection.NumberFormat = "General"
Else
Sheets("Data").Cells.Clear
End If
Next Cell
解决方案
我认为你让它变得复杂然后需要(我可能错了)但你实际上想要做的是
- 清除旧数据
- 插入标题为 的新数据
Date
,Y1
并且Y2
- 在其中插入一列
B
- 给它一个标题,
Time/min
以便新标题是Date
,Time/min
和Y1
Y2
- 在列中插入公式
B
如果我的理解是正确的,那么试试这个(未测试)。如果您有任何错误,请告诉我。
Option Explicit
Sub ImportCMOSLog(Path As String, filename As String)
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Data")
With ws
.Cells.Clear
With .QueryTables.Add(Connection:= _
"TEXT;" & Path & filename, Destination:=.Range("$A$1"))
.Name = filename
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = False
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileDecimalSeparator = "."
.TextFileThousandsSeparator = " "
.Refresh BackgroundQuery:=False
End With
.Columns(2).Insert Shift:=xlToRight
.Range("B1").Value = "Time/min"
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B2:B" & lRow).FormulaR1C1 = "=(RC[-1]-R3C1)*24*60"
End With
End Sub
推荐阅读
- javascript - 尽管有成功消息,为什么用户输入不会进入数据库?
- android - OkHttpResponse - 解析接收到的分块数据,即 text/html
- r - R语言,如何使用引导程序生成最大似然和AICc?
- c# - 如何在 WriteableBitmap 上写文本?
- angular - ng2-completer 搜索对象数组时不返回任何搜索结果
- python - 如何使用嵌套字典映射数据框中的列?
- javascript - 使用 gatsby-source-filesystem 配置构建模式时出错(遵循官方教程)
- html - 边框在 CSS 上的重叠元素上仍然可见
- php - 如何将 ICS/VCALENDAR 文件标记为生命周期结束/不再同步?
- python - 绘图类型问题:TypeError:只有大小为 1 的数组可以转换为 Python 标量