+ Reply to Thread
Results 1 to 6 of 6

Running code for three sets of worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2013
    Location
    syracuse, ny
    MS-Off Ver
    Excel 2003
    Posts
    9

    Exclamation Running code for three sets of worksheets

    Hi, I have a vba problem that probably has a very easy answer.
    I have the code below which formats a table so that I can make a pivot table out of it. What I need to do now is to apply this code to 3 different sets of worksheets. The first set (below) takes data from "M) Avg Hrs- Month" and formats the values into a table labeled "M) Data for PT." I know need this code to run for "A) Avg Hrs- Month"/ "A) Data for PT" and "N) Avg Hrs- Month"/"A) Data for PT." I canot just copy and paste the code and change the names of the worksheets it uses because the variable names are the same.

    Thanks in advance everyone!! I am a BIG vba novice so any help is appreciated!

    A sample workbook is attached!


    
    Option Explicit
    Sub ReorgData()
    
    With Excel.Application
            .ScreenUpdating = False
            .Calculation = Excel.xlManual
            .EnableEvents = False
    End With
    
    
    'find active range
    Sheets("M) Avg Hrs- Month").Select
    Columns("A:A").Select
        Selection.Find(What:="", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Activate
        ActiveCell.Offset(-1, 0).Select
        Cells.FindNext(After:=ActiveCell).Activate
        ActiveCell.Offset(0, -1).Select
        Range(Selection, Selection.End(xlUp)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
    
    Dim a As Variant, b As Variant
    Dim c As Long, i As Long, ii As Long
    With Sheets("M) Avg Hrs- Month")
      a = Selection
      ReDim b(1 To (UBound(a, 1) * (UBound(a, 2) - 3)), 1 To 5)
    End With
    For c = 4 To UBound(a, 2)
      For i = 2 To UBound(a, 1)
        ii = ii + 1
        b(ii, 1) = a(i, 1)
        b(ii, 2) = a(i, 2)
        b(ii, 3) = a(i, 3)
        b(ii, 4) = a(1, c)
        b(ii, 5) = a(i, c)
      Next i
    Next c
    With Sheets("M) Data for PT")
      .UsedRange.ClearContents
      With .Cells(1, 1).Resize(, 5)
        .Value = [{"Resource Name","Team","Department","Month","Hours"}]
        .Font.Bold = True
      End With
      .Cells(2, 1).Resize(UBound(b, 1), UBound(b, 2)) = b
      .Columns.AutoFit
      .Activate
    End With
    
    'delete extra
    Sheets("M) Data for PT").Select
    Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        Selection.Delete
        Range("A1").Select
    
        
    'Paste Values
    Sheets("M) Data for PT").Select
        Columns("E:E").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("A1").Select
        Columns("E:E").Select
        Selection.NumberFormat = "0.0"
        Range("A1").Select
        ActiveWorkbook.Save
    
    With Excel.Application
        .ScreenUpdating = True
        .Calculation = Excel.xlAutomatic
        .EnableEvents = True
    End With
        
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Running code for three sets of worksheets

    Cross posted here with a solution: http://www.excelforum.com/excel-prog...st-column.html
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Running code for three sets of worksheets

    I was mistaken in reply #2 - sorry.

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Running code for three sets of worksheets

    ag123,

    With your latest attached workbook containing the following three worksheets, and, no formulae as per your other thread:
    M) Avg Hrs- Month
    N) Avg Hrs- Month
    A) Avg Hrs- Month


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub ReorgDataForThree()
    ' stanleydgromjr, 07/15/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/938902-copy-column-and-paste-to-end-of-first-column.html
    Dim r As Long, lr As Long, nlr As Long, c As Long, lc As Long, nlc As Long
    Dim a As Variant, b As Variant
    Dim i As Long, ii As Long
    Application.ScreenUpdating = False
    With Sheets("M) Avg Hrs- Month")
      lr = .Cells(Rows.Count, 1).End(xlUp).Row + 1
      lc = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
      For r = 1 To lr Step 1
        If .Cells(r, 1).Value = "" Then nlr = r - 1
      Next r
      For c = 1 To lc Step 1
        If .Cells(1, c).Value = "" Then nlc = c - 1
      Next c
      a = .Range(.Cells(1, 1), .Cells(nlr, nlc)).Value
      ReDim b(1 To (UBound(a, 1) * (UBound(a, 2) - 3)), 1 To 5)
      For c = 4 To UBound(a, 2)
        For i = 2 To UBound(a, 1)
          If a(i, 1) <> "" Then
            ii = ii + 1
            b(ii, 1) = a(i, 1)
            b(ii, 2) = a(i, 2)
            b(ii, 3) = a(i, 3)
            b(ii, 4) = a(1, c)
            b(ii, 5) = a(i, c)
          End If
        Next i
      Next c
    End With
    If Not Evaluate("ISREF('M) Data for PT'!A1)") Then Worksheets.Add(After:=Sheets("M) Avg Hrs- Month")).Name = "M) Data for PT"
    With Sheets("M) Data for PT")
      .UsedRange.ClearContents
      With .Cells(1, 1).Resize(, 5)
        .Value = [{"Resource Name","Team","Department","Month","Hours"}]
        .Font.Bold = True
      End With
      .Cells(2, 1).Resize(UBound(b, 1), UBound(b, 2)) = b
      lr = .Cells(Rows.Count, 1).End(xlUp).Row
      .Range("E2:E" & lr).NumberFormat = "#,##0.00"
      .Columns.AutoFit
    End With
    Erase a: Erase b
    i = 0: ii = 0
    With Sheets("N) Avg Hrs- Month")
      lr = .Cells(Rows.Count, 1).End(xlUp).Row + 1
      lc = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
      For r = 1 To lr Step 1
        If .Cells(r, 1).Value = "" Then nlr = r - 1
      Next r
      For c = 1 To lc Step 1
        If .Cells(1, c).Value = "" Then nlc = c - 1
      Next c
      a = .Range(.Cells(1, 1), .Cells(nlr, nlc)).Value
      ReDim b(1 To (UBound(a, 1) * (UBound(a, 2) - 3)), 1 To 5)
      For c = 4 To UBound(a, 2)
        For i = 2 To UBound(a, 1)
          If a(i, 1) <> "" Then
            ii = ii + 1
            b(ii, 1) = a(i, 1)
            b(ii, 2) = a(i, 2)
            b(ii, 3) = a(i, 3)
            b(ii, 4) = a(1, c)
            b(ii, 5) = a(i, c)
          End If
        Next i
      Next c
    End With
    If Not Evaluate("ISREF('N) Data for PT'!A1)") Then Worksheets.Add(After:=Sheets("N) Avg Hrs- Month")).Name = "N) Data for PT"
    With Sheets("N) Data for PT")
      .UsedRange.ClearContents
      With .Cells(1, 1).Resize(, 5)
        .Value = [{"Resource Name","Team","Department","Month","Hours"}]
        .Font.Bold = True
      End With
      .Cells(2, 1).Resize(UBound(b, 1), UBound(b, 2)) = b
      lr = .Cells(Rows.Count, 1).End(xlUp).Row
      .Range("E2:E" & lr).NumberFormat = "#,##0.00"
      .Columns.AutoFit
    End With
    Erase a: Erase b
    i = 0: ii = 0
    With Sheets("A) Avg Hrs- Month")
      lr = .Cells(Rows.Count, 1).End(xlUp).Row + 1
      lc = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
      For r = 1 To lr Step 1
        If .Cells(r, 1).Value = "" Then nlr = r - 1
      Next r
      For c = 1 To lc Step 1
        If .Cells(1, c).Value = "" Then nlc = c - 1
      Next c
      a = .Range(.Cells(1, 1), .Cells(nlr, nlc)).Value
      ReDim b(1 To (UBound(a, 1) * (UBound(a, 2) - 3)), 1 To 5)
      For c = 4 To UBound(a, 2)
        For i = 2 To UBound(a, 1)
          If a(i, 1) <> "" Then
            ii = ii + 1
            b(ii, 1) = a(i, 1)
            b(ii, 2) = a(i, 2)
            b(ii, 3) = a(i, 3)
            b(ii, 4) = a(1, c)
            b(ii, 5) = a(i, c)
          End If
        Next i
      Next c
    End With
    If Not Evaluate("ISREF('A) Data for PT'!A1)") Then Worksheets.Add(After:=Sheets("A) Avg Hrs- Month")).Name = "A) Data for PT"
    With Sheets("A) Data for PT")
      .UsedRange.ClearContents
      With .Cells(1, 1).Resize(, 5)
        .Value = [{"Resource Name","Team","Department","Month","Hours"}]
        .Font.Bold = True
      End With
      .Cells(2, 1).Resize(UBound(b, 1), UBound(b, 2)) = b
      lr = .Cells(Rows.Count, 1).End(xlUp).Row
      .Range("E2:E" & lr).NumberFormat = "#,##0.00"
      .Columns.AutoFit
    End With
    Sheets(2).Activate
    Application.ScreenUpdating = True
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the ReorgDataForThree macro.
    Last edited by stanleydgromjr; 07-15-2013 at 08:47 AM.

  5. #5
    Registered User
    Join Date
    07-13-2013
    Location
    syracuse, ny
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Running code for three sets of worksheets

    You are a LIFESAVER! Thank you so much!!!!

  6. #6
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Running code for three sets of worksheets

    ag123,

    Thanks for the feedback.

    You are very welcome. Glad I could help.

    Come back anytime.

+ 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. 2 different sets of code in 1 tab
    By fionaby in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-10-2012, 05:45 AM
  2. How to match two sets of data between two worksheets
    By humptydumpty in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-07-2010, 04:48 AM
  3. Transferring Data Sets Between Worksheets
    By eichstadt28 in forum Excel General
    Replies: 3
    Last Post: 04-14-2009, 12:16 PM
  4. Error code when I run 2 sets of code.
    By richard11153 in forum Excel General
    Replies: 1
    Last Post: 11-28-2008, 02:33 AM
  5. Hiding and Unhiding two sets of worksheets
    By Glio in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-14-2008, 03:09 PM

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