Dylan stolp
Dylan stolp

Reputation: 1

Pasting Rows B2:B16 from Sheet 2 onto Newly Inserted column on Sheet 1

I am attempting to have a macro insert a column on sheet "Runs", and then paste information from sheet "Templates" onto the newly inserted column on a specific Row. I have named the range for row four as "Eight", however, info from templates is pasted onto column A, Row 4, and not the newly inserted column.

Set myWorksheet = Worksheets("Runs")
myFirstColumnT = myWorksheet.Cells.Find( _
                                         What:="TS", _
                                         LookIn:=xlFormulas, _
                                         LookAt:=xlPart, _
                                         SearchOrder:=xlByColumns, _
                                         SearchDirection:=xlPrevious).Column
                                    

    myLastColumnT = myWorksheet.Cells.Find( _
                                         What:="TE", _
                                         LookIn:=xlFormulas, _
                                         LookAt:=xlPart, _
                                         SearchOrder:=xlByColumns, _
                                         SearchDirection:=xlPrevious).Column
                                         
    For iCounter = myLastColumnT To (myFirstColumnT + 1) Step -100000000
        myWorksheet.Columns(iCounter).Insert
        Sheets("Templates").Select
        Range("B2:B16").Copy
        Sheets("Runs").Select
        With Columns(iCounter).Select
            Range("eight").PasteSpecial
        End With
    Next iCounter

Upvotes: 0

Views: 44

Answers (1)

Warcupine
Warcupine

Reputation: 4640

The issue is your With doesn't actually do anything and if it did your Range doesn't reference it.

I've removed your loop, it has such a massive step it isn't actually looping anything. Also you don't need a loop for this.

I removed the .column from your .finds because that will cause an error if it fails, I also added in some error checking for if (when) it doesn't find anything.

I removed all instances of .Select because they aren't necessary.

    Dim myworksheet As Worksheet
    Dim myfirstcolumnt As Range
    Dim mylastcolumnt As Range
    
    Dim newcol As Long
    Set myworksheet = Worksheets("Runs")

    'I'm assuming you need this for a reason other than the loop, otherwise you can remove it
    Set myfirstcolumnt = myworksheet.Cells.Find( _
                                         What:="TS", _
                                         LookIn:=xlFormulas, _
                                         LookAt:=xlPart, _
                                         SearchOrder:=xlByColumns, _
                                         SearchDirection:=xlPrevious)
                                    
    If myfirstcolumnt Is Nothing Then
        MsgBox "TS not found"
        Exit Sub
    End If

    Set mylastcolumnt = myworksheet.Cells.Find( _
                                         What:="TE", _
                                         LookIn:=xlFormulas, _
                                         LookAt:=xlPart, _
                                         SearchOrder:=xlByColumns, _
                                         SearchDirection:=xlPrevious)

    If mylastcolumnt Is Nothing Then 'Avoiding errors
        MsgBox "TE not found"
        Exit Sub
    End If
    
    newcol = mylastcolumnt.Column + 1 'No need to loop to find the column you're making
    myworksheet.Columns(newcol).Insert 'use the new column index to add the column
    Sheets("Templates").Range("b2:b16").Copy 
    myworksheet.Cells(4, newcol).PasteSpecial 'We know it's going in row 4 and we have the new column index now

If you want to use your named range you can do myworksheet.Cells(myworksheet.range("Eight").Row, newcol)... Though I suggest changing the name, a range called "Eight" pointing to Row 4 isn't very clear.

Upvotes: 1

Related Questions