What Does This Code Do?

L

Lil Pun

Can somebody explain what this section of code does? It is located i
Sheet1 of the VBA Explorer in Excel.

Here it is:


On Error GoTo errorhandler
If avoidloop And Trim(TARGET) <> "" Then
If TARGET = "1" Then
Range("C2").Select
Application.SendKeys "{F2}"
Else
Select Case (ActiveCell.Column)
Case 1
avoidloop = False
If ActiveSheet.Rows(2).Columns(1).Value
TARGET Then
ActiveSheet.Rows(ActiveCell.Row - 1).Columns(1).Value
TARGET
ActiveSheet.Rows(ActiveCell.Row - 1).Columns(2).Value
""
avoidloop = True
Else
ActiveSheet.Rows(ActiveCell.Row - 1).Columns(2).Value
TARGET
ActiveSheet.Rows(ActiveCell.Row - 1).Columns(1).Value
""
avoidloop = True
End If
Case 2
Case 3
If TARGET <> "" Then SAVE_DATA (TARGET)
Case Else
End Select
End If
End If
errorhandler:
End Su
 
L

Lil Pun

All of it actually, the rest of the code in this file is commented so I
think if I understand what this part means I will understand how it
works with the rest of the other code in the project. I can post that
as well if needed or wanted. This code did have some commenting at the
top of the page which I will post at the end of this post. But oall the
other commenting basically goes line for line and these comments were at
the top of the page. Here are the comments:

' This routine is activated each time data from a cell is captured
' It needs to evaluate what changed and where it changed
' If it was in column A, B, C or any other; each column has a differen
process.
' Since this routine also changes data from others cells it is possible
that
' this routine calls itself. That is why the avoidloop is been used.
' When the routine knows that it is going to call itself it changes
this variable
' so that the complete routine can be jumped and ignored since it is
known what the data is
' and where it going to be placed.

And there was some code above those comments that have no comments:

Private Sub Worksheet_Activate()
avoidloop = True
End Sub

Private Sub Worksheet_Change(ByVal TARGET As Range)


I am just wanting to see how it all works together.
 
G

Guest

It is what does the things you said the application does.

If avoidloop And Trim(TARGET) <> "" Then

first, it checks if AvoidLoop is True and the cell that triggered the code
(target) is not empty. If those cases check out then it checks:


If TARGET = "1" Then
Range("C2").Select
Application.SendKeys "{F2}"

Target is the cell that triggered the macro. If it has a value of 1 then
select cell C2 and go into edit mode.

if Target doesn't equal 1 then you choose from 3 possibilities




If the target is in column 1 then
if the target cell equals the value in A2 then
then in the row above the activecell
column A is set to the target value
column B is set to ""
else target doesn't equal A2 then
then in the row above the activecell
column A is set to ""
column B is set to the Target Value
end if


In each case, AvoidLoop is set to true

The second case is that the target is in column B
in that case, do nothing



the third case is if target is in column C
in that case, if Target is not empty, then executed SAVEDATA passing a
Target as a reference. (We wouldn't be here if Target were empty, so this
check is redundant).

If Target is not in columns A, B or C, then do nothing.
 
L

Lil Pun

OK, I think I am slowly but surely understanding the code but what do
those errorhandler variables do? As I said there are various pieces of
this file that I don't understand because it is not commented. I will
post all of the code in the file on this post.

Here is another piece of code, located in the Excel object
ThisWorkbook, what does this do:
 
G

Guest

The error handler just jumps to the end of the sub if there is an error
rather than stopping the code and putting up an error message.
 
T

Tim Williams

What "above code"?

Please *quote* relevant content and remember we're not all using "web forums" (yuk!).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top