jannahbuang
New Member
- Joined
- Jul 3, 2014
- Messages
- 8
Hi all.
Can someone explain me the code i provided. I got this code from somewhere.
Unfortunately, i'm still new in VBA and this code does not have comment.
Thanks
Sub EF1022331()
Dim wsData As Worksheet
Dim wsTarg As Worksheet
Dim lngRC As Long
Dim var As Variant
Dim lngMonth As Long
Const cstrDAT_COL As String = "C"
Const cstrTAR_COL As String = "B"
Const clngSTART As Long = 4
Const clngYEAR_START As Long = 2
Set wsData = Sheets("Sheet1")
Set wsTarg = Sheets("Sheet2")
For lngRC = clngSTART To wsData.Cells(Rows.Count, cstrDAT_COL).End(xlUp).Row
lngMonth = Month(wsData.Cells(lngRC, cstrDAT_COL).Value)
var = Application.Match(wsData.Cells(lngRC, cstrDAT_COL).Offset(0, 1).Value, wsTarg.Range(cstrTAR_COL & ":" & cstrTAR_COL), 0)
If IsError(var) Then
With wsTarg.Cells(Rows.Count, cstrDAT_COL).End(xlUp).Offset(1, 0)
.Value = wsData.Cells(lngRC, cstrDAT_COL).Offset(0, 1).Value
.Offset(0, lngMonth).Value = wsData.Cells(lngRC, cstrDAT_COL).Value
End With
Else
Do Until IsEmpty(wsTarg.Cells(var, clngYEAR_START + lngMonth))
If wsTarg.Cells(var, cstrTAR_COL) <> wsTarg.Cells(var + 1, cstrTAR_COL) Then
wsTarg.Rows(var + 1).Insert
wsTarg.Rows(var + 1).ClearFormats
wsTarg.Cells(var + 1, cstrTAR_COL) = wsTarg.Cells(var, cstrTAR_COL)
End If
var = var + 1
Loop
With wsTarg.Cells(var, clngYEAR_START + lngMonth)
.Value = wsData.Cells(lngRC, cstrDAT_COL).Offset(0, 2).Value
.Interior.ColorIndex = 3
End With
End If
Next lngRC
Set wsTarg = Nothing
Set wsData = Nothing
End Sub
Can someone explain me the code i provided. I got this code from somewhere.
Unfortunately, i'm still new in VBA and this code does not have comment.
Thanks
Sub EF1022331()
Dim wsData As Worksheet
Dim wsTarg As Worksheet
Dim lngRC As Long
Dim var As Variant
Dim lngMonth As Long
Const cstrDAT_COL As String = "C"
Const cstrTAR_COL As String = "B"
Const clngSTART As Long = 4
Const clngYEAR_START As Long = 2
Set wsData = Sheets("Sheet1")
Set wsTarg = Sheets("Sheet2")
For lngRC = clngSTART To wsData.Cells(Rows.Count, cstrDAT_COL).End(xlUp).Row
lngMonth = Month(wsData.Cells(lngRC, cstrDAT_COL).Value)
var = Application.Match(wsData.Cells(lngRC, cstrDAT_COL).Offset(0, 1).Value, wsTarg.Range(cstrTAR_COL & ":" & cstrTAR_COL), 0)
If IsError(var) Then
With wsTarg.Cells(Rows.Count, cstrDAT_COL).End(xlUp).Offset(1, 0)
.Value = wsData.Cells(lngRC, cstrDAT_COL).Offset(0, 1).Value
.Offset(0, lngMonth).Value = wsData.Cells(lngRC, cstrDAT_COL).Value
End With
Else
Do Until IsEmpty(wsTarg.Cells(var, clngYEAR_START + lngMonth))
If wsTarg.Cells(var, cstrTAR_COL) <> wsTarg.Cells(var + 1, cstrTAR_COL) Then
wsTarg.Rows(var + 1).Insert
wsTarg.Rows(var + 1).ClearFormats
wsTarg.Cells(var + 1, cstrTAR_COL) = wsTarg.Cells(var, cstrTAR_COL)
End If
var = var + 1
Loop
With wsTarg.Cells(var, clngYEAR_START + lngMonth)
.Value = wsData.Cells(lngRC, cstrDAT_COL).Offset(0, 2).Value
.Interior.ColorIndex = 3
End With
End If
Next lngRC
Set wsTarg = Nothing
Set wsData = Nothing
End Sub
Last edited: