Consulting

Results 1 to 8 of 8

Thread: Solved: Help Writing VBA code

  1. #1
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location

    Solved: Help Writing VBA code

    I have a workbook with 5 tabs. Tabs 1, 2 and 3 are pivot charts that Tabs 4 and 5 pull data from and they are set up like =Sheet1!J17 etc. What I am wanting to do is write a VBA macro that will open up the workbook, copy tabs 4 & 5 but ONLY the formatting and values. I have a macro that will do everything with the exception of only copy the values and formatting. The macro that I have will copy everything, whereas I need one that will only copy the values and formatting so that the links are lost. The code below is what I have that will copy the data, but every way I try to only copy formats/values errors out (compile error)


    [vba]
    Public Sub Move_WOrksheets
    'Half Works --- Just copies more than just he values
    Application.DisplayAlerts = False
    Workbooks.Open Filename:="C:\Test_Data\Test.xls"
    With Sheets(Array("Test_Sheet_1", "Test_Sheet_2")).Select
    ActiveWindow.SelectedSheets.Copy
    Application.CutCopyMode = False
    ActiveWorkbook.SaveCopyAs Filename:="C:\Test_Reports\" & "Working_" & Format(Date, "mmddyyyy") & ".xls"
    End With
    Application.Quit
    End Sub
    [/vba]
    Last edited by Simon Lloyd; 10-21-2011 at 02:10 AM. Reason: adjusted the code tags for the correct usage

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    try this with a backup of the file.


    [vba]
    Public Sub Move_WOrksheets()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim calc As Long
    Dim fName As String

    calc = Application.Calculation
    fName = "C:\Test_Reports\" & "Working_" & Format(Date, "mmddyyyy")

    With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    Set wb = Workbooks.Open("C:\Test_Data\Test.xls")

    With wb
    Sheets(Array("Test_Sheet_1", "Test_Sheet_2")).Copy
    With ActiveWorkbook
    For Each ws In .Worksheets
    ws.UsedRange.Value = ws.UsedRange.Value
    Next
    .SaveAs Filename:=fName, FileFormat:=56
    .Close
    End With
    .Close
    End With

    With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .Calculation = calc
    End With

    Application.Quit

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    The code you posted above will accomplish what I was after, thank you so much for your help!!! One tweak I would like if possible....

    The pivot charts refresh on open, but this macro above copies the sheets before they have time to display the updated values. Is there a way to either 1) delay the copy long enough for the two sheets to display the current values, or 2) run a macro that I recorded called "Refresh" which refreshes all queries in a workbook, and then execute your macro?

    Sorry to nit pick, but again thank you for your assistance

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome.

    so, it's a part of a larger procedure.

    because we cant see the entirety of the procedure, WAIT method may be used.

    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    [VBA]
    'This example pauses a running macro until 6:23 P.M. today.
    Application.Wait "18:23:00"
    [/VBA]

    [VBA]
    'This example pauses a running macro for approximately 10 seconds.
    newHour = Hour(Now())
    newMinute = Minute(Now())
    newSecond = Second(Now()) + 10
    waitTime = TimeSerial(newHour, newMinute, newSecond)
    Application.Wait waitTime
    [/VBA]

    or simply
    [VBA]
    Application.Wait(Now + TimeValue("0:00:10"))
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    Where in the code above would I post this statement:

    Application.Wait(Now + TimeValue("0:00:10"))

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    sorry for that.

    [vba]
    Public Sub Move_Worksheets()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim calc As Long
    Dim fName As String

    Application.Wait(Now + TimeValue("0:00:10"))

    calc = Application.Calculation
    fName = "C:\Test_Reports\" & "Working_" & Format(Date, "mmddyyyy")

    With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With
    [/vba]

    adjust the time needed for updationg the pivots...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    That worked to perfection. Thank you.

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome.

    pls mark the thread as SOLVED from thread tools.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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