BritsBlitz
New Member
- Joined
- Jan 10, 2014
- Messages
- 34
- Office Version
- 365
- Platform
- Windows
Hi. This question is purely for my own learning experience. I have the code below which works perfectly for what I want. I’m just interested in knowing if there’s a way to optimize the code even further and reduce the number of lines.
The code checks which of the three checkboxes I have listed on the “Setup” sheet are checked. It then copies the corresponding range for the checked boxes from the “TestPro” sheet and pastes it to the “TestPlan” sheet. After the paste, it moves to the cell where the next range will be pasted.
Private Sub Generate_Report()
Dim nbr As Long
Dim Rng(1 To 3) As String
Dim MvTo(1 To 3) As String
'Grounding Inspection
Rng(1) = ("A4:I14")
MvTo(1) = 11
'Equipment Inspection
Rng(2) = ("A15:I26")
MvTo(2) = 12
'Antenna System Test
Rng(3) = ("A29:I38")
MvTo(3) = 10
'CODE STARTS
Sheets("TestPlan").Activate
ActiveSheet.Cells(1, 1).Select
For nbr = 1 To 3
If Sheets("Setup").Cells(nbr, 3).Value = True Then
Sheets("TestPro").Range(Rng(nbr)).Copy
PasteValues
MoveToLine (MvTo(nbr)), 0
End If
Next nbr
End Sub
Sub PasteValues()
Sheets("TestPlan").Select
ActiveCell.PasteSpecial xlPasteColumnWidths
ActiveCell.PasteSpecial xlValues
ActiveCell.PasteSpecial xlFormats
Application.CutCopyMode = False
End Sub
Sub MoveToLine(line As Single, column As Single)
ActiveCell = ActiveCell.Offset(line, column).Select
End Sub
The code checks which of the three checkboxes I have listed on the “Setup” sheet are checked. It then copies the corresponding range for the checked boxes from the “TestPro” sheet and pastes it to the “TestPlan” sheet. After the paste, it moves to the cell where the next range will be pasted.
Private Sub Generate_Report()
Dim nbr As Long
Dim Rng(1 To 3) As String
Dim MvTo(1 To 3) As String
'Grounding Inspection
Rng(1) = ("A4:I14")
MvTo(1) = 11
'Equipment Inspection
Rng(2) = ("A15:I26")
MvTo(2) = 12
'Antenna System Test
Rng(3) = ("A29:I38")
MvTo(3) = 10
'CODE STARTS
Sheets("TestPlan").Activate
ActiveSheet.Cells(1, 1).Select
For nbr = 1 To 3
If Sheets("Setup").Cells(nbr, 3).Value = True Then
Sheets("TestPro").Range(Rng(nbr)).Copy
PasteValues
MoveToLine (MvTo(nbr)), 0
End If
Next nbr
End Sub
Sub PasteValues()
Sheets("TestPlan").Select
ActiveCell.PasteSpecial xlPasteColumnWidths
ActiveCell.PasteSpecial xlValues
ActiveCell.PasteSpecial xlFormats
Application.CutCopyMode = False
End Sub
Sub MoveToLine(line As Single, column As Single)
ActiveCell = ActiveCell.Offset(line, column).Select
End Sub