+ Reply to Thread
Results 1 to 7 of 7

Can someone add comments to this code?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-19-2010
    Location
    Findlay Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Can someone add comments to this code?

    I need to edit this code and apply it to my workbook but I don't know enough about VBA yet to do it by myself.
    Here is the code-->
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$A$3" Or Target.Address = "$B$3" Then
    
    Application.ScreenUpdating = False
    
    lastrow = Sheet2.Range("C" & Sheet2.UsedRange.Rows.Count).End(xlUp).Row
        
    If lastrow > 2 Then     'added
    Sheet2.Range("C3:D" & lastrow).ClearContents
    End If                  'added
        
    matchMonth = Application.Match(Sheet2.Range("A3"), Sheet1.Range("3:3"), False)
    
    i = 3
    
    For r = 4 To Sheet1.UsedRange.Rows.Count
    
    If Sheet1.Range("C" & r) = Sheet2.Range("B3") Then
    Sheet2.Range("C" & i) = Sheet1.Range("B" & r)
    Sheet2.Range("D" & i) = Sheet1.Cells(r, matchMonth)
    i = i + 1
    
    End If
    
    Next r
    
    Application.ScreenUpdating = True
    
    End If
    
    End Sub


    Here is the Program--> http://www.excelforum.com/attachment...gdata_rev2.xls

    Basically it copies a column of part numbers and their volumes based on the Plant and Month that the user specifies in a drop down list the other worksheet. Any help or explanation is really appreciated! Thank you.
    Last edited by neokeelo; 06-15-2010 at 11:43 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Can someone add comments to this code?

    Quote Originally Posted by neokeelo
    Can someone add comments to this code?
    Can you add CODE tags around your VBA ? (per the Forum Rules)

  3. #3
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Can someone add comments to this code?

    I can't comment further until you have added code tags around your VBA, however:

    lastrow = Sheet2.Range("C" & Sheet2.UsedRange.Rows.Count).End(xlUp).Row
    should read

    lastrow = Sheet2.UsedRange.Rows.Count

  4. #4
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Can someone add comments to this code?

    I have attached a workbook with comments in the VBA. I hope this helps.
    Attached Files Attached Files
    Last edited by pb71; 06-15-2010 at 01:00 PM.

  5. #5
    Registered User
    Join Date
    05-19-2010
    Location
    Findlay Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Can someone add comments to this code?

    Thank you! I will try and use this and let you know how it goes.

  6. #6
    Registered User
    Join Date
    05-19-2010
    Location
    Findlay Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Can someone add comments to this code?

    On the workbook I am working with, There are 2 main worksheets from which I am doing the same thing in the example. Sheet 1 is called PLANNED and sheet 2 is called FINAL. so in the VBA code , everywhere that the code has Sheet1 and Sheet2 written I can just replace those with "Planned" and "FINAL" correct? Then all I need to do is replace the cells and rows that you defined to the related ones in my workbook? For example Cell A3 B 3 is now A5 B5. As long as I keep my cells and rows aligned in the code, the logic is the same.

  7. #7
    Forum Contributor
    Join Date
    05-16-2010
    Location
    UK
    MS-Off Ver
    2003 and 2007
    Posts
    252

    Re: Can someone add comments to this code?

    In VBAProject you will see a list of Microsoft Excel Objects e.g. Sheet1 (Sheet 1). The text inside the bracket is the worksheet name i.e. what you have called it. So as long as you see:

    Sheet1 (PLANNED) and Sheet2 (FINAL) you will not have to change Sheet1 and Sheet2 in the VBA code.

    Otherwise replace Sheet1 with Sheets("PLANNED") and Sheet2 with Sheets("FINAL"),
    e.g.:

    For r = 4 To Sheets("PLANNED").UsedRange.Rows.Count
    
    If Sheets("PLANNED").Range("C" & r) = Sheets("FINAL").Range("B3") Then
    Sheets("FINAL").Range("C" & i) = Sheets("PLANNED").Range("B" & r)
    Sheets("FINAL").Range("D" & i) = Sheets("PLANNED").Cells(r, matchMonth)
    i = i + 1
    End If
    
    Next r
    For example Cell A3 B 3 is now A5 B5. As long as I keep my cells and rows aligned in the code, the logic is the same.
    Given A3 is now A5 and B3 is now B5 (you haven't specified if the layout has changed in any other way), but for these changes, for example:

    If Target.Address = "$A$5" Or Target.Address = "$B$5" Then
    and

    matchMonth = Application.Match(Sheets("FINAL").Range("A5"), Sheets("PLANNED").Range("3:3"), False)
    and

    If Sheets("PLANNED").Range("C" & r) = Sheets("FINAL").Range("B5") Then
    Last edited by pb71; 06-15-2010 at 02:09 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1