Results 1 to 20 of 20

Thread: [Resolved]Problem with VBA in Excel

  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

  2. #2

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

    Re: Problem with VBA in Excel

    Does anyone have any suggestions?

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    Why are yo using the Worksheet_SelectionChange? IF you are using a combobox then you shoul;d have your code triggered by the _Change event of the combo.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: Problem with VBA in Excel

    It is a list generated through data validation.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    OK, that makes a bit more sense - guess I could have looked at the attachment.

    You still should not be using the _SelectionChange event for this. You would be better off using the _Change event and only executing the code if the Target cell is in Column "I" which has the Dropdown validation list.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

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

    Re: Problem with VBA in Excel

    The reason I am using _SelectionChange is because i only want it to run through the code when one of the cells is changed (J). If i use _Change it runs through on every cell change.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    If i use _Change it runs through on every cell change.
    Not true.

    Here's an example of how you can test to see if the cell being changed is in Column I and only continue if that condition is true.
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.    
    3.     'Check if the cell being changed is in Column I
    4.     'If not then exit
    5.     If Target.Column <> 9 Then Exit Sub
    6.    
    7.     'rest of code here
    8.    
    9. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

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

    Re: Problem with VBA in Excel

    i tried putting in your suggestion and the problem became worse. it is copying every cell when it is changed even with the if target.column statement in the code. have you tried running my code to see what it is doing?

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    We need to take several steps back here. I'm a little confused about what the requirement is here. I know you want to copy cells from sheet1 to sheet2 but I have some questions.

    You state that you need to copy 4 of the fields, but I have no idea which 4?

    You have lots of looping through evey cell in column I and column J. Do you need to copy all rows every time a single row is changed or only the row that was changed?


    What is this piece of code supposed to achieve?
    VB Code:
    1. If sourceRange(i) < #11/1/2005# Then
    2.             targetRange(x) = "No"
    3.             x = x + 1
    4.         End If
    5.         If sourceRange(i) > #11/1/2005# Then
    6.             targetRange(x) = "Yes"
    7.             x = x + 1
    8.         End If


    In this procedure your comment has no relationship to the code, no where do you check the values in A11:C11. What is the intent here?
    VB Code:
    1. Sub KeyCellsChanged()
    2.    Dim Cell As Object
    3.    ' If the values in A11:C11 are greater than 50...
    4.    For Each Cell In Range("I2:I65000")
    5.    If Cell = "New" Then
    6.     CopyCellsValues
    7.  
    8.    End If
    9.    Next Cell
    10.  
    11. End Sub

    So, right now I have no idea what the real purpose is. If you can give an explaination of what the true requirements are, we will be able to get you the code you really need.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

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

    Re: Problem with VBA in Excel

    i need to copy the 1st 4 cells in sheet one. they only need to copy when the active row is changed. this is my first excel project. i usually deal with Access or VB6. thanks for all the help.

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    OK, that's a start. Here's some code that will copy the first 4 cells from the active line on sheet1 to the next available line on sheet2 - but only when the value in column I = "New".
    Remove, or comment out all your existing code. Add this event procedure and test the result. I'm sure we will have to tweak this a little, but play with it and come back with your questions/comments We will continue form there.
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Dim lSourceRowNum As Long
    3. Dim lDestRowNum As Long
    4. Dim rngCopy As Range
    5.  
    6.     'Check if the cell being changed is in Column I
    7.     If Target.Column = 9 Then
    8.        
    9.         'Has the cell been changed to the value "New"?
    10.         If Target.Value = "New" Then
    11.            
    12.             'Deterime what row we are on
    13.             lSourceRowNum = Target.Row
    14.            
    15.             'Copy the first 4 cells in the current row
    16.             With ActiveSheet
    17.                 .Range(.Cells(lSourceRowNum, 1), .Cells(lSourceRowNum, 4)).Copy
    18.             End With
    19.            
    20.             'Determine our Destination Row
    21.             lDestRowNum = Worksheets("Sheet2").Range("A65536").End(xlUp).Row + 1
    22.            
    23.             'Paste to the next row on Sheet2
    24.             With Worksheets("Sheet2")
    25.                 .Range(.Cells(lDestRowNum, 1), .Cells(lDestRowNum, 4)).PasteSpecial
    26.             End With
    27.            
    28.             'Clear the clipboard
    29.             Application.CutCopyMode = False
    30.            
    31.         End If
    32.     End If
    33. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12

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

    Re: Problem with VBA in Excel

    That works perfect. Now I need to get all my other stuff back in (changing column F depending on what date is in column E and filling in column J).

    Thank you so much.

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    No worries, glad to help.

    Here are some suggestions

    If there is anywhere in the code that you need to make changes to Sheet1 make sure that you Disable Events before you make the changes and reenable that after the change. this will prevent the _Change event from calling itself which can be a very bad thing indeed.

    For filling in Column J - you may want to consider using a SELECT CASE statement based on Target.Value. Have a look at the help on SEELCT CASE, and come back with any questions.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

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

    Re: Problem with VBA in Excel

    I have everything working except 1 more thing. I keep overwriting the line on sheet 2. It is not advancing to the next available line.

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    Hummm, this piece of code should always give you the next unused cell in Column A. It is working fine on my machine. Are you coyping into Cols A-D on sheet2?
    VB Code:
    1. 'Determine our Destination Row
    2.             lDestRowNum = Worksheets("Sheet2").Range("A65536").End(xlUp).Row + 1
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

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

    Re: Problem with VBA in Excel

    It is asking if I want to replace the contents of the cells. If I choose yes it overwrites, if I choose no it errors and stops.

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    Please answer this question
    Are you copying into Cols A-D on sheet2?

    If the answer is yes then there's something else going on in your code. If the answer is no then change .Range("A65536") so that it references the first column that you are copying into.

    Can you post the _Change event macro that you currently have. I need to compare it to my original.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

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

    Re: Problem with VBA in Excel

    I am copying into A - E on sheet 2.

    Here is the code:

    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Dim lSourceRowNum As Long
    3. Dim lDestRowNum As Long
    4. Dim rngCopy As Range
    5.    
    6.     'Check if the cell being changed is in Column I
    7.     If Target.Column = 9 Then
    8.        
    9.         'Has the cell been changed to the value "New"?
    10.         If Target.Value = "New" Then
    11.            
    12.             'Deterime what row we are on
    13.             lSourceRowNum = Target.Row
    14.            
    15.             'Copy the first 4 cells in the current row
    16.             With ActiveSheet
    17.                 .Range(.Cells(lSourceRowNum, 1), .Cells(lSourceRowNum, 5)).Copy
    18.             End With
    19.            
    20.             'Determine our Destination Row
    21.             lDestRowNum = Worksheets("Sheet2").Range("A60000").End(xlUp).Row + 1
    22.            
    23.             'Paste to the next row on Sheet2
    24.             With Worksheets("Sheet2")
    25.                 .Range(.Cells(lDestRowNum, 1), .Cells(lDestRowNum, 4)).PasteSpecial
    26.             End With
    27.            
    28.             'Clear the clipboard
    29.             Application.CutCopyMode = False
    30.            
    31.         End If
    32.     End If
    33.    
    34. End Sub

  19. #19
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VBA in Excel

    OK, this may seem like a dumb question, but...
    Do you have any data in columns B through D on the row(s) that are directly below the last row on sheet2 where you have a value in Column A?

    Also, do the following
    1/ Write down the last row number used on sheet2.
    2/ Insert a breakpoint on the following line
    VB Code:
    1. .Range(.Cells(lSourceRowNum, 1), .Cells(lSourceRowNum, 5)).Copy
    3/ Add a row in sheet one and make the value in column i = 'New".
    4/ when the code pauses, get the value of lDestRowNum from either the imediates or watch windows.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  20. #20

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

    Resolved Resolved: Problem with VBA in Excel

    I am not sure what happened, but when I put in the breaks and ran the code it worked and has worked ever since. Thank you so much for all the help.

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