Explain the code.

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
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Let me ask you another question what would you like your macro to do? you can't start randomly copying code if you have no clue what it does.
 
Upvote 0
It will highlight cell with the condition below by using vba.

1)if 'process' in sheet 1 is equal/contain with the process in sheet 2, copy 'title' to the empty cell according to months
2)after insert to suitable cell, the cell will be highlighted with red colour
3) if the cell already full, insert new cell below the existing one.

something like that.

 
Upvote 0

Forum statistics

Threads
1,223,738
Messages
6,174,209
Members
452,551
Latest member
croud

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