#1   Report Post  
Steven M. Britton
 
Posts: n/a
Default Columns Selected

I'm having some issues with trying to get this code to work properly. What
this is trying to do is record changes made to the columns containing the
Part Number, Qty and Unit Cost. I had it working fine if the user only
changed one cell at a time, I was able to pick up the row/column and produce
the change.

The issue is that if the user selects a range and deletes it or drag and
drop or xlFillDown, I can't figure out how to record it properly - however
I'm getting close. My main issue right now is I want to only build the array
if the range contains the columns I am interested in AND build the array of
just those columns. Example, if the user selects Cells.Select in the upper
left most corner of the spreadsheet the workbook goes "nuts" while it builds
an array of (1 to 65536, 1 to 256) this is bad...

How do I use the intersect or some other range type function to build and
array of the columns I'm interested in? That should send me into the right
direction I hope...

Thanks,

-Steven M. Britton

Here is the code:

Option Explicit

Public varPriorRange As Variant
Public varAddr As Variant
Public varStartColumn As Variant
Public varCount As Variant

Private Sub Worksheet_Change(ByVal Target As Range)

Dim strSheet As String
Dim strCellValue As String
Dim varTargetAddress As Variant
Dim varCurrentRange As Variant
Dim r As Long
Dim x As Long
Dim y As Long
Dim lngRow As Long
Dim lngCol As Long


Application.ScreenUpdating = False
strSheet = ActiveSheet.Name

If Target.Column = 17 Or Target.Column = 18 Or Target.Column = 19 Then

strCellValue = Worksheets("2005").Cells(2, 1).Value

If Target.Count = 1 Then
If Target.Count = 1 And varCount = 1 Then
varTargetAddress = Right(Target.Address,
InStr(StrReverse(Target.Address), "$") - 1)
varAddr = Right(varAddr, InStr(StrReverse(varAddr), "$") - 1)
Else
varTargetAddress = Right(Target.Address,
InStr(StrReverse(Target.Address), "$") - 1)
varAddr = Mid(varAddr, 4, InStr(1, varAddr, ":", vbTextCompare)
- 4)
End If

x = (varTargetAddress - varAddr) + 1
y = (Target.Column - varStartColumn) + 1

If IsNull(strCellValue) = True Or strCellValue = "" Then

Worksheets("2005").Cells(2, 1).Value = Application.UserName
Worksheets("2005").Cells(2, 2).Value = Date & " " & Time
Worksheets("2005").Cells(2, 3).Value = varPriorRange(x, y)
Worksheets("2005").Cells(2, 4).Value = Target.Text
Else

Sheets("2005").Select
ActiveSheet.Cells(1, 1).Select
Selection.End(xlDown).Select
r = ActiveCell.Row + 1

Worksheets("2005").Cells(r, 1).Value = Application.UserName
Worksheets("2005").Cells(r, 2).Value = Date & " " & Time
Worksheets("2005").Cells(r, 3).Value = varPriorRange(x, y)
Worksheets("2005").Cells(r, 4).Value = Target.Text

End If

Else
lngRow = UBound(varPriorRange, 1)
lngCol = UBound(varPriorRange, 2)

varCurrentRange = Target.FormulaR1C1

If IsNull(strCellValue) = True Or strCellValue = "" Then

r = 2

For x = 1 To lngRow
For y = 1 To lngCol
If varPriorRange(x, y) < "" Or varCurrentRange(x, y) <
"" Then
Worksheets("2005").Cells(r, 1).Value =
Application.UserName
Worksheets("2005").Cells(r, 2).Value = Date & " " & Time
Worksheets("2005").Cells(r, 3).Value = varPriorRange(x, y)
Worksheets("2005").Cells(r, 4).Value =
varCurrentRange(x, y)
r = r + 1
End If
Next
Next
Else

Sheets("2005").Select
ActiveSheet.Cells(1, 1).Select
Selection.End(xlDown).Select
r = ActiveCell.Row + 1

For x = 1 To lngRow
For y = 1 To lngCol
If varPriorRange(x, y) < "" Or varCurrentRange(x, y) <
"" Then
Worksheets("2005").Cells(r, 1).Value =
Application.UserName
Worksheets("2005").Cells(r, 2).Value = Date & " " & Time
Worksheets("2005").Cells(r, 3).Value = varPriorRange(x, y)
Worksheets("2005").Cells(r, 4).Value =
varCurrentRange(x, y)
r = r + 1
End If
Next
Next

End If

End If

End If

Sheets(strSheet).Select
Application.ScreenUpdating = True

End Sub

Public Sub Worksheet_SelectionChange(ByVal Target As Range)

varPriorRange = Target.FormulaR1C1
varAddr = Target.Address
varStartColumn = Target.Column
varCount = Target.Count

End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Columns FemIce Excel Discussion (Misc queries) 1 September 28th 05 09:29 AM
Perform oiperations relative to initial selected cell scratching my head Excel Discussion (Misc queries) 1 May 30th 05 05:42 PM
delete columns and rows-cells equalling zero or any selected value Scottie Excel Worksheet Functions 2 May 9th 05 08:47 PM
can't insert columns between columns smooth operator Excel Discussion (Misc queries) 1 May 1st 05 10:53 PM
How do I use @choose command to add selected columns Help with algorithm Excel Worksheet Functions 1 December 12th 04 10:01 PM


All times are GMT +1. The time now is 02:47 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"