I'm new here as well as new to vba. Im trying to copy the row based on selected columns.
I have used useform to get ranges from users to copy details of those data to next sheet.
I have written code to get the address of the range ( which is entered on userfrom)
Now when i try to copy the row, its just copying the S.no without any other details.
Sub copy_data()
If pass1 = 1 Then
Sheets("I yr").Range(cellFrom1 & ":" & cellTo1).Copy Destination:=Sheets("final").Range("B8:B37")
End If
If pass2 = 1 Then
Sheets("II yr").Range(cellFrom2 & ":" & cellTo2).Copy Destination:=Sheets("final").Range("B8:B37")
End If
If pass3 = 1 Then
Sheets("III yr").Range(cellFrom3 & ":" & cellTo3).Copy Destination:=Sheets("final").Range("B8:B37")
End If
If pass4 = 1 Then
Sheets("IV yr").Range(cellFrom4 & ":" & cellTo4).Copy Destination:=Sheets("final").Range("B8:B37")
End If
End Sub
here
cellFrom1 = Sheets("I yr").Range("N4").Value //N4 will have from address of the s.no selected
cellTo1 = Sheets("I yr").Range("O4").Value //O4 will to address of the s.no selected
I tried this code, but it's just copying the S.No ,not with that respected details
the data format will be like
S.no | regNo | Name | Year |
---|---|---|---|
1 | 921 | aaa | II |
2 | 922 | bbb | II |
3 | 923 | ccc | II |
4 | 924 | ddd | II |
5 | 925 | eee | II |
... |
If i giving the range from 3 -5 it should copy the data like
S.no | Reg No | Name | Year |
---|---|---|---|
3 | 923 | ccc | II |
4 | 924 | ddd | II |
5 | 925 | eee | II |
CodePudding user response:
If you're trying to copy $A$2
to $A$20
, you're only going to get column A
.
Ranges need to be $A$2
to $D$20
- is that your issue?
You're also pasting into only one column (column B
).
Try pasting into B8:E37
as an example (or just specify the starting area for paste like Range("B8")
)
Here is how your code would look updated.
It's very important that your cellFrom1
and cellTo1
contains the right column. Example: cellFrom1 = $A$1
and cellTo1 = $D$4
Sub copy_data()
If pass1 = 1 Then
Sheets("I yr").Range(cellFrom1 & ":" & cellTo1).Copy Destination:=Sheets("final").Range("B8")
End If
If pass2 = 1 Then
Sheets("II yr").Range(cellFrom2 & ":" & cellTo2).Copy Destination:=Sheets("final").Range("B8")
End If
If pass3 = 1 Then
Sheets("III yr").Range(cellFrom3 & ":" & cellTo3).Copy Destination:=Sheets("final").Range("B8")
End If
If pass4 = 1 Then
Sheets("IV yr").Range(cellFrom4 & ":" & cellTo4).Copy Destination:=Sheets("final").Range("B8")
End If
End Sub