+ Reply to Thread
Results 1 to 2 of 2

Print after using macro to fill worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2009
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2007
    Posts
    53

    Print after using macro to fill worksheet

    Hello, bit of a strange one. Hopfully easy for someone.
    I am using some code that automatically retrieves a list of data using a cell value as a search criteria and then it populates my worksheet with all the relevant data I require. The problem I have is when it comes to printing. Because the data has been added via code/macro the print are does not automatically expand to include all the data (eg if you look in page break view, it has not moved to include the data added as it would do if the data was added maually)?
    The data inserted could be upto 100 pages long, but when I try to print it the page break is only part down page 1 and I then need to manually drag it down????
    Is there a way around this in the code? Has it got something to do with EnableEvents?
    The code in question is:
    #If VBA7 Then
        Declare PtrSafe Function SetCurrentDirectoryA Lib _
        "kernel32" (ByVal lpPathName As String) As Long
    #Else
        Declare Function SetCurrentDirectoryA Lib _
        "kernel32" (ByVal lpPathName As String) As Long
    #End If
    
    
    Sub ChDirNet(szPath As String)
        SetCurrentDirectoryA szPath
    End Sub
    
    Sub Extract_RD_1()
        Dim MyPath As String
        Dim SourceRcount As Long, Fnum As Long
        Dim mybook As Workbook, BaseWks As Worksheet
        Dim sourceRange As Range, destrange As Range
        Dim rnum As Long, CalcMode As Long
        Dim SaveDriveDir As String
        Dim FName As Variant
        Dim W As Worksheet
        Dim r As Long
        Dim I As Long
        
        'Change ScreenUpdating, Calculation and EnableEvents
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
    I = 7
    
        SaveDriveDir = CurDir
        ChDirNet "S:\File Name\File Name\"
        FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
                                            MultiSelect:=True)
        If IsArray(FName) Then
            For Fnum = LBound(FName) To UBound(FName)
                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(FName(Fnum))
                On Error GoTo 0
                For Each W In mybook.Worksheets
                For r = 1 To WorksheetFunction.Max(W.Cells(Rows.Count, "A").End(xlUp).Row)
                If W.Cells(r, "A") = Range("A2") Then
                With ThisWorkbook.Worksheets(1)
                .Cells(3, 2).Value = W.Cells(r, "A").Value
                .Cells(I, 1).Value = W.Cells(r, "A").Offset(0, 3).Value
                .Cells(I, 5).Value = W.Cells(r, "A").Offset(0, 5).Value
                .Cells(I, 6).Value = W.Cells(r, "A").Offset(0, 6).Value
                End With
                I = I + 1
                End If
                Next r
                Next W
                    
       mybook.Close SaveChanges:=False
       Next Fnum
    '   Workbook.Worksheets.Columns.AutoFit
        End If
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
        ChDirNet SaveDriveDir
    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Print after using macro to fill worksheet

    Is this solved?

    If you solve a problem yourself before anyone else has responded, please take a moment to describe your solution, chances are some other member will benefit.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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