私は、これはあなたが探しているものだと思います。コードの上に
Sub CopyNonBlankCells()
Dim cel As Range, myRange As Range, CopyRange As Range
Set myRange = Sheet1.Range("C1:C20") '---> give your range here
For Each cel In myRange
If Not IsEmpty(cel) Then
If CopyRange Is Nothing Then
Set CopyRange = cel
Else
Set CopyRange = Union(CopyRange, cel)
End If
End If
Next cel
CopyRange.Copy Sheet2.Range("C1") '---> enter desired range to paste copied range without blank cells
End Sub
は範囲C1:C20
をコピーします~C1
~Sheet2
これをhereから取得しました。
EDIT:あなたは
Set myRange = Sheet1.Range("G:G")
Set myRange = Sheet2.Range("G:G")
myRange
以下のようなものがSheet1.Range("G:G")
への最初のセットで、その後になります書きます場合は、以下の答えはあなたのコメント ________________________________________________________________________________
に基づいていますSheet2.Range("G:G")
は、電流レンジがmyRange
はSheet2.Range("G:G")
です。
複数の範囲を使用する場合は、UNION
の機能を使用することができますが、UNIONを使用する場合は範囲が異なるが1つのシートしか結合できないという制限があります。そして、あなたの要件は、異なるシートの範囲を組み合わせることです。これを達成するために、新しいワークシートを追加し、すべてのシートの範囲をG:G
の範囲に追加します。その後、新しく追加されたシートを使用した後、私はそれを削除しています。
次のコードは、Result
という名前のシートに出力します。
Sub CopyNonBlankCells()
Dim cel As Range, myRange As Range, CopyRange As Range
Dim wsCount As Integer, i As Integer
Dim lastRow As Long, lastRowTemp As Long
Dim tempSheet As Worksheet
wsCount = Worksheets.Count '--->wsCount will give the number of Sheets in your workbook
Set tempSheet = Worksheets.Add '--->new sheet added
tempSheet.Move After:=Worksheets(wsCount + 1)
For i = 1 To wsCount
If Sheets(i).Name <> "Result" Then '---> not considering sheet "Result" for taking data
lastRow = Sheets(i).Cells(Rows.Count, "G").End(xlUp).Row '--->will give last row in sheet
lastRowTemp = tempSheet.Cells(Rows.Count, "G").End(xlUp).Row '--->will give last row in newly added sheet
Sheets(i).Range("G1:G" & lastRow).Copy _
tempSheet.Range("G" & lastRowTemp + 1).End(xlUp)(2)
End If
Next i
lastRowTemp = tempSheet.Cells(Rows.Count, "G").End(xlUp).Row
Set myRange = tempSheet.Range("G1:G" & lastRowTemp) '--->setting range for removing blanks cells
For Each cel In myRange
If Not IsEmpty(cel) Then
If CopyRange Is Nothing Then
Set CopyRange = cel
Else
Set CopyRange = Union(CopyRange, cel)
End If
End If
Next cel
CopyRange.Copy Sheets("Result").Range("G1") '---> enter desired range to paste copied range without blank cells
Application.DisplayAlerts = False
tempSheet.Delete '--->deleting added sheet
Application.DisplayAlerts = True
End Sub
を魔法の弾丸は、ここではありません。スタックオーバーフローは私のサイトのコードではありません。あなたが望むものを推測するためにも、あなたのポストには十分な情報がありません。既存のコードの特定の問題を支援します。 –