1

I want to refer to a range of cells across columns: B:C then E:M (skipping D). I want to copy the cells and paste them to another worksheet.

I have a For Next loop with the row number variable iT. How do I select them using the variable?

This selects the whole range including D.

Sheet4.Range("B" & iT & ":C" & iT, "E" & iT & ":M" & iT).Select

I tried Cells().

Community
  • 1
  • 1
Rob
  • 11
  • 1
  • 1
    `Sheet4.Range("B" & iT & ":C" & iT, "E" & iT & ":M" & iT).Select` You have the comma in the wrong place. Try `Sheet4.Range("B" & iT & ":C" & iT & ",E" & iT & ":M" & iT).Select` – Siddharth Rout Nov 27 '20 at 16:54
  • 1
    BTW you do not need to select the range. You can directly work with the range as well. You may want to read up on [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) – Siddharth Rout Nov 27 '20 at 16:55
  • Thanks. Worked great.. I will read your suggestion. – Rob Nov 27 '20 at 17:04

3 Answers3

0

If you want to use Cells Method.

Sub CopyUsingCellsMethod()
    Dim ColumnNumber As Long                                                                  
    Dim RowNumber As Long
    RowNumber = 1  'Enter Your Required Row Number Here

    With ThisWorkbook.Worksheets("Sheet4")
        For ColumnNumber = 2 To 5 Step 3  'This would Copy Range(B1:C1) into Range(I1:J1) and Then Range(E1:F1) into Range(L1:M1)
            .Range(Cells(RowNumber, ColumnNumber), Cells(RowNumber, ColumnNumber + 1)).Copy Worksheets("Sheet4").Range(.Cells(RowNumber, ColumnNumber + 7), .Cells(RowNumber, ColumnNumber + 8))
        Next ColumnNumber
    End With
End Sub
Community
  • 1
  • 1
Anmol Kumar
  • 157
  • 1
  • 7
0

Try this

   Sht.range("A:M").copy AnotherWorkbook.sheets("YourSheet").range("A1")
   AnotherWorkbook.sheets("YourSheet").range("D:D").delete
Bruno Leite
  • 1,403
  • 13
  • 17
0

A Brief Study

Copy Values, Formats, Formulas

Sub NonContiguousRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    ' Optionally:
    'Set cols = Union(Sheet1.Columns("B:C"), Sheet1.Columns("E:M"))
    Dim rRng As Range
    Set rRng = Intersect(Sheet1.Rows(iT), cols)
    rRng.Copy Sheet2.Cells(1, "A")
    ' This will also work:
    'Dim ColumnsCount As Long
    'ColumnsCount = getColumnsCount(cols)
    'rRng.Copy Sheet2.Cells(1, "A").Resize(, ColumnsCount)
    ' This will NOT work:
    'Sheet2.Cells(1, "A").Resize(, ColumnsCount).Value = rRng.Value
End Sub

Function getColumnsCount( _
    aRange As Range) _
As Long
    If Not aRange Is Nothing Then
        Dim rng As Range
        For Each rng In aRange.Areas
            getColumnsCount = getColumnsCount + rng.Columns.Count
        Next rng
    End If
End Function

Copy Values

Sub TESTgetRow()
    Dim iT As Long
    iT = 1
    Dim cols As Range
    Set cols = Sheet1.Range("B:C,E:M")
    Dim Data As Variant
    Data = getRow(cols, iT)
    Sheet2.Cells(1, "A").Resize(, UBound(Data) - LBound(Data) + 1).Value = Data
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values at the intersection of a range
'               and one of its worsheet's rows, in an array.
' Remarks:      Supports non-contiguous ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getRow( _
    aRange As Range, _
    Optional ByVal aRow As Long = 1) _
As Variant
    If Not aRange Is Nothing Then
        Dim rRng As Range
        Set rRng = Intersect(aRange, aRange.Worksheet.Rows(aRow))
        If Not rRng Is Nothing Then
            With CreateObject("Scripting.Dictionary")
                Dim rng As Range
                Dim cel As Range
                Dim n As Long
                For Each rng In rRng.Areas
                    For Each cel In rng.Cells
                        n = n + 1
                        .Item(n) = cel.Value
                    Next cel
                Next rng
                getRow = .Items
            End With
        Else
        ' Row range is empty ('Nothing').
        End If
    Else
        ' Range is empty ('Nothing').
    End If
End Function
VBasic2008
  • 44,888
  • 5
  • 17
  • 28