+ Reply to Thread
Results 1 to 11 of 11

Looping through sheets help

Hybrid View

apunc1 Looping through sheets help 01-31-2013, 11:33 AM
Norie Re: Looping through sheets... 01-31-2013, 11:36 AM
apunc1 Re: Looping through sheets... 01-31-2013, 11:50 AM
apunc1 Re: Looping through sheets... 01-31-2013, 12:39 PM
Norie Re: Looping through sheets... 01-31-2013, 12:58 PM
apunc1 Re: Looping through sheets... 01-31-2013, 02:09 PM
Norie Re: Looping through sheets... 01-31-2013, 02:11 PM
apunc1 Re: Looping through sheets... 01-31-2013, 02:17 PM
apunc1 Re: Looping through sheets... 01-31-2013, 02:21 PM
apunc1 Re: Looping through sheets... 01-31-2013, 02:33 PM
apunc1 Re: Looping through sheets... 01-31-2013, 03:35 PM
  1. #1
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Looping through sheets help

    Hi,
    The following loop works as intended
    Sub testloop()
    
        Dim ws As Worksheet
        
        For Each ws In ActiveWorkbook.Worksheets
             
            
            
            On Error Resume Next 'Will continue if an error results
            ws.Range("A2") = ws.Name
            
             
        Next ws
    End Sub
    it loops through each spreadsheet in the workbook, putting the name of the sheet in A2.

    The loop below DOES NOT work as intended.

     Sub testmove()
     Dim ws As Worksheet
     For Each ws In ActiveWorkbook.Worksheets
       On Error Resume Next 'Will continue if an error results
        Columns("D:E").Select
        Selection.Copy
        Range("F1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Columns("D:E").Select
        Selection.delete
    Next ws
    End Sub
    it loops through the code correctly, but ON THE SAME SHEET, not looping THROUGH the sheets. It just keep coping D and E to F, deleting D and E, over and over on the same sheet.

    what is the problem?

    thanks.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through sheets help

    You are missing worksheet references for Columns, Range etc.

    Try this.
    Sub testmove()
    Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            With ws
                .Columns("D:E").Copy .Range("F1")
    
                .Columns("D:E").Delete
                Application.CutCopyMode = False
            End With
        Next ws
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    Thanks Norie. This appears to be working.
    What I'm really trying to do is loop through all worksheets in a workbook, find the words Volume and Revenue in the header row, copy and paste those columns to the column after the last column of data (lastusedcolumn+1) and delete the original columns.

    below is my attempt, but it just loops through the columns on one spreadsheet again.
    Sub VolumeMove()
    
        Dim ws As Worksheet
         Dim last_column As Integer
        Dim targetcol As Integer
        
        For Each ws In ActiveWorkbook.Worksheets
             
        
             'last_column = ws.Cells.find("*", [A1], , , xlByColumns, xlPrevious).Column
             With ws
              last_column = ws.Cells(1, .Columns.Count).End(xlToLeft).Column
    
    
            On Error Resume Next 'Will continue if an error results
           
             For iloop = 1 To last_column
                If InStr(1, ws.Cells(1, iloop), "Volume", vbTextCompare) <> 0 Then
         Columns(iloop).Select
                Selection.Copy
                End If
        targetcol = last_column + 1
            With ws.Columns(targetcol)
            ws.Columns(targetcol).Select
          Selection.PasteSpecial xlPasteAll
            ws.Cells(1, targetcol).Value = "Total Volume"
           End With
               
         
            
            Next iloop
         End With
        Next ws
    End Sub
    Quote Originally Posted by Norie View Post
    You are missing worksheet references for Columns, Range etc.

    Try this.
    Sub testmove()
    Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
            With ws
                .Columns("D:E").Copy .Range("F1")
    
                .Columns("D:E").Delete
                Application.CutCopyMode = False
            End With
        Next ws
    End Sub

  4. #4
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    ok I can get it to do everything I need except copy the entire column. It just copies the header into the first column after the last column of data. also i can't get it to delete the original "volume" column.


    Sub VolumeMove()
    
        Dim ws As Worksheet
         Dim last_column As Integer
        Dim targetcol As Integer
        Dim curCol As Integer
        
        
        For Each ws In ActiveWorkbook.Worksheets
             
        
             'last_column = ws.Cells.find("*", [A1], , , xlByColumns, xlPrevious).Column
             With ws
              last_column = ws.Cells(1, .Columns.Count).End(xlToLeft).Column
    
    
            On Error Resume Next 'Will continue if an error results
           
             For iloop = 1 To last_column
             
               curCol = Column + last_column
                If InStr(1, ws.Cells(1, iloop), "Volume", vbTextCompare) <> 0 Then
                .Columns(curCol).Copy
                End If
        targetcol = last_column + 1
            With ws.Columns(targetcol)
            ws.Columns(targetcol).Select
          Selection.PasteSpecial xlPasteAll
            ws.Cells(1, targetcol).Value = "Total Volume"
            
           End With
                If InStr(1, ws.Cells(1, iloop), "Volume", vbTextCompare) <> 0 Then
                .Columns(curCol).delete xlToLeft
            End If
         
            
            Next iloop
         End With
        Next ws
    End Sub

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through sheets help

    Does this work?
    Sub VolumeMove()
    
    Dim ws As Worksheet
    Dim last_column As Integer
    Dim targetcol As Integer
    Dim curCol As Integer
    
    
        For Each ws In ActiveWorkbook.Worksheets
    
    
            'last_column = ws.Cells.find("*", [A1], , , xlByColumns, xlPrevious).Column
            With ws
                last_column = ws.Cells(1, .Columns.Count).End(xlToLeft).Column
    
                targetcol = last_column + 1
    
    
                For iloop = 1 To last_column
    
                    curCol = Column + last_column
                    If InStr(1, ws.Cells(1, iloop), "Volume", vbTextCompare) <> 0 Then
    
                        .Columns(curCol).Copy .Columns(targetcol)
    
                        .Cells(1, targetcol).Value = "Total Volume"
                        .Columns(curCol).Delete xlToLeft
                    End If
    
    
                Next iloop
            End With
        Next ws
    End Sub

  6. #6
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    Thanks. It works on some of the sheets but not all. Also, it's deleting thewrong column. I want to rename the new Volume column "Total Volume" and remove the old "volume" column.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through sheets help

    Is there always a volume column on a worksheet?

    Can you attach a sample workbook?

  8. #8
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    yes. There are about ten worksheets with silmilar column headings. All Have a Volume and Revenue Column. It's an export from an Access crosstab, so the volume and revenue totals are in the wrong place. I want them at the end of the other colums.

    Quote Originally Posted by Norie View Post
    Is there always a volume column on a worksheet?

    Can you attach a sample workbook?

  9. #9
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    I can't get the file to upload. Sorry

    edit: it's also coping and pasting the wrong volume column. I actually don't know what it's pasting!
    Last edited by apunc1; 01-31-2013 at 02:30 PM.

  10. #10
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    The vbscript below works for one sheet only. When I try to loop the sheets, the loop doesn't work so that's why i'm trying to do this in VBA. I was trying to follow the basic structure of the vbscript, since it works, but add the looping sheets.

     Set objExcel = CreateObject("Excel.Application")  
    objExcel.Visible = False  'opens in read-write mode
    objExcel.DisplayAlerts = 0  
    
    objExcel.Workbooks.Open "H:\SMART\ReportStd\Parrish\GenzymeReveal\" & "Microarray_LabDM_Reveal_" & dtmYesterday & ".xlsx",,False
    
    Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets("Data")  
    
    usedColumnsCount = currentWorkSheet.UsedRange.Columns.Count  
    usedRowsCount = currentWorkSheet.UsedRange.Rows.Count  
    top = currentWorksheet.UsedRange.Row  
    left = currentWorksheet.UsedRange.Column  
    
    Set Cells = currentWorksheet.Cells  
    For row = 0 to (usedRowsCount - 1)  
     For column = 0 to (usedColumnsCount - 1)  
      curRow = row+top  
      curCol = column+left  
      word = Cells(curRow,curCol).Value
      
     if instr(1, word, "Grand") > 0  then
          With currentWorkSheet.Columns(curCol)
    		.Copy
    	 End With   
    	
    targetCol = usedColumnsCount + 1 
    With currentWorkSheet.Columns(targetCol)
    objexcel.Columns(targetCol).Select
    objexcel.Selection.PasteSpecial xlPasteAll
    objexcel.cells(1,targetCol).Value="Total Volume"
    end with
     	End If 
    	'delete old total column
    	  if instr(1, word, "Grand") > 0  then
          With currentWorkSheet.Columns(curCol)
    		.Delete xlToLeft
    	    End With 
    
    End If 
      
    Next  'next row
    Next	'next column

  11. #11
    Registered User
    Join Date
    10-17-2012
    Location
    nc
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Looping through sheets help

    this code does what i want. I had curCol defined incorrectly, so it was doing weird things. thanks for your help.

    Sub VolumeMove()
    
    Dim ws As Worksheet
    Dim last_column As Integer
    Dim targetcol As Integer
    Dim curCol As Integer
    
    
        For Each ws In ActiveWorkbook.Worksheets
    
    
            'last_column = ws.Cells.find("*", [A1], , , xlByColumns, xlPrevious).Column
            With ws
                last_column = ws.Cells(1, .Columns.Count).End(xlToLeft).Column
    
                targetcol = last_column + 1
    
    
                For iloop = 1 To last_column
    
                    curCol = Column + iloop
                    If InStr(1, ws.Cells(1, iloop), "Volume", vbTextCompare) <> 0 Then
    
                        .Columns(curCol).Copy .Columns(targetcol)
    
                        .Cells(1, targetcol).Value = "Total Volume"
                        .Columns(curCol).delete xlToLeft
                    End If
                Next iloop
               
            End With
            
        Next ws
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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