+ Reply to Thread
Results 1 to 7 of 7

Copying rows to another worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2007
    Posts
    4

    Copying rows to another worksheet

    Hello all,

    I had a question about excel macros and VBA. So I have a sheet of raw data within an excel workbook. I already created a macro to create new worksheets. What I want to do is iterate through the raw data and depending on a value in a certain column, I want to copy that row to another worksheet. I think I know the beginning and the end, but I cant figure out how to iterate through the rows.

    Any help would be much apprecieated.

    Thanks!

  2. #2
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    solidgold10287

    Maybe something like this would get you started.
    Sub aaa()
    
    Dim lRow As Long, lLastRow As Long
    Dim wsSht1 As Worksheet, wsSht2 As Worksheet
    
    wsSht1 = Sheets("Sheet1") 'Raw Data
    wsSht2 = Sheets("Sheet2") 'Filtered Data
    
    lLastRow = wsSht1.Cells(Rows.Count, 1).End(xlUp).row
    
    For lRow = 1 To lLastRow Step 1
    
        'code goes here
    
    Next lRow
    
    End Sub
    Sincerely,
    Jeff

  3. #3
    Registered User
    Join Date
    08-21-2007
    Posts
    4
    Thanks for the quick response. This is what I have but for some reason it keeps throwing this error:

    Run-time error '9':
    Subscript out of range

    It happens right when I try to set wsDefense equal to Sheets("Sheet2"). What am i doing wrong?


        Dim lRow As Long, lLastRow As Long
        Dim wsRaw As Worksheet
        Dim wsDefense As Worksheet
        Dim wsEnergyConsulting As Worksheet
        Dim wsEnergyManaged As Worksheet
        Dim wsBusiness As Worksheet
        Dim wsGeneral As Worksheet
        Dim ws As Worksheet
        Dim Exist As Boolean
        
        Range("A1:M2").Select
        Range("M2").Activate
        Selection.Copy
    
        Exist = False
        For Each ws In Sheets
            If ws.Name = "Defense" Then
                Exist = True
            End If
        Next ws
    
        If Exist = False Then
            Sheets.Add
            ActiveSheet.Name = "Defense"
            ActiveSheet.Paste
        End If
    
        Exist = False
        For Each ws In Sheets
            If ws.Name = "Energy Consulting" Then
                Exist = True
            End If
        Next ws
    
        If Exist = False Then
            Sheets.Add
            ActiveSheet.Name = "Energy Consulting"
            ActiveSheet.Paste
        End If
        
        Exist = False
        For Each ws In Sheets
            If ws.Name = "Energy Managed Services" Then
                Exist = True
            End If
        Next ws
    
        If Exist = False Then
            Sheets.Add
            ActiveSheet.Name = "Energy Managed Services"
            ActiveSheet.Paste
        End If
    
        Exist = False
        For Each ws In Sheets
            If ws.Name = "Business Development" Then
                Exist = True
            End If
        Next ws
    
        If Exist = False Then
            Sheets.Add
            ActiveSheet.Name = "Business Development"
            ActiveSheet.Paste
        End If
        
            Exist = False
        For Each ws In Sheets
            If ws.Name = "General & Adminstrative" Then
                Exist = True
            End If
        Next ws
    
        If Exist = False Then
            Sheets.Add
            ActiveSheet.Name = "General & Adminstrative"
            ActiveSheet.Paste
        End If
        
        wsRaw = Sheets("Sheet1")
        wsDefense = Sheets("Sheet2")
        wsEnergyConsulting = Sheets("Sheet3")
        wsEnergyManaged = Sheets("Sheet4")
        wsBusiness = Sheets("Sheet5")
        wsGeneral = Sheets("Sheet6")
    
        lLastRow = wsRaw.Cells(Rows.Count, 1).End(xlUp).Row
        
        For lRow = 3 To lLastRow Step 1
    
        'Code goes here
        
        Next lRow
        
    End Sub
    I also tried to do wsDefense = Sheets("Defense"), but for some reason i get this error:

    Run-Time error '91':
    Object variable or with block variable not set


    I dont know where to go from here on either of these problems.

    Thanks
    Last edited by solidgold10287; 08-21-2007 at 01:57 PM.

  4. #4
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    solidgold10287,

    I apologize I was a little to quick with my response. I left the work "Set" off of my response.

    These
        wsRaw = Sheets("Sheet1")
        wsDefense = Sheets("Sheet2")
        wsEnergyConsulting = Sheets("Sheet3")
        wsEnergyManaged = Sheets("Sheet4")
        wsBusiness = Sheets("Sheet5")
        wsGeneral = Sheets("Sheet6")
    all need the word Set in front. For example
    Set wsRaw = Sheets("Sheet1")
    Make that change and see what happens. Sorry for the error.

  5. #5
    Registered User
    Join Date
    08-21-2007
    Posts
    4
    Ahh thank you. Works like a charm

  6. #6
    Registered User
    Join Date
    08-21-2007
    Posts
    4
    One more thing, how would I access a certain column. I dont know the syntax. I'm assuming it would be something like:

    lRow.Columns(index of column needed)

    I've been looking but I cant find much on it.

+ 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