Results 1 to 7 of 7

Thread: excel - copying between sheets in macro

  1. #1

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Question excel - copying between sheets in macro

    I have a workbook of sheets that contain data and then also a reports sheet which i want to use to pull data from the other sheets and list it.

    So, the idea is to scan through column E in each sheet and where the cell is empty copy the entire row to the next line available on the reports sheet.

    This sorta works, but only when 'reports' isnt the active sheet, and then it lists all the other rows after the needed ones. This probably isnt the best way to do it, is there a better way so that it doesnt matter which sheet is active? I'd quite like to put this into a button macro on the reports sheet.

    Code so far:
    VB Code:
    1. Sub Show_Outstanding()
    2. Dim ws As Worksheet
    3. Dim rng As Range
    4. Dim lLastRow As Integer
    5. Dim lLastCol As Integer
    6. Dim x As Integer
    7.  
    8.     For Each ws In Worksheets
    9.         If ws.Name <> "Reports" Then
    10.             Set rng = Worksheets(ws.Name).Range("A1").SpecialCells(xlCellTypeLastCell)
    11.             lLastRow = rng.Row
    12.             lLastCol = rng.Column
    13.            
    14.             For x = 2 To lLastRow
    15.                 If Cells(x, "E") = "" Then
    16.          
    17.                 Worksheets("Reports").Rows(FindBottomRow("Reports", 1)).Value = Worksheets(ws.Name).Rows(x).Value
    18.          
    19.                 End If
    20.             Next x
    21.         End If
    22.     Next
    23. End Sub
    Last edited by Lee_S; Sep 29th, 2005 at 04:26 AM.
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: excel - copying between sheets in macro

    Hi Lee, i have adapted some of my own code to do what you want, i haven't tested it so you will need too, but it should work!

    VB Code:
    1. 'Assumes Summary Sheet is Sheet 1
    2.  
    3. Dim i As Integer         'needs to be declared at top of module
    4. Dim n as Integer
    5.  
    6. i = 1
    7.  
    8. For n = 2 To Worksheets.Count                       ' add to your sub
    9.     Calc_Summary 1, n, Worksheets(n).Name
    10. Next
    11.  
    12.  
    13. Public Sub Calc_Summary(IntSummary As Integer,n As Integer, SheetName As String)
    14.  
    15. Dim strRange As String
    16. Dim SaveRange As String
    17. Dim CellVal As String
    18. Dim Count As Long
    19. Dim max As Long
    20. Dim max2 As Long
    21. Dim SaveRange As String
    22. Dim j As Integer
    23.  
    24. '****
    25.  
    26. max2 = Worksheets(n).Range("E1").End(xlDown).Row        'Max Row for Worksheet being parsed ! - you may need to declare a range on the worksheet as this may not calculate properly with spaces in the column !
    27.  
    28. j = 1
    29.  
    30. strRange = SheetName & "!E1:E" & max2               'Range for Worksheet being parsed !
    31.  
    32.     For Each cell In Range(strRange)                'Loops through Column E for the current spreadsheet
    33.         CellVal = cell.Value
    34.         If CellVal = "" then
    35.                 Worksheets(IntSummary).Rows(i & ":" & i).Select = Worksheets(SheetName).Rows(j & ":" & j)
    36.                 i = i + 1
    37.         End If
    38.     j = j + 1
    39.  
    40.     Next
    41.  
    42. End Sub
    Last edited by NeedSomeAnswers; Sep 30th, 2005 at 04:40 AM.

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel - copying between sheets in macro

    Quote Originally Posted by Lee_S
    This sorta works, but only when 'reports' isnt the active sheet, and then it lists all the other rows after the needed ones. This probably isnt the best way to do it, is there a better way so that it doesnt matter which sheet is active? I'd quite like to put this into a button macro on the reports sheet.

    (Snippets)
    VB Code:
    1. For Each ws In Worksheets
    2.         If ws.Name <> "Reports" Then
    3.             Set rng = Worksheets(ws.Name).Range("A1").SpecialCells(xlCellTypeLastCell)
    4.                 ...
    5.                 Worksheets("Reports").Rows(FindBottomRow("Reports", 1)).Value = Worksheets(ws.Name).Rows(x).Value
    Try using 'handles' for the two sheets you are currently using ... "ws" is already a handle for the current data sheet. Do something like the following:
    Code:
    dim sht_Rpt as Worksheet  'Set a handle for the Reports Sheet
    Set sht_Rpt = Sheets("Reports")
    
    ' Change the following:
                Set rng = Worksheets(ws.Name).Range("A1").SpecialCells(xlCellTypeLastCell)
    ' To something like the following:
                Set rng = ws.Range("A1").SpecialCells(xlCellTypeLastCell)
    '
    ' Change the following:
                    Worksheets("Reports").Rows(FindBottomRow("Reports", 1)).Value = Worksheets(ws.Name).Rows(x).Value
    ' To something like the following:
                    sht_Rpt.Rows(FindBottomRow("Reports", 1)).Value = ws.Rows(x).Value
    In any case, you seem to be using "Worksheets(sheetname)" where I think you should be using "Sheets(sheetname)" to specify a particular sheet. "ws" identifies the current datasheet, and if you add the handle I suggest above, "sht_Rpt" identifies the "Reports" sheet.

    Some functions only work on the active sheet currently in the display window. Check for that.

    Work with it a little and post back again. I hope this helps.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel - copying between sheets in macro

    Also, I think:

    If Cells(x, "E") = "" Then

    Should be something like:

    If ws.Cells(x, 5) = "" Then

    P.S.
    The "E" works in Cells ... I never knew that! I've always converted it to a number!
    Last edited by Webtest; Sep 29th, 2005 at 11:24 AM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Re: excel - copying between sheets in macro

    Brilliant, thanks for the help guys. It really helped and now i'm a step further along. However I have another couple of problems to sort out.

    How can i get it to clear the Reports sheet of all cell contents and formatting EXCEPT the first couple of rows where headers etc are? This is so that fresh data is always shown, rather than adding to the bottom of the sheet.

    Another problem, is that when i copy a cell with a date in it goes from dd/mm/yy to mm/dd/yy and I cant figure out how to stop it. Changing the cell properties doesnt seem to work.

    This is the code so far..
    Code:
    Sub Show_Outstanding()
    Dim rng As Range
    Dim lLastRow As Integer
    Dim x As Integer
    Dim bFound As Boolean
    
    Dim ws As Worksheet
    Dim sht_Rpt As Worksheet  'Set a handle for the Reports Sheet
    Set sht_Rpt = Sheets("Reports")
    
        bFound = False
        For Each ws In Worksheets
            If ws.Name <> "Reports" Then
                Set rng = ws.Range("A1").SpecialCells(xlCellTypeLastCell)
                lLastRow = rng.Row
                
                For x = 2 To lLastRow 'First two rows contain headers etc
                    If ws.Cells(x, "E").Value = "" And ws.Cells(x, "E").Interior.ColorIndex = xlAutomatic Then
                        If bFound = False Then
                            bFound = True
                            With sht_Rpt.Cells(FindBottomRow(sht_Rpt, 1), "A")
                                .Value = ws.Name
                                .Font.Bold = True
                            End With
                        End If
                        sht_Rpt.Rows(FindBottomRow(sht_Rpt, 1)).Value = ws.Rows(x).Value
                    End If
                Next x
            End If
            bFound = False
        Next
    End Sub
    
    Function FindBottomRow(WhatSheet As Worksheet, WhatColumn As Long) As Long
    Dim R As Long
       R = WhatSheet.Cells(65534, WhatColumn).End(xlUp).Row
       If Len(WhatSheet.Cells(R, WhatColumn).Text) Then R = R + 1
       FindBottomRow = R
    End Function
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel - copying between sheets in macro

    I hope you find someone who knows the elegant way involving (I think it is called "Regional Settings"), but in the mean time you can 'brute force' it by setting a range object to the range of cells and doing the following:

    Range(Cells(1,3),Cells(1,7)).NumberFormat = "dd/mm/yy"

    Check Regional Options, Regional and Language Settings, etc.

    You might start a new thread regarding just this topic.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel - copying between sheets in macro

    There are various ways to clear out all but the header rows. Here are a couple of them ... I haven't tried any of them so I don't have specific code.

    Copy the header rows ... I'm pretty sure they go to the clipboard. Clear the entire sheet, and then paste the headers back. You can test this by recording a macro to do that. It will give you a template for your code.

    Here's a good one!
    Rows("3:65536").Delete Shift:=xlUp
    It doesn't take but a split second to run.

    Insert a new blank sheet into the workbook. Copy the headers into the new sheet. Rename the old sheet tab slightly, and assign the old name to the new sheet. Delete the old sheet. I don't really like this way though, because you loose a lot ... Print settings, formatting, column widths, etc. Try my first suggestion first.

    Good Learning and Good Programming!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width