Results 1 to 20 of 20

Thread: [Resolved]Problem with VBA in Excel

Threaded View

  1. #1

    Thread Starter
    Junior Member jmcgill's Avatar
    Join Date
    Jul 2005
    Posts
    22

    Resolved [Resolved]Problem with VBA in Excel

    I am putting together a spreadsheet for my company. Their are 2 sheets in the workbook. If "New" is chosen from the combo box in column I then I am copying 4 of the fields already entered in sheet one to the corresponding fields on sheet 2. The problem i am having is that it is copying the data 4 times. I can not figure out why this is happening. If anyone can tell what is going on I would greatly appreciate it. Below is the code. File is attached.

    Thanks,

    Jason

    VB Code:
    1. Sub Worksheet_SelectionChange(ByVal Target As Range)
    2. Dim topCel As Range, bottomCel As Range, _
    3.     sourceRange As Range, targetRange As Range
    4. Dim x As Integer, i As Integer, numofRows As Integer
    5. Set topCel = Range("I2")
    6. Set bottomCel = Range("I65536").End(xlUp)
    7. If topCel.Row > bottomCel.Row Then End     ' test if source range is empty
    8. Set sourceRange = Range(topCel, bottomCel)
    9. Set targetRange = Range("J2")
    10. numofRows = sourceRange.Rows.Count
    11. x = 1
    12. For i = 1 To numofRows
    13.    
    14.         If sourceRange(i) = "As Is" Then
    15.             targetRange(x) = "No Action Needed"
    16.             x = x + 1
    17.         End If
    18.         If sourceRange(i) = "Group Owned" Then
    19.             targetRange(x) = "No Action Needed"
    20.             x = x + 1
    21.         End If
    22.         If sourceRange(i) = "New" Then
    23.             targetRange(x) = "Cells Copied to Sheet2"
    24.             DidCellsChange
    25.             x = x + 1
    26.            
    27.         End If
    28.         If sourceRange(i) = "Assign To" Then
    29.             targetRange(x) = "Cells Copied to Sheet2"
    30.             x = x + 1
    31.         End If
    32.         If sourceRange(i) = "" Then
    33.             targetRange(x) = ""
    34.             x = x + 1
    35.         End If
    36.        
    37.    
    38. Next
    39. Set topCel = Range("E2")
    40. Set bottomCel = Range("E65536").End(xlUp)
    41. If topCel.Row > bottomCel.Row Then End     ' test if source range is empty
    42. Set sourceRange = Range(topCel, bottomCel)
    43. Set targetRange = Range("F2")
    44. numofRows = sourceRange.Rows.Count
    45. x = 1
    46. For i = 1 To numofRows
    47.    
    48.         If sourceRange(i) < #11/1/2005# Then
    49.             targetRange(x) = "No"
    50.             x = x + 1
    51.         End If
    52.         If sourceRange(i) > #11/1/2005# Then
    53.             targetRange(x) = "Yes"
    54.             x = x + 1
    55.         End If
    56.    
    57. Next
    58.  
    59. End Sub
    60. Sub CopyCellsValues()
    61.     Dim sourceRange As Range
    62.     Dim destrange As Range
    63.     Dim Lr As Long
    64.     Lr = LastRow(Sheets("Sheet2")) + 1
    65.     Set sourceRange = Sheets("Sheet1").Cells( _
    66.     ActiveCell.Row, 1).Range("A1:E1")
    67.     With sourceRange
    68.         Set destrange = Sheets("Sheet2").Range("A" _
    69.         & Lr).Resize(.Rows.Count, .Columns.Count)
    70.     End With
    71.     destrange.Value = sourceRange.Value
    72. End Sub
    73.  
    74.  
    75. Function LastRow(sh As Worksheet)
    76.     On Error Resume Next
    77.     LastRow = sh.Cells.Find(What:="*", _
    78.                             After:=sh.Range("A1"), _
    79.                             Lookat:=xlPart, _
    80.                             LookIn:=xlFormulas, _
    81.                             SearchOrder:=xlByRows, _
    82.                             SearchDirection:=xlPrevious, _
    83.                             MatchCase:=False).Row
    84.     On Error GoTo 0
    85. End Function
    86.  
    87. Sub DidCellsChange()
    88.   Dim KeyCells As String
    89.    ' Define which cells should trigger the KeyCellsChanged macro.
    90.    KeyCells = "J2:J65000"
    91.  
    92.    ' If the Activecell is one of the key cells, call the
    93.    ' KeyCellsChanged macro.
    94.    If Not Application.Intersect(ActiveCell, Range(KeyCells)) _
    95.    Is Nothing Then KeyCellsChanged
    96.  
    97. End Sub
    98.  
    99. Sub KeyCellsChanged()
    100.    Dim Cell As Object
    101.    ' If the values in A11:C11 are greater than 50...
    102.    For Each Cell In Range("I2:I65000")
    103.    If Cell = "New" Then
    104.     CopyCellsValues
    105.  
    106.    End If
    107.    Next Cell
    108.  
    109. End Sub
    Attached Files Attached Files
    Last edited by jmcgill; Apr 25th, 2006 at 04:58 PM. Reason: Attached File

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width