Need Help Copying Only Some Cells in VBA

SilverSlug

New Member
Joined
Oct 28, 2015
Messages
6
I'm working on a code in VBA to copy rows from sheet "ALL FILES" and paste them into "VIEW STUDENT" based on "JOHN SMITH". Right now it copies the entire row, but I only want to select columns D, E, H-AB (4, 5, 8-28) in the same row. Any ideas?

Code:
Sub RunMain()

Dim r As Long, endRow As Long, pasteRowIndex As Long
   
    Sheets("ALL FILES").Select

endRow = 50
pasteRowIndex = 1

For r = 1 To endRow 

    If Cells(r, Columns("B").Column).Value = "JOHN SMITH" Then 'Found



'I BELIEVE THAT THIS CODE NEEDS TO BE CHANGED...
            'Copy the current row
            Rows(r).Select
            Selection.Copy
            



           Sheets("view students").Select
            Rows(pasteRowIndex).Select
            ActiveSheet.Paste

            'Next time you find a match, it will be pasted in a new row
            pasteRowIndex = pasteRowIndex + 1

           'Switch back to your table & continue to search for your criteria
            Sheets("ALL FILES").Select
    End If

Next r
 'REVET TO VIEW NORMAL SHEET AGAIN
   Worksheets("view students").Activate
   
  'CLEAR CLIPBOARD
   Application.CutCopyMode = False
   
   'ADDS BLANK ROWS TO TOP OF CHART
       Rows("1:1").Select
    Range("B1").Activate


End Sub

I also need to edit the code so it searches for the value of a ListBox. Any ideas?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Here is some code that may help you.

I suggest you try to stay away from "Select" and "Activate"

Also, specify which sheet you are referencing.

Let me know how it works for you...

Code:
Sub main()
    CopyRow "John Smith"
End Sub
Function GetRowFor(sName) As Long
    GetRowFor = 0
    Dim r As Long, rng As Range, fnd As Range
    [B]With Sheets("ALL FILES")[/B]
        Set rng = .Range("A1").CurrentRegion.Offset(1, 0)
        Set rng = rng.Resize(rng.Rows.Count - 1, rng.Columns.Count)
        Set fnd = rng.Find(What:=sName, LookIn:=xlConstants, Lookat:=xlPart, MatchCase:=False)
        If Not fnd Is Nothing Then GetRowFor = fnd.Row
    End With
End Function
Sub CopyRow(sName)
    Dim lrow
    lrow = GetRowFor(sName)
    Select Case lrow
        Case 0
            MsgBox "Could not find"
        Case Else
        [B]Sheets("All Files")[/B].Rows(lrow).Copy
        [B]Sheets("view students")[/B].Rows(1).Insert
        Application.CutCopyMode = False
    End Select
End Sub
 
Upvote 0
tlorwy, thanks for the help!
all I get is:

Runtime '1004' error
Application-defined or object-defined error

the debugger takes me to the code

Code:
Set rng = rng.Resize(rng.Rows.Count - 1, rng.Columns.Count)

I'm still pretty new at this so it may be a newbie mistake I made running it.

Thanks again for the help.
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,003
Members
449,203
Latest member
Daymo66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top