+ Reply to Thread
Results 1 to 8 of 8

Error with a loop across columns with an inside loop down rows

  1. #1
    Registered User
    Join Date
    01-02-2018
    Location
    Iowa
    MS-Off Ver
    2017
    Posts
    27

    Error with a loop across columns with an inside loop down rows

    At first, I posted this at

    https://www.mrexcel.com/board/thread...think.1206014/

    with no reply's I reworked the code but still get errors


    I have several worksheets (at present 27) with or without data in row(3). Row(1), and row(2) values depend on if row(3) is blank; for example if D3 has data, then D1 will have 'Data from ID 2' and D2 will have 'Columm 4' and cells 4 to 304 in column(D) will have borders for more information. If AF3 is blank, then AF1 and AF2 will be blank and the cells in column AF will be without borders. The worksheets will vary the placement of the primary data BUT always in the 3rd row. I'm still a 'NEWBIE' with vba and so my looping skills are simple as you can see from my code attempt. I'm getting (Run-time error 1004, Application-defined or object-defined error) , and of course it does not say where. Any help will be appreciated.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error with a loop across columns with an inside loop down rows

    This is a bit if a guess, but if you are wanting to loop from column 3 to column LC then this is the wrong syntax...
    Please Login or Register  to view this content.
    You use a range reference instead of a column number.

    Try this...
    Please Login or Register  to view this content.
    Or just this...
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    01-02-2018
    Location
    Iowa
    MS-Off Ver
    2017
    Posts
    27

    Re: Error with a loop across columns with an inside loop down rows

    I tried both add-on 's but I still get the same error. I'll keep working on it thought. I'll keep the first suggestion about .column. I forgot about it. THANKS

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Error with a loop across columns with an inside loop down rows

    Please Login or Register  to view this content.
    Last edited by protonLeah; 05-25-2022 at 11:07 PM.
    Ben Van Johnson

  5. #5
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,463

    Re: Error with a loop across columns with an inside loop down rows

    May be:
    PHP Code: 
    Option Explicit
    Sub Test
    ()
    Dim lc&, cell As Rangews As Worksheet
    For Each ws In Worksheets ' loop with each sheet in workbook
        '
    If ws.name <> "Main" then ====> add this condition, if you want to exclude specific sheet "Main"
        
    lc ws.Cells(3Columns.Count).End(xlToLeft).Column
        ws
    .Range("A1"ws.Cells(2lc)).ClearContents ' delete history in row 1 & 2
        ws.Range(ws.Cells(1, lc + 1), ws.Cells(Rows.Count, Columns.Count)).Delete ' 
    delete all data and border from last column
        
    For Each cell In ws.Range(ws.Cells(3"A"), ws.Cells(3lc)) ' loop from A3 to last cell with data in row 3
            Select Case IsEmpty(cell)
                Case True
                    cell.Offset(1, 0).Resize(304, 1).Borders.LineStyle = xlNone
                Case Else
                    cell.Offset(-2, 0).Value = "Data from ID" & cell.Column - 2
                    cell.Offset(-1, 0).Value = "COLUMN" & cell.Column
                    cell.Offset(1, 0).Resize(304, 1).Borders.LineStyle = xlContinuous
                End Select
        Next
    Next
    End Sub 
    Quang PT

  6. #6
    Registered User
    Join Date
    01-02-2018
    Location
    Iowa
    MS-Off Ver
    2017
    Posts
    27

    Re: Error with a loop across columns with an inside loop down rows

    bebo021999
    I liked your code and it work but it works on ALL worksheets. My worksheets have a 2-part name with a separator (.) such as 'Data.All' and 'Data.Mathmatics' and 'Main.Switchboard' and 'Main.Collected' etc. Can this code run on only the worksheets with the name starting with Data.
    Last edited by kds14589; 05-26-2022 at 02:02 PM.

  7. #7
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,463

    Re: Error with a loop across columns with an inside loop down rows

    HTML Code: 

  8. #8
    Registered User
    Join Date
    01-02-2018
    Location
    Iowa
    MS-Off Ver
    2017
    Posts
    27

    Re: Error with a loop across columns with an inside loop down rows

    Thanks for the help! At first had an error but then discovered extra speech mark after Data (If ws.name like "Data*""). Once it was remover code worked fine. AGAIN THANKS

+ 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] (Beginner help) For loop inside do loop that displays information from reference sheet.
    By lediable007 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-08-2016, 03:20 PM
  2. Loop Across Columns...If Blank Cell, Loop Down Rows
    By flindy87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2014, 07:05 PM
  3. Loop Across Columns If Blank Cell Loop Down Rows
    By flindy87 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2014, 10:22 AM
  4. Multiple If statements inside a for loop error
    By pvhs07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2013, 01:00 PM
  5. Changing the index of a for loop inside of the loop
    By drinkmorewine in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-03-2013, 12:19 PM
  6. VBA For loop where number of iterations can be changed from inside loop
    By barrboy89 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-11-2011, 12:18 PM
  7. 'Loop without do' error message (code inside)
    By Jeroen1000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-23-2009, 07:16 AM

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