how to select rows depending on a cell value using VBA

dpmg1991

New Member
Joined
Feb 20, 2019
Messages
2
I am trying to copy and paste rows from one sheet (fill) of the workbook to another (BD). The rows I need to copy depend on the value of a cell. If such cell =3, I need 3 rows to be copied. If the cell =4 I need 4 rows copied to the other.
Is there a way of doing this with VBA?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How does this suit?

Code:
Sub copyrows()


Dim rownum As Long


rownum = Sheets("fill").Range("A1").Value  'cell containing the number of rows to copy


Sheets("BD").Rows("1:" & rownum).Copy Sheets("fill").Rows("1:1" & rownum)


End Sub
 
Upvote 0
You can use event code to have it automatically do the copy when you enter the number in a cell. Assume that the entry cell is on Sheet 'BD', cell B2 and the copy will begin on row three and be pasted to the next available row on Sheet 'fill'
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
    If Not Intersect(Target, Range("B2")) Is Nothing Then
        Rows(3).Resize(Target.Value).Copy Sheets("fill").Cells(Rows.Count, 1).End(xlUp)(2)
    End If
Application.EnableEvents = True
End Sub

The code should be copied to the worksheet code module of the sheet to be copied from that has the cell specifying the number or rows to copy.
 
Upvote 0
Try this. Change the data in red with your data

Code:
Sub Copy_Rows()
    Dim wRows As Variant
    Dim ws1  As Worksheet, ws2 As Worksheet
    Dim ini As Double
    
    Set ws1 = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")  'sourde sheet
    Set ws2 = Sheets("[COLOR=#ff0000]Sheet2[/COLOR]")  'target sheet
    ini = [COLOR=#ff0000]4[/COLOR]                     'initial row to copy
    wRows = ws1.Range("[COLOR=#ff0000]A3[/COLOR]").Value   'cell withrows number to copy
    
    If wRows = "" Or Not IsNumeric(wRows) Then
        MsgBox "Enter rows number to copy"
        Exit Sub
    End If
    
    ws1.Rows(ini & ":" & ini + wRows).Copy ws2.Range("[COLOR=#ff0000]A2[/COLOR]")  'target cell
    
    MsgBox "Rows copied"
End Sub
 
Upvote 0
I think I didnt explained myself...

I have two worksheets: Fill and BD
On the Fill worksheet I am filling information. Cell A3 is the sum of the rows that have values on them.
I want to copy only the number of rows that A3 has. So if A3 = 10, i want to select rows from A4 to A14 and paste them on the nex available row in the BD sheet.
 
Upvote 0
OK, then maybe you want to use a button instead of event code. You can attach this to a button.
Code:
Sub t()    
Sheets("fill").Rows(4).Resize(Range("A3").Value).Copy Sheets("BD").Cells(Rows.Count, 1).End(xlUp)(2)   
End Sub

BTW, your example in Post #5 would copy 11 rows, not 10.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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