Hi, i am new to VBA and i tried to write a code a below for me to automated a reporting task.
Is there anyway to simpilify the code, as i feel like its too lengthy and immature.
Is there anyway to simpilify the code, as i feel like its too lengthy and immature.
VBA Code:
Sub working()
'populate the month based on the reporting period
Dim reportprd As String
Set dessh1 = ThisWorkbook.Sheets("Part 1")
Set dessh2 = ThisWorkbook.Sheets("Part 2")
Set dessh3 = ThisWorkbook.Sheets("Part 3")
reportprd = ThisWorkbook.Sheets("Main").Range("I10").Value
If reportprd = "1H" Then
With dessh1
.Range("B1").MergeArea.Value = "Jan"
.Range("E1").MergeArea.Value = "Feb"
.Range("H1").MergeArea.Value = "Mar"
.Range("K1").MergeArea.Value = "Apr"
.Range("N1").MergeArea.Value = "May"
.Range("Q1").MergeArea.Value = "Jun"
End With
With dessh2
.Range("B1").MergeArea.Value = "Jan"
.Range("E1").MergeArea.Value = "Feb"
.Range("H1").MergeArea.Value = "Mar"
.Range("K1").MergeArea.Value = "Apr"
.Range("N1").MergeArea.Value = "May"
.Range("Q1").MergeArea.Value = "Jun"
End With
With dessh3
.Range("A5").Value = "Jan"
.Range("A6").Value = "Feb"
.Range("A7").Value = "Mar"
.Range("A8").Value = "Apr"
.Range("A9").Value = "May"
.Range("A10").Value = "Jun"
.Range("A16").Value = "Jan"
.Range("A17").Value = "Feb"
.Range("A18").Value = "Mar"
.Range("A19").Value = "Apr"
.Range("A20").Value = "May"
.Range("A21").Value = "Jun"
.Range("A27").Value = "Jan"
.Range("A28").Value = "Feb"
.Range("A29").Value = "Mar"
.Range("A30").Value = "Apr"
.Range("A31").Value = "May"
.Range("A32").Value = "Jun"
End With
Else
With dessh1
.Range("B1").MergeArea.Value = "Jul"
.Range("E1").MergeArea.Value = "Aug"
.Range("H1").MergeArea.Value = "Sep"
.Range("K1").MergeArea.Value = "Oct"
.Range("N1").MergeArea.Value = "Nov"
.Range("Q1").MergeArea.Value = "Dec"
End With
With dessh2
.Range("B1").MergeArea.Value = "Jul"
.Range("E1").MergeArea.Value = "Aug"
.Range("H1").MergeArea.Value = "Sep"
.Range("K1").MergeArea.Value = "Oct"
.Range("N1").MergeArea.Value = "Nov"
.Range("Q1").MergeArea.Value = "Dec"
End With
With dessh3
.Range("A5").Value = "Jul"
.Range("A6").Value = "Aug"
.Range("A7").Value = "Sep"
.Range("A8").Value = "Oct"
.Range("A9").Value = "Nov"
.Range("A10").Value = "Dec"
.Range("A16").Value = "Jul"
.Range("A17").Value = "Aug"
.Range("A18").Value = "Sep"
.Range("A19").Value = "Oct"
.Range("A20").Value = "Nov"
.Range("A21").Value = "Dec"
.Range("A27").Value = "Jul"
.Range("A28").Value = "Aug"
.Range("A29").Value = "Sep"
.Range("A30").Value = "Oct"
.Range("A31").Value = "Nov"
.Range("A32").Value = "Dec"
End With
End If
End Sub