xancalagonx
Board Regular
- Joined
- Oct 31, 2011
- Messages
- 57
Fairly new to VBA coding, but loving it and it's really making life easier (at work at least).
I've made some rather simple code in a worksheet where I am using VBA to add Date and Application.Username to two columns whenever someone changes the information in 3 other columns.
This is the code I am using.
What I want is to use just 1 chunk of code to handle all three variables for the Ranges (column C, D and E), and the variables using Target from (1,3 to 1,6).
Since the Target changes depending on what column the user does a change in I ended up making 3 "copy/paste" pieces of code.
I was thinking maybe using Arrays I could break this code down to just one chunk of code, then have the variables instead of the Range and Target values?
Hope I explained it properly. I never used Arrays or multiple ranges before... at least not in same line of code so to speak.
The code works as it should right now, but now that it DOES work, I want to refine it
I was thinking something along the lines of using only this chunk of code, and then having the parts I bolded to change.
Hope someone can help as this would broaden my VBA knowledge alot as well.
I've made some rather simple code in a worksheet where I am using VBA to add Date and Application.Username to two columns whenever someone changes the information in 3 other columns.
This is the code I am using.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c2:c1000")) Is Nothing Then
With Target(1, 5)
.Value = Date
.EntireColumn.AutoFit
.Locked = True
End With
With Target(1, 6)
.Value = Application.UserName
.EntireColumn.AutoFit
.Locked = True
End With
ActiveSheet.Protect Password:="", userinterfaceonly:=True
End If
If Not Intersect(Target, Range("d2:d1000")) Is Nothing Then
With Target(1, 4)
.Value = Date
.EntireColumn.AutoFit
.Locked = True
End With
With Target(1, 5)
.Value = Application.UserName
.EntireColumn.AutoFit
.Locked = True
End With
ActiveSheet.Protect Password:="", userinterfaceonly:=True
End If
If Not Intersect(Target, Range("e2:e1000")) Is Nothing Then
With Target(1, 3)
.Value = Date
.EntireColumn.AutoFit
.Locked = True
End With
With Target(1, 4)
.Value = Application.UserName
.EntireColumn.AutoFit
.Locked = True
End With
ActiveSheet.Protect Password:="", userinterfaceonly:=True
End If
End Sub
What I want is to use just 1 chunk of code to handle all three variables for the Ranges (column C, D and E), and the variables using Target from (1,3 to 1,6).
Since the Target changes depending on what column the user does a change in I ended up making 3 "copy/paste" pieces of code.
I was thinking maybe using Arrays I could break this code down to just one chunk of code, then have the variables instead of the Range and Target values?
Hope I explained it properly. I never used Arrays or multiple ranges before... at least not in same line of code so to speak.
The code works as it should right now, but now that it DOES work, I want to refine it
I was thinking something along the lines of using only this chunk of code, and then having the parts I bolded to change.
Rich (BB code):
If Not Intersect(Target, Range("c2:c1000")) Is Nothing Then
With Target(1, 5)
.Value = Date
.EntireColumn.AutoFit
.Locked = True
End With
With Target(1, 6)
.Value = Application.UserName
.EntireColumn.AutoFit
.Locked = True
End With
ActiveSheet.Protect Password:="", userinterfaceonly:=True
End If
Hope someone can help as this would broaden my VBA knowledge alot as well.