+ Reply to Thread
Results 1 to 7 of 7

Loop in my macro doesn't seem to be running; however, it compiles fine...

  1. #1
    Registered User
    Join Date
    01-24-2006
    Posts
    8

    Question Loop in my macro doesn't seem to be running; however, it compiles fine...

    I have a loop in my code which compiles fine; however, it doesn't seem to be running (or doing anything?) . The loop is supposed to check the values of Column C in each row, If the value is different than the row below it, then it draws a line along the bottom of all those cells. The rest of my macro runs perfectly fine; however, The loop doesn't appear to do anything. Nothing gets highlighted. I added a line right after j = i + 1 to see if the loop even was running which was ThisWorkbook.Worksheets("Sheet1").Cells(i, "F").Value = 5. None of column F's cell's in Sheet 1 had value 5. There was no value in them whatsoever. This leads me to believe my loop isn't doing anything; however, I'm at a loss as to why. The code is as follows with comments in blue:

    Dim i As Integer 'These are initialized at the start of the macro
    Dim j As Integer

    '***Miscelaneous Macro Code that executes perfectly***

    'Start of Loop that doesn't seem to be running
    For i = 2 To 1000 'run loop a maximum of 1000 iterations
    j = i + 1 'j is 1 more than i so that j can be used to check the next row
    If ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value <> "" Then 'check to see If Column C in the row after row(i) isn't empty, if False then exit the loop
    If ThisWorkbook.Worksheets("Sheet1").Cells(i, "C").Value <> ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value Then 'check to see if row(i) column C and row(j) column C have different values, if true then row(i) needs to get formatted with a line along the bottom of all row(i)'s cells
    With Rows("i:i").Borders(xlEdgeBottom) 'Create a line along the bottom of all of row(i)'s cells
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    End If
    End If
    Next i

    '***More Miscelaneous Macro code that executes perfectly***
    Last edited by Goobies; 01-31-2006 at 04:22 AM.

  2. #2

    Re: Loop in my macro doesn't seem to be running; however, it compiles fine...

    I think, With Rows("i:i").Borders(xlEdgeBottom) may be problem.

    'i' of "i:i" is not a variable but is a part of string "i:i"

    Best regards,

    sjoo


  3. #3
    Forum Contributor
    Join Date
    01-10-2006
    Location
    Ahmedabad, India
    MS-Off Ver
    Office 2000
    Posts
    346

    Re: Loop in my macro doesn't seem to be running; however, it compiles fine

    Quote Originally Posted by [email protected]
    I think, With Rows("i:i").Borders(xlEdgeBottom) may be problem.

    'i' of "i:i" is not a variable but is a part of string "i:i"

    Best regards,

    sjoo
    That is right. Instead of Rows("i:i") just say rows(i) and it should work

    A V Veerkar

  4. #4
    Registered User
    Join Date
    01-24-2006
    Posts
    8

    Unhappy

    Quote Originally Posted by avveerkar
    That is right. Instead of Rows("i:i") just say rows(i) and it should work

    A V Veerkar
    I tried this but still no luck. Nothing happens. I've attached the macro in it's entirety in case something else is keeping this loop from doing anything. Most of the code was generated using the record macro button in excell so please excuse the .select this and .select that... I haven't had a chance to clean up that part of the code. Also, Everthing before and after the For loop executes properly, it just seems the loop itself is doing nothing...? I've highlighted the loop in blue:

    Dim i As Integer
    Dim j As Integer

    Cells.Select
    Selection.Columns.AutoFit
    Range("A1").Select
    Sheets("output").Select
    Sheets.Add
    Sheets("output").Select
    Columns("B:B").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Columns("A:A").Select
    ActiveSheet.Paste
    Sheets("output").Select
    ActiveWindow.SmallScroll ToRight:=14
    Columns("T:T").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Columns("B:B").Select
    ActiveSheet.Paste
    Sheets("output").Select
    ActiveWindow.SmallScroll ToRight:=16
    Columns("AG:AG").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Columns("C:C").Select
    ActiveSheet.Paste
    Range("A1").Select
    Sheets("output").Select
    ActiveWindow.SmallScroll ToRight:=31
    Columns("BL:BL").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Columns("D:D").Select
    ActiveSheet.Paste
    Columns("B:B").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlToRight
    Range("B1").Select
    ActiveCell.FormulaR1C1 = ""
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "PC"
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],4)"
    Range("C2").Select
    Selection.AutoFill Destination:=Range("C2:C1000"), Type:=xlFillDefault
    Range("C2:C1000").Select
    ActiveWindow.SmallScroll Down:=-1098
    Columns("A:A").Select
    Selection.NumberFormat = "mm/dd/yy"
    Cells.Select
    Selection.Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range("A2") _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
    DataOption2:=xlSortNormal

    For i = 2 To 1000
    j = i + 1
    If ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value <> "" Then
    If ThisWorkbook.Worksheets("Sheet1").Cells(i, "C").Value <> ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value Then
    With Rows(i).Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    End If
    End If
    Next i


    Range("A1").Select
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    "Sheet1!R1C1:R1000C5").CreatePivotTable TableDestination:="", TableName:= _
    "PivotTable1"
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
    ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="PC"
    ActiveSheet.PivotTables("PivotTable1").PivotFields("PC").Orientation = _
    xlDataField
    Sheets("Sheet1").Select
    ActiveSheet.PivotTableWizard SourceType:=xlPivotTable, SourceData:= _
    "[output.csv]Sheet2!PivotTable1", TableDestination:="", TableName:= _
    "PivotTable2"
    ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
    ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:="Owner"
    ActiveSheet.PivotTables("PivotTable2").PivotFields("Owner").Orientation = _
    xlDataField
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Owner")
    .PivotItems("(blank)").Visible = False
    End With
    Range("A5").Select
    ActiveSheet.PivotTables("PivotTable2").PivotSelect "", xlDataAndLabel
    Selection.Copy
    Sheets("Sheet2").Select
    Range("D3").Select
    ActiveSheet.Paste
    Cells.Select
    Selection.Columns.AutoFit
    ActiveWindow.SmallScroll Down:=-6
    Range("A1").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("PC")
    .PivotItems("").Visible = False
    End With
    ActiveWindow.SmallScroll Down:=-2
    Sheets("Sheet2").Select
    Sheets("Sheet2").Name = "Summary"
    Range("A1").Select
    Sheets("Sheet3").Select
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.Delete
    End Sub
    Last edited by Goobies; 01-31-2006 at 06:03 PM.

  5. #5
    Registered User
    Join Date
    01-24-2006
    Posts
    8
    Bump! Any help is appreciated!

  6. #6
    Tom Ogilvy
    Guest

    Re: Loop in my macro doesn't seem to be running; however, it compiles fine...

    With Rows(i).Borders(xlEdgeBottom)
    LineStyle = xlContinuous
    Weight = xlMedium
    ColorIndex = xlAutomatic
    End With

    should be

    With Rows(i).Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With

    just to demonstrate, this works fine:

    Sub AABB()
    For i = 10 To 15
    With Rows(i).Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    Next
    End Sub

    --
    Regards,
    Tom Ogilvy



    "Goobies" <[email protected]> wrote in
    message news:[email protected]...
    >
    > avveerkar Wrote:
    > > That is right. Instead of Rows("i:i") just say rows(i) and it should
    > > work
    > >
    > > A V Veerkar

    >
    > I tried this but still no luck. Nothing happens. I've attached the
    > macro in it's entirety in case something else is keeping this loop from
    > doing anything. Most of the code was generated using the record macro
    > button in excell so please excuse the .select this and .select that...
    > I haven't had a chance to clean up that part of the code. Also,
    > Everthing before and after the For loop executes properly, it just
    > seems the loop itself is doing nothing...? I've highlighted the loop
    > in blue:
    >
    > Dim i As Integer
    > Dim j As Integer
    >
    > Cells.Select
    > Selection.Columns.AutoFit
    > Range("A1").Select
    > Sheets("output").Select
    > Sheets.Add
    > Sheets("output").Select
    > Columns("B:B").Select
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("A:A").Select
    > ActiveSheet.Paste
    > Sheets("output").Select
    > ActiveWindow.SmallScroll ToRight:=14
    > Columns("T:T").Select
    > Application.CutCopyMode = False
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("B:B").Select
    > ActiveSheet.Paste
    > Sheets("output").Select
    > ActiveWindow.SmallScroll ToRight:=16
    > Columns("AG:AG").Select
    > Application.CutCopyMode = False
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("C:C").Select
    > ActiveSheet.Paste
    > Range("A1").Select
    > Sheets("output").Select
    > ActiveWindow.SmallScroll ToRight:=31
    > Columns("BL:BL").Select
    > Application.CutCopyMode = False
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("D:D").Select
    > ActiveSheet.Paste
    > Columns("B:B").Select
    > Application.CutCopyMode = False
    > Selection.Insert Shift:=xlToRight
    > Range("B1").Select
    > ActiveCell.FormulaR1C1 = ""
    > Columns("B:B").Select
    > Selection.Delete Shift:=xlToLeft
    > Columns("C:C").Select
    > Selection.Insert Shift:=xlToRight
    > Range("C1").Select
    > ActiveCell.FormulaR1C1 = "PC"
    > Range("C2").Select
    > ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],4)"
    > Range("C2").Select
    > Selection.AutoFill Destination:=Range("C2:C1000"),
    > Type:=xlFillDefault
    > Range("C2:C1000").Select
    > ActiveWindow.SmallScroll Down:=-1098
    > Columns("A:A").Select
    > Selection.NumberFormat = "mm/dd/yy"
    > Cells.Select
    > Selection.Sort Key1:=Range("C2"), Order1:=xlAscending,
    > Key2:=Range("A2") _
    > , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
    > MatchCase:= _
    > False, Orientation:=xlTopToBottom,
    > DataOption1:=xlSortTextAsNumbers, _
    > DataOption2:=xlSortNormal
    >
    > For i = 2 To 1000
    > j = i + 1
    > If ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value <> ""
    > Then
    > If ThisWorkbook.Worksheets("Sheet1").Cells(i, "C").Value <>
    > ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value Then
    > With Rows(i).Borders(xlEdgeBottom)
    > LineStyle = xlContinuous
    > Weight = xlMedium
    > ColorIndex = xlAutomatic
    > End With
    > End If
    > End If
    > Next i
    >
    > Range("A1").Select
    > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=
    > _
    > "Sheet1!R1C1:R1000C5").CreatePivotTable TableDestination:="",
    > TableName:= _
    > "PivotTable1"
    > ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3,
    > 1)
    > ActiveSheet.Cells(3, 1).Select
    > ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
    > ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="PC"
    >
    > ActiveSheet.PivotTables("PivotTable1").PivotFields("PC").Orientation =
    > _
    > xlDataField
    > Sheets("Sheet1").Select
    > ActiveSheet.PivotTableWizard SourceType:=xlPivotTable, SourceData:=
    > _
    > "[output.csv]Sheet2!PivotTable1", TableDestination:="",
    > TableName:= _
    > "PivotTable2"
    > ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
    > ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:="Owner"
    >
    > ActiveSheet.PivotTables("PivotTable2").PivotFields("Owner").Orientation
    > = _
    > xlDataField
    > With ActiveSheet.PivotTables("PivotTable2").PivotFields("Owner")
    > PivotItems("(blank)").Visible = False
    > End With
    > Range("A5").Select
    > ActiveSheet.PivotTables("PivotTable2").PivotSelect "",
    > xlDataAndLabel
    > Selection.Copy
    > Sheets("Sheet2").Select
    > Range("D3").Select
    > ActiveSheet.Paste
    > Cells.Select
    > Selection.Columns.AutoFit
    > ActiveWindow.SmallScroll Down:=-6
    > Range("A1").Select
    > With ActiveSheet.PivotTables("PivotTable1").PivotFields("PC")
    > PivotItems("").Visible = False
    > End With
    > ActiveWindow.SmallScroll Down:=-2
    > Sheets("Sheet2").Select
    > Sheets("Sheet2").Name = "Summary"
    > Range("A1").Select
    > Sheets("Sheet3").Select
    > Application.CutCopyMode = False
    > ActiveWindow.SelectedSheets.Delete
    > End Sub
    >
    >
    > --
    > Goobies
    > ------------------------------------------------------------------------
    > Goobies's Profile:

    http://www.excelforum.com/member.php...o&userid=30768
    > View this thread: http://www.excelforum.com/showthread...hreadid=506683
    >




  7. #7
    Registered User
    Join Date
    01-24-2006
    Posts
    8
    I figured out what the problem was!. BTW thanks all for the advice. It turns out since my macro is saved in my Personal macro workbook I had to use Activeworkbook instead of Thisworkbook.


    Quote Originally Posted by Tom Ogilvy
    With Rows(i).Borders(xlEdgeBottom)
    LineStyle = xlContinuous
    Weight = xlMedium
    ColorIndex = xlAutomatic
    End With

    should be

    With Rows(i).Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With

    just to demonstrate, this works fine:

    Sub AABB()
    For i = 10 To 15
    With Rows(i).Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlMedium
    .ColorIndex = xlAutomatic
    End With
    Next
    End Sub

    --
    Regards,
    Tom Ogilvy



    "Goobies" <[email protected]> wrote in
    message news:[email protected]...
    >
    > avveerkar Wrote:
    > > That is right. Instead of Rows("i:i") just say rows(i) and it should
    > > work
    > >
    > > A V Veerkar

    >
    > I tried this but still no luck. Nothing happens. I've attached the
    > macro in it's entirety in case something else is keeping this loop from
    > doing anything. Most of the code was generated using the record macro
    > button in excell so please excuse the .select this and .select that...
    > I haven't had a chance to clean up that part of the code. Also,
    > Everthing before and after the For loop executes properly, it just
    > seems the loop itself is doing nothing...? I've highlighted the loop
    > in blue:
    >
    > Dim i As Integer
    > Dim j As Integer
    >
    > Cells.Select
    > Selection.Columns.AutoFit
    > Range("A1").Select
    > Sheets("output").Select
    > Sheets.Add
    > Sheets("output").Select
    > Columns("B:B").Select
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("A:A").Select
    > ActiveSheet.Paste
    > Sheets("output").Select
    > ActiveWindow.SmallScroll ToRight:=14
    > Columns("T:T").Select
    > Application.CutCopyMode = False
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("B:B").Select
    > ActiveSheet.Paste
    > Sheets("output").Select
    > ActiveWindow.SmallScroll ToRight:=16
    > Columns("AG:AG").Select
    > Application.CutCopyMode = False
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("C:C").Select
    > ActiveSheet.Paste
    > Range("A1").Select
    > Sheets("output").Select
    > ActiveWindow.SmallScroll ToRight:=31
    > Columns("BL:BL").Select
    > Application.CutCopyMode = False
    > Selection.Copy
    > Sheets("Sheet1").Select
    > Columns("D:D").Select
    > ActiveSheet.Paste
    > Columns("B:B").Select
    > Application.CutCopyMode = False
    > Selection.Insert Shift:=xlToRight
    > Range("B1").Select
    > ActiveCell.FormulaR1C1 = ""
    > Columns("B:B").Select
    > Selection.Delete Shift:=xlToLeft
    > Columns("C:C").Select
    > Selection.Insert Shift:=xlToRight
    > Range("C1").Select
    > ActiveCell.FormulaR1C1 = "PC"
    > Range("C2").Select
    > ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],4)"
    > Range("C2").Select
    > Selection.AutoFill Destination:=Range("C2:C1000"),
    > Type:=xlFillDefault
    > Range("C2:C1000").Select
    > ActiveWindow.SmallScroll Down:=-1098
    > Columns("A:A").Select
    > Selection.NumberFormat = "mm/dd/yy"
    > Cells.Select
    > Selection.Sort Key1:=Range("C2"), Order1:=xlAscending,
    > Key2:=Range("A2") _
    > , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
    > MatchCase:= _
    > False, Orientation:=xlTopToBottom,
    > DataOption1:=xlSortTextAsNumbers, _
    > DataOption2:=xlSortNormal
    >
    > For i = 2 To 1000
    > j = i + 1
    > If ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value <> ""
    > Then
    > If ThisWorkbook.Worksheets("Sheet1").Cells(i, "C").Value <>
    > ThisWorkbook.Worksheets("Sheet1").Cells(j, "C").Value Then
    > With Rows(i).Borders(xlEdgeBottom)
    > LineStyle = xlContinuous
    > Weight = xlMedium
    > ColorIndex = xlAutomatic
    > End With
    > End If
    > End If
    > Next i
    >
    > Range("A1").Select
    > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=
    > _
    > "Sheet1!R1C1:R1000C5").CreatePivotTable TableDestination:="",
    > TableName:= _
    > "PivotTable1"
    > ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3,
    > 1)
    > ActiveSheet.Cells(3, 1).Select
    > ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
    > ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="PC"
    >
    > ActiveSheet.PivotTables("PivotTable1").PivotFields("PC").Orientation =
    > _
    > xlDataField
    > Sheets("Sheet1").Select
    > ActiveSheet.PivotTableWizard SourceType:=xlPivotTable, SourceData:=
    > _
    > "[output.csv]Sheet2!PivotTable1", TableDestination:="",
    > TableName:= _
    > "PivotTable2"
    > ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
    > ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:="Owner"
    >
    > ActiveSheet.PivotTables("PivotTable2").PivotFields("Owner").Orientation
    > = _
    > xlDataField
    > With ActiveSheet.PivotTables("PivotTable2").PivotFields("Owner")
    > PivotItems("(blank)").Visible = False
    > End With
    > Range("A5").Select
    > ActiveSheet.PivotTables("PivotTable2").PivotSelect "",
    > xlDataAndLabel
    > Selection.Copy
    > Sheets("Sheet2").Select
    > Range("D3").Select
    > ActiveSheet.Paste
    > Cells.Select
    > Selection.Columns.AutoFit
    > ActiveWindow.SmallScroll Down:=-6
    > Range("A1").Select
    > With ActiveSheet.PivotTables("PivotTable1").PivotFields("PC")
    > PivotItems("").Visible = False
    > End With
    > ActiveWindow.SmallScroll Down:=-2
    > Sheets("Sheet2").Select
    > Sheets("Sheet2").Name = "Summary"
    > Range("A1").Select
    > Sheets("Sheet3").Select
    > Application.CutCopyMode = False
    > ActiveWindow.SelectedSheets.Delete
    > End Sub
    >
    >
    > --
    > Goobies
    > ------------------------------------------------------------------------
    > Goobies's Profile:

    http://www.excelforum.com/member.php...o&userid=30768
    > View this thread: http://www.excelforum.com/showthread...hreadid=506683
    >

+ 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