+ Reply to Thread
Results 1 to 8 of 8

Struggling to get the Loop to work

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2015
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    18

    Struggling to get the Loop to work

    Hello everyone,

    Imagine if I have more than one row with the current date. My macro works for one row but I want it to work for multiple rows, how can I fix it, here is the code.

        Sub SelectingPartOfTable()
    
        Workbooks.Open Filename:="Y:\Guinness\Error Log\NBCGF Event Log.xlsm"
        Dim i As Integer
        
        Workbooks("Risk").Activate
        Sheets("Sheet1").Select
        
        For i = 1 To 100
        
        If Cells(i, 2) = Date Then
        
        Cells(i, 2).EntireRow.Select
        Selection.Copy
        
        Workbooks("NBCGF Event Log").Activate
        Sheets("NBCGF Error Log").Select
        
        Rows("2:2").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("A2").Select
        ActiveSheet.Paste
        
        End If
        
        Next i
        
    End Sub

  2. #2
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    Ok, first things first, get out of the habit of selecting and activating. Reference objects directly. Besides issues arising from not selecting the right object, it takes less code and executes quicker.

    Sub SelectingPartOfTable()
    
        Dim wbErrorLog as Workbook
        Dim wsErrorLog as Worksheet
        Dim wsRisk as Worksheet
        Dim i as Integer
    
        Set wbErrorLog = Workbooks.Open Filename:="Y:\Guinness\Error Log\NBCGF Event Log.xlsm"
        Set wsErrorLog = wbErrorLog.Sheets("NBCGF Error Log")
        Set wsRisk = Workbooks("Risk").Sheets("Sheet1")
        
        For i = 1 To 100
            If wsRisk.Range("b" & i) = Now() Then
                wsRisk.Range("b" & i).EntireRow.Copy
                with wsErrorLog.Range("a2").EntireRow
                    .Insert Shift:=xlDown
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End If
        Next
        
    End Sub
    This will result in each new line found in the Risk sheet being inserted into row 2 of the ErrorLog and each previously found line being shifted down to make way for it.

    You could also write it so that each new line gets added to the NEXT line of the ErrorLog:

    Sub SelectingPartOfTable()
    
        Dim wbErrorLog as Workbook
        Dim wsErrorLog as Worksheet
        Dim wsRisk as Worksheet
        Dim i as Integer
        Dim WriteLine as Integer
    
        Set wbErrorLog = Workbooks.Open Filename:="Y:\Guinness\Error Log\NBCGF Event Log.xlsm"
        Set wsErrorLog = wbErrorLog.Sheets("NBCGF Error Log")
        Set wsRisk = Workbooks("Risk").Sheets("Sheet1")
        WriteLine = 2
        
        For i = 1 To 100
            If wsRisk.Range("b" & i) = Now() Then
                wsRisk.Range("b" & i).EntireRow.Copy
                with wsErrorLog.Range("a" & WriteLine).EntireRow
                    .PasteSpecial xlPasteAll
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
                WriteLine = WriteLine + 1
            End If
        Next
        
    End Sub
    Hope this helps.
    Last edited by kadeo; 03-18-2015 at 10:51 AM.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Struggling to get the Loop to work

    The technical answer to your question is; after the first iteration of your loop the active worksheet is changed and the code within the loop incorrecly functions on the active workbook/worksheet.

  4. #4
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    Quote Originally Posted by stnkynts View Post
    The technical answer to your question is; after the first iteration of your loop the active worksheet is changed and the code within the loop incorrecly functions on the active workbook/worksheet.
    Perfect example of why you should avoid using Select and Activate!

  5. #5
    Registered User
    Join Date
    01-08-2015
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Struggling to get the Loop to work

    Thank you for your reply, it looks brilliant. I'm getting an error though, here is the code:

    Sub SelectingPartOfTable()
    
        Dim wbErrorLog As Workbook
        Dim wsErrorLog As Worksheet
        Dim wsRisk As Worksheet
        Dim i As Integer
    
        Set wbErrorLog = Workbooks.Open Filename:="Y:\Guinness\Error Log\NBCGF Event Log.xlsm"
        Set wsErrorLog = wbErrorLog.Sheets("NBCGF Error Log")
        Set wsRisk = Workbooks("Risk").Sheets("Sheet1")
        
        For i = 1 To 100
            If wsRisk.Range("b" & i) = Now() Then
                wsRisk.Range("b" & i).EntireRow.Copy
                With wsErrorLog.Range("a2").EntireRow
                    .Insert Shift:=xlDown
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            End If
        Next
        
    End Sub
    It highlights Filename and says

    Compile error:

    Expected: end of statement

    Any ideas?

  6. #6
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    ok, Replace this line:
    Set wbErrorLog = Workbooks.Open Filename:="Y:\Guinness\Error Log\NBCGF Event Log.xlsm"
    with this line:
    Set wbErrorLog = Workbooks.Open("Y:\Guinness\Error Log\NBCGF Event Log.xlsm")
    And see if that works.

  7. #7
    Registered User
    Join Date
    01-08-2015
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Struggling to get the Loop to work

    This works

    Sub SelectingPartOfTable()
    
        Dim wbErrorLog As Workbook
        Dim wsErrorLog As Worksheet
        Dim wsRisk As Worksheet
        Dim i As Integer
    
        Set wbErrorLog = Workbooks.Open("Y:\Guinness\Error Log\NBCGF Event Log.xlsm")
        Set wsErrorLog = wbErrorLog.Sheets("NBCGF Error Log")
        Set wsRisk = Workbooks("Risk").Sheets("Sheet1")
        
        For i = 1 To 100
            If wsRisk.Range("b" & i) = Date Then
                wsRisk.Range("b" & i).EntireRow.Copy
                With wsErrorLog.Range("a2").EntireRow
                    .Insert Shift:=xlDown
                End With
            End If
        Next
        
    End Sub
    Thank you for alll your help

  8. #8
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    Cool, Glad I could help. Mark your thread as solved in the thread options. Cheers Lewis.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Cannot get loop within a loop to work
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2015, 10:02 AM
  2. struggling to get 2 IF formulas to work in one cell
    By cab1979 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-07-2014, 11:18 AM
  3. New Tabs for each run of a loop does not work!!!
    By danitouffaha in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2012, 04:57 PM
  4. My loop doesn't work why not
    By ljh66 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2007, 01:02 PM
  5. struggling to create a loop - pls help
    By joule in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2005, 01:25 PM

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