VBA code to repeat the code on different rows

petaaalouise

New Member
Joined
Apr 23, 2015
Messages
23
Hey,

I've got a code written to transfer the exact data I want from each row. How do I apply this to multiple rows as there is about 300 rows I need to go threw.

Range ("B5").Select
Selection.Copy
Sheets ("TemplateTrans").Select
Range("B16").Select
ActiveSheet. Paste
Sheets ("Master").Select
Range("C5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets ("TemplateTrans").Select
Range("C16").Select
ActiveSheet.Paste
Sheets ("Master").Select
Range ("D5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets ("TemplateTrans").Select
Range ("A16").Select
ActiveSheet.Paste
Sheets ("Master").Select
ActiveSheet.Shapes.Range (Array (check Box 84")).Select
Application.CutCopyMode =False
Selection.OnAction = "Macro2"


I'm also using a check box for each row as some rows are not required in the new sheet. Is there a way I can go without writing 300 codes??


Please help :(
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You can copy a range in one go, like this:

Sheets("Sheet1").Range("B5:D5").Copy Destination:=Sheets("TemplateTrans").Range("B16")

or if you just want values

Sheets("TemplateTrans").Range("B16:D16").Value = Sheets("Sheet1").Range("B5:D5").Value

(I'm not sure if D5 --> A16 was a typo, the code above assumes D5 ---> D16).

If you have 300 checkboxes, there are ways around writing 300 sets of code. But why do you need a checkbox on each line? Do you want to copy just the lines selected randomly by the user, or can they be filtered in some way automatically?
 
Last edited:
Upvote 0
So i have a master spread sheet which has all my info in it. But i want to be able to go through and select certain rows and only specific columns in that row to transfer to a new spreadsheet.

So the code was just for the first row and i need to apply it to all rows in the master. So when I click on the box only that information will go onto the new spreadsheet
 
Upvote 0
Here's one way you might do it:

Excel 2010
ABCDEFGHI
1Selectxxx
2Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8
3x12345678
41112131415161718
5x2122232425262728
63132333435363738
7x4142434445464748
85152535455565758

<tbody>
</tbody>
Sheet1

--->

Excel 2010
ABC
1Header 3Header 4Header 6
2346
3232426
4434446

<tbody>
</tbody>
Sheet2

using code:

Code:
Sub CopyRowsAndCols()

    Dim rng As Range, rngRows As Range, rngCols As Range
    Dim i As Long, lRows As Long, lCols As Long
    
    Set rng = Range("B2:I8") 'Should use range names or dynamic references
    Set rngRows = rng.Rows(1)
    For i = 1 To rng.Rows.Count
        If Sheets("Sheet1").Range("A2:A8")(i).Value = "x" Then
            Set rngRows = Union(rngRows, rng.Rows(i))
            lRows = lRows + 1
        End If
    Next i
    For i = 1 To rng.Columns.Count
        If Sheets("Sheet1").Range("B1:I1")(i).Value = "x" Then
            lCols = lCols + 1
            If rngCols Is Nothing Then
                Set rngCols = rng.Columns(i)
            Else
                Set rngCols = Union(rngCols, rng.Columns(i))
            End If
        End If
    Next i
    
    If lRows = 0 Or lCols = 0 Then
        MsgBox "Please specify at least one row/column"
    Else
        On Error Resume Next
            Range("MyCopiedRange").Clear
        On Error GoTo 0
        With Sheets("Sheet2")
            Intersect(rngRows, rngCols).Copy Destination:=.Range("A1")
            .Range("A1").CurrentRegion.Name = "MyCopiedRange"
        End With
    End If
    
End Sub

Someone else can probably suggest a more succinct way to do this?
 
Upvote 0
A non looping solution:

Code:
Sub Succint()
Dim lr%, r As Range
Range("a:a").AutoFilter
Set r = Range("b2").CurrentRegion
lr = Range("b" & Rows.Count).End(xlUp).Row
Range("a1:a" & lr).AutoFilter 1, "x", xlFilterValues
r.SpecialCells(xlCellTypeVisible).Copy
Range("a" & lr + 3).PasteSpecial xlPasteValues, xlNone, False, True ' transpose
Range("a:a").AutoFilter
Range("a" & lr + 3).CurrentRegion.AutoFilter 1, "x", xlFilterValues
Range("a" & lr + 3).CurrentRegion.SpecialCells(12).Copy
Sheets("Sheet1").Range("a15").PasteSpecial xlPasteValues, xlNone, False, True   ' transpose
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,070
Messages
6,128,612
Members
449,460
Latest member
jgharbawi

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