サブストリング値に応じて、6番目のシートの範囲を1番目または3番目のシートにコピーしようとしています。私はこのコードを使用しようとすると "ランタイムエラー '1004'"が表示されます。私は解決策を見つけようとしました - 唯一の可能な答えはThisWorkbook.Save
を追加していましたが、結果は変わりませんでした。VBA:値をコピーするランタイムエラー1004
Dim LastRow As Long
'Finds last row
With ActiveSheet
LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
End With
'Iterates through rows in column A, and copies the row into proper sheet depending on "H" or "L"
For i = 4 To LastRow
If InStr(1, Range("A" & i), "L") <> 0 Then
ThisWorkbook.Worksheets(1).Range(Cells(i, 9), Cells(i, 48)).Value = ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value
ElseIf InStr(1, Range("A" & i), "H") <> 0 Then
ThisWorkbook.Worksheets(3).Range(Cells(i, 9), Cells(i, 48)).Value = ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value
End If
Next i
'Message Box when tasks are completed
MsgBox "Complete"
デバッガは、エラーが発生した後起動されると、それが特異的ElseIf
結果浮き彫りに:
ThisWorkbook.Worksheets(3).Range(Cells(i, 9), Cells(i, 48)).Value = ThisWorkbook.Worksheets(6).Range(Cells(i, 1), Cells(i, 40)).Value
を編集下記答えた人たちからの助けに
おかげで、私ができました問題を解決する。 .Cells
を.Range
から削除し、希望するセルを.Range("I" & y1 & ":AV" & y1)
(最初の変更例)に参照しました。さらに、変数としてwsR
、wsL
、およびwsH
と定義したので、私がワークシートを引用した方法を変更しました。 With
/End With
ステートメントにも追加されています。
Private Sub CommandButton2_Click()
Dim LastRow As Long
Dim wsR As Worksheet
Dim wsL As Worksheet
Dim wsH As Worksheet
Dim y1 As Integer
Dim y2 As Integer
'Set row value for light chain
y1 = 4
'Set row value for heavy chain
y2 = 4
'Set raw data worksheet
Set wsR = ThisWorkbook.Worksheets(6)
'Set light chain worksheet
Set wsL = ThisWorkbook.Worksheets(1)
'Set heavy chain worksheet
Set wsH = ThisWorkbook.Worksheets(3)
'Finds last row
With wsR
LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
End With
'Iterates through rows in column A, and copies the row into proper sheet depending on "H" or "L"
For i = 4 To LastRow
'Checks for "L" for light chain
If InStr(1, Range("A" & i), "L") <> 0 Then
With wsL
.Range("I" & y1 & ":AV" & y1).Value = wsR.Range("A" & i & ":AN" & i).Value
End With
y1 = y1 + 1
'Checks for "H" for heavy chain
ElseIf InStr(1, Range("A" & i), "H") <> 0 Then
With wsH
.Range("I" & y2 & ":AV" & y2).Value = wsR.Range("A" & i & ":AN" & i).Value
End With
y2 = y2 + 1
End If
Next i
'Message Box when tasks are completed
MsgBox "Complete"
End Sub
を参照してください。この一般的な問題に関する拡張された議論のために、.Cells](https://stackoverflow.com/questions/36368220/is-the-in-range-necessary-when-defined-by-cells)で定義されている場合に必要な範囲で。 – Jeeped