I'm fairly new to VBA coding, or any kind of coding at all (had a brief experience on college, 4 or 5 years ago, but with C, html and whatever language Adobe Flash uses), so please, be as didactic as possible.
I've came here with this problem (thread name: Boolean to String on the same cell.) where I have a (huge) worksheet where I input data from paper. And there are lots of paper, so the faster i do it, more relevant things I can do with my working hours.
The data that I input is composed by really long numbers and some words that are standard like "SATISFACTORY", "DAMAGED", "N/A". What I want to happen is to never have to take my hand off the numpad when inputting these words. This really cool guy named Arithos made me some code using "select case" and it worked smoothly. The point is that now I want to go beyond. I want, in some columns (lets say "E:E"), 1=no and 2=yes, and on "A:D" columns i want 1, 2 and 3 respectively as "SATISFACTORY", "DAMAGED", "N/A".
The troubles i'm having are the following:
If i try to breake the code into 2 subs with "select case", it says to me that the name "Worksheet_Change" is a repeated name. If i change one of the subs name second one wont work.
If i try to mesh them in one sub the same thing happens, it will run only first one. I tryed moving the firs "end if" close to second one, so it'd interpret the second if as part of the first else, but it says i have an "if" without end.
I tried another way, with the code that macro recorder generates when i use find and replace, but I'm stuck with the selection. I dont want to select the columns on the last replace every time i change a cell on my worksheet. I'm realy new to all of this, so i still dont know how to make the active cell go to where it first began. below is my tryout. it says that the method range of the object worksheet has failed. it highlights the "Set previous_range = Range(ActiveCell)" . i tryed adding "me." in front of range but still the same message.
I know it's asking too much, but I'm totally into this now, and i really want to learn, if you could comment on the code so i can get a clue of what's going on i'd immensely appreciate. I'm new to VBA but get the basics.
Like... I know that "Worksheet_Change" (created from drop-down menu) triggers whenever a cell changes value on said worksheet, but the reason of it is the "(ByVal Target As Range)"???
I think that "If Intersect(Target, Range("F:J, M:M, W:W, Y:Y, AE:AE, AI:AI, AL:AL")) Is Nothing" means to end the sub if the changed cell (target, am i right?) is outside of the range, but what's the purpose of "Target.Cells.Count > 1" as one of the condition to end sub???
And could someone, pretty please with a cherry on top, explain to me better how the "select case" function works in this particular case?? I get (at least believe) "case" will state conditions for a decision, but from there i cant figure out the structure "select>case>target".
I thankful in advance, and i bet all my doubts seems like retarded stuff for experienced programmers, so i apologize for it and any english mistakes i might have committed.
Regards.
I've came here with this problem (thread name: Boolean to String on the same cell.) where I have a (huge) worksheet where I input data from paper. And there are lots of paper, so the faster i do it, more relevant things I can do with my working hours.
The data that I input is composed by really long numbers and some words that are standard like "SATISFACTORY", "DAMAGED", "N/A". What I want to happen is to never have to take my hand off the numpad when inputting these words. This really cool guy named Arithos made me some code using "select case" and it worked smoothly. The point is that now I want to go beyond. I want, in some columns (lets say "E:E"), 1=no and 2=yes, and on "A:D" columns i want 1, 2 and 3 respectively as "SATISFACTORY", "DAMAGED", "N/A".
The troubles i'm having are the following:
If i try to breake the code into 2 subs with "select case", it says to me that the name "Worksheet_Change" is a repeated name. If i change one of the subs name second one wont work.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Err
If Intersect(Target, Range("A:D")) Is Nothing Or Target.Cells.Count > 1 Then
Exit Sub
Else
Select Case Target
Case Is = 1
Target.Value = "SATISFACTORY"
Case Is = 2
Target.Value = "DAMAGED"
Case Is = 3
Target.Value = "N/A"
End Select
End If
Err:
End Sub
Private Sub Worksheet_Change1(ByVal Target As Range)
On Error GoTo Err
If Intersect(Target, Range("E:E")) Is Nothing Or Target.Cells.Count > 1 Then
Exit Sub
Else
Select Case Target
Case Is = 1
Target.Value = "NO"
Case Is = 2
Target.Value = "YES"
End Select
End If
Err:
End Sub
If i try to mesh them in one sub the same thing happens, it will run only first one. I tryed moving the firs "end if" close to second one, so it'd interpret the second if as part of the first else, but it says i have an "if" without end.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Err
If Intersect(Target, Range("A:D")) Is Nothing Or Target.Cells.Count > 1 Then
Exit Sub
Else
Select Case Target
Case Is = 1
Target.Value = "SATISFACTORY"
Case Is = 2
Target.Value = "DAMAGED"
Case Is = 3
Target.Value = "N/A"
End Select
End If
If Intersect(Target, Range("E:E")) Is Nothing Or Target.Cells.Count > 1 Then
Exit Sub
Else
Select Case Target
Case Is = 1
Target.Value = "YES"
Case Is = 2
Target.Value = "NO"
End Select
End If
Err:
End Sub
I tried another way, with the code that macro recorder generates when i use find and replace, but I'm stuck with the selection. I dont want to select the columns on the last replace every time i change a cell on my worksheet. I'm realy new to all of this, so i still dont know how to make the active cell go to where it first began. below is my tryout. it says that the method range of the object worksheet has failed. it highlights the "Set previous_range = Range(ActiveCell)" . i tryed adding "me." in front of range but still the same message.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim previous_range As Range
Set previous_range = Range(ActiveCell)
Columns("A:D").Select
Selection.Replace What:="1", Replacement:="SATISFACTORY"
Selection.Replace What:="2", Replacement:="DANIFIED"
Selection.Replace What:="3", Replacement:="N/A"
Columns("E:E").Select
Selection.Replace What:="1", Replacement:="NO"
Selection.Replace What:="2", Replacement:="YES"
previous_range.Select
End Sub
I know it's asking too much, but I'm totally into this now, and i really want to learn, if you could comment on the code so i can get a clue of what's going on i'd immensely appreciate. I'm new to VBA but get the basics.
Like... I know that "Worksheet_Change" (created from drop-down menu) triggers whenever a cell changes value on said worksheet, but the reason of it is the "(ByVal Target As Range)"???
I think that "If Intersect(Target, Range("F:J, M:M, W:W, Y:Y, AE:AE, AI:AI, AL:AL")) Is Nothing" means to end the sub if the changed cell (target, am i right?) is outside of the range, but what's the purpose of "Target.Cells.Count > 1" as one of the condition to end sub???
And could someone, pretty please with a cherry on top, explain to me better how the "select case" function works in this particular case?? I get (at least believe) "case" will state conditions for a decision, but from there i cant figure out the structure "select>case>target".
I thankful in advance, and i bet all my doubts seems like retarded stuff for experienced programmers, so i apologize for it and any english mistakes i might have committed.
Regards.