我有两个Excel作为输入。我从一个excel中获取值并将其作为参数传递给其他excel以应用过滤器。一旦过滤器打开,我正在读取列中的过滤值并尝试匹配下面代码中提到的条件。一切都很好,到这个地方。一旦我得到特定的匹配,我必须将它写回第一个Excel,但这样做时会出现运行时错误。
strPath = "C:\Users\PSingh\Desktop\CodeInventory.xlsx"
strPath1 = "C:\Users\PSingh\Desktop\MyScripts\Processes.xlsx"
Dim rows
Set objExcel1 = CreateObject("Excel.Application")
objExcel1.Visible = True
Set objExcel2 = CreateObject("Excel.Application")
objExcel2.Visible = True
Set objDict = CreateObject("Scripting.Dictionary")
objDict.CompareMode = vbTextCompare
Const xlUp = -4162
Const xlCellTypeVisible = 12
Dim a(), val
objExcel1.Workbooks.Open(strPath1)
With objExcel1.Activeworkbook.Sheets("Sheet1")
rowcount = .Range("A" & .Rows.Count).End(xlUp).Row
MsgBox rowcount
End With
Redim Preserve a(rowcount)
For i=1 To rowcount
a(i) = objExcel1.Activeworkbook.Sheets("Sheet1").Cells(i,1).Value
objDict(a(i)) = a(i)
'msgbox a(i)
Next
n = objDict.Items
objExcel2.Workbooks.Open(strPath)
For i=0 To UBound(n)
With objExcel2.Activeworkbook.Sheets("All")
.Range("A1").AutoFilter 19, "="&n(i)
'rows=.usedrange.columns(1).specialcells(xlCellTypeVisible)
For Each cl In objExcel2.Activeworkbook.Sheets("All").UsedRange.Columns(12).SpecialCells(xlCellTypeVisible)
If (InStr(cl, "Seq") <> 0 Or InStr(cl,"seq")) <> 0 Then
objExcel1.Activeworkbook.Sheets("Sheet1").Cells(i,2) = "Data" 'Not Working
Exit For
End If
Next
End With
Next
答案 0 :(得分:2)
尝试使用,
objExcel1.Activeworkbook.Sheets("Sheet1").Cells(i + 1, 2) = "Data"
i
的循环从For i = 0 to ...
开始。 B列中没有行零。