ThisWorkbookと他のワークブックが一致するかどうかを比較するプログラムがあります。コードが他のワークブックの2番目のワークシートでテキストを見つける必要がある場合を除いて、すべて正常に実行されています。クラッシュするまで繰り返します。私が見つけようとしているテキストセルは両方のファイルに存在しますが、何らかの理由で私のコードはそれを識別できません(私はフォーマットを確認しました、両方ともテキストフォーマットです)
クラッシュは次の行で発生しますCase taxasWks.Cells(lin_dest, 4) = transf1Wks.Cells(lin_ori_2, 1)
:
エラーは次のとおりです。実行時エラー1004アプリケーション定義またはオブジェクト定義のエラー
Dim consultaWbk As Excel.Workbook
Dim linhas1Wks As Excel.Worksheet
Dim linhas2Wks As Excel.Worksheet
Dim transf1Wks As Excel.Worksheet
Dim transf2Wks As Excel.Worksheet
Dim taxasWks As Excel.Worksheet
Dim lin_dest As Long
Dim lin_ori_1 As Long
Dim lin_ori_2 As Long
Set consultaWbk = Workbooks.Open("C:\Users\Feels Bad Man\Dropbox\Tesingz\tesingz\Com paineis de transformador - versao 2.xlsm")
Set linhas1Wks = consultaWbk.Worksheets("Taxas linhas")
Set linhas2Wks = consultaWbk.Worksheets("Tempo médio de reposição linhas")
Set transf1Wks = consultaWbk.Worksheets("Taxas Transformadores")
Set transf2Wks = consultaWbk.Worksheets("Tempo médio de reposição transf")
Set taxasWks = ThisWorkbook.Worksheets("taxas falha temp med rep")
lin_dest = 2
lin_ori_1 = 2
lin_ori_2 = 2
Do While taxasWks.Cells(lin_dest, 1) <> ""
Select Case True
Case taxasWks.Cells(lin_dest, 4).Value2 = linhas1Wks.Cells(lin_ori_1, 1).Value2:
taxasWks.Cells(lin_dest, 5).Value2 = linhas1Wks.Cells(lin_ori_1, 3).Value2
taxasWks.Cells(lin_dest, 6).Value2 = linhas1Wks.Cells(lin_ori_1, 4).Value2
taxasWks.Cells(lin_dest, 7).Value2 = linhas1Wks.Cells(lin_ori_1, 5).Value2
taxasWks.Cells(lin_dest, 8).Value2 = linhas1Wks.Cells(lin_ori_1, 6).Value2
taxasWks.Cells(lin_dest, 9).Value2 = linhas1Wks.Cells(lin_ori_1, 7).Value2
taxasWks.Cells(lin_dest, 10).Value2 = linhas2Wks.Cells(lin_ori_1, 2).Value2
taxasWks.Cells(lin_dest, 11).Value2 = linhas2Wks.Cells(lin_ori_1, 3).Value2
taxasWks.Cells(lin_dest, 12).Value2 = linhas2Wks.Cells(lin_ori_1, 4).Value2
taxasWks.Cells(lin_dest, 13).Value2 = linhas2Wks.Cells(lin_ori_1, 5).Value2
taxasWks.Cells(lin_dest, 14).Value2 = linhas2Wks.Cells(lin_ori_1, 6).Value2
lin_dest = lin_dest + 1
lin_ori_1 = 2
Case Else:
lin_ori_1 = lin_ori_1 + 1
End Select
Select Case True
Case taxasWks.Cells(lin_dest, 4).Value2 = transf1Wks.Cells(lin_ori_2, 1).Value2:
taxasWks.Cells(lin_dest, 5).Value2 = transf1Wks.Cells(lin_ori_2, 2).Value2
taxasWks.Cells(lin_dest, 6).Value2 = transf1Wks.Cells(lin_ori_2, 3).Value2
taxasWks.Cells(lin_dest, 7).Value2 = transf1Wks.Cells(lin_ori_2, 4).Value2
taxasWks.Cells(lin_dest, 8).Value2 = transf1Wks.Cells(lin_ori_2, 5).Value2
taxasWks.Cells(lin_dest, 9).Value2 = transf1Wks.Cells(lin_ori_2, 6).Value2
taxasWks.Cells(lin_dest, 10).Value2 = transf2Wks.Cells(lin_ori_2, 2).Value2
taxasWks.Cells(lin_dest, 11).Value2 = transf2Wks.Cells(lin_ori_2, 3).Value2
taxasWks.Cells(lin_dest, 12).Value2 = transf2Wks.Cells(lin_ori_2, 4).Value2
taxasWks.Cells(lin_dest, 13).Value2 = transf2Wks.Cells(lin_ori_2, 5).Value2
taxasWks.Cells(lin_dest, 14).Value2 = transf2Wks.Cells(lin_ori_2, 6).Value2
lin_dest = lin_dest + 1
lin_ori_2 = 2
Case Else:
lin_ori_2 = lin_ori_2 + 1
End Select
Loop
Set linhas1Wks = Nothing
Set linhas2Wks = Nothing
Set transf1Wks = Nothing
Set transf2Wks = Nothing
consultaWbk.Close SaveChanges:=False
Set consultaWbk = Nothing
MsgBox "END"
End Sub
空白が見つかるまでループし続けるこの種のDoWhileループは好きではありませんでした。一致するものが見つからない場合、lin_ori_1とlin_ori_2は、一致が見つかった場合に個別に2にリセットされるだけなので、ワークシートの行数を超えるまで繰り返します。
Dim fnd As Variant
With taxasWks
For lin_dest = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
fnd = Application.Match(.Cells(lin_dest, 4).Value2, linhas1Wks.Columns(1), 0)
If Not IsError(fnd) Then
'a match was found
.Cells(lin_dest, 5) = linhas1Wks.Cells(fnd , 3).Value2
.Cells(lin_dest, 6) = linhas1Wks.Cells(fnd , 4).Value2
.Cells(lin_dest, 7) = linhas1Wks.Cells(fnd , 5).Value2
.Cells(lin_dest, 8) = linhas1Wks.Cells(fnd , 6).Value2
.Cells(lin_dest, 9) = linhas1Wks.Cells(fnd , 7).Value2
.Cells(lin_dest, 10) = linhas2Wks.Cells(fnd , 2).Value2
.Cells(lin_dest, 11) = linhas2Wks.Cells(fnd , 3).Value2
.Cells(lin_dest, 12) = linhas2Wks.Cells(fnd , 4).Value2
.Cells(lin_dest, 13) = linhas2Wks.Cells(fnd , 5).Value2
.Cells(lin_dest, 14) = linhas2Wks.Cells(fnd , 6).Value2
End If
fnd = Application.Match(.Cells(lin_dest, 4).Value2, transf1Wks.Columns(1), 0)
If Not IsError(fnd) Then
'a match was found
.Cells(lin_dest, 5) = transf1Wks.Cells(fnd , 2).Value2
.Cells(lin_dest, 6) = transf1Wks.Cells(fnd , 3).Value2
.Cells(lin_dest, 7) = transf1Wks.Cells(fnd , 4).Value2
.Cells(lin_dest, 8) = transf1Wks.Cells(fnd , 5).Value2
.Cells(lin_dest, 9) = transf1Wks.Cells(fnd , 6).Value2
.Cells(lin_dest, 10) = transf2Wks.Cells(fnd , 2).Value2
.Cells(lin_dest, 11) = transf2Wks.Cells(fnd , 3).Value2
.Cells(lin_dest, 12) = transf2Wks.Cells(fnd , 4).Value2
.Cells(lin_dest, 13) = transf2Wks.Cells(fnd , 5).Value2
.Cells(lin_dest, 14) = transf2Wks.Cells(fnd , 6).Value2
End If
Next lin_dest
End With
.Value2プロパティに値を割り当てないでください。別のセルの.Value2をデフォルトの.Valueに割り当てます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加