Macro Help

catguy

New Member
Joined
Jul 20, 2009
Messages
25
Hello, I inherited an Excel file that has a macro. Basically, you are supposed to click a button and it updates from two different files saved in a network drive. The updating works fine, however it is supposed to set a print area and perform a custom sort. The issue I have been asked to fix is: clicking the update button once updates the base data only, and clicking it a second time will set the print area. It does not however, perform the custom sort.
I've tested the macro and made a few changes, but I just can't seem to get the custom sort and set print area to complete with only one click of the button. I know I'm missing something, but my brain just isn't seeing something. Hope someone can help, this is getting frustrating. Thanks
Sub Update()
'
' Update Macro
'
Dim x As Long, y As Long
Dim rngPrintArea As Range

x = Cells(1, Columns.Count).End(xlToLeft).Column
y = Cells(Rows.Count, 1).End(xlUp).Row
Set rngPrintArea = Range(Cells(1, 1), Cells(y, x))

With Application
.ScreenUpdating = False: .DisplayAlerts = False
End With
With Range("A2:I199")
.ClearContents
End With
Range("A2").Select
Sheets("917_Confirm").Select
Selection.ClearContents
Range("A2").Select
Sheets("IQA_48HR").Select
Workbooks.Open Filename:="S:\Mfg_Quality\Daily_Metrics\Temp\LX03_temp.xlsx"
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("IQA_48HR.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
Sheets("917_Confirm").Select
Windows("LX03_temp.xlsx").Activate
ActiveWindow.Close
Workbooks.Open Filename:="S:\Mfg_Quality\Daily_Metrics\Temp\LT23_temp.xlsx"
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("IQA_48HR.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
Windows("LT23_temp.xlsx").Activate
ActiveWindow.Close
Sheets("IQA_48HR").Select

Range("T18").Select
ActiveSheet.PageSetup.PrintArea = rngPrintArea.Address
Range("T18").Select
ActiveWorkbook.Save
With Application
.ScreenUpdating = True: .DisplayAlerts = True
End With
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hia
This macro isn't actually sorting anything & whilst It's setting the print area, it's not telling it to print
 
Upvote 0
Sorry, I posted the wrong macro:

Sub Update()
'
' Update Macro
'
Dim x As Long, y As Long
Dim rngPrintArea As Range

x = Cells(1, Columns.Count).End(xlToLeft).Column
y = Cells(Rows.Count, 1).End(xlUp).Row
Set rngPrintArea = Range(Cells(1, 1), Cells(y, x))

With Application
.ScreenUpdating = False: .DisplayAlerts = False
End With
With Range("A2:I199")
.ClearContents
End With
Range("A2").Select
Sheets("917_Confirm").Select
Selection.ClearContents
Range("A2").Select
Sheets("IQA_48HR").Select
Workbooks.Open Filename:="S:\Mfg_Quality\Daily_Metrics\Temp\LX03_temp.xlsx"
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("IQA_48HR.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
Sheets("917_Confirm").Select
Windows("LX03_temp.xlsx").Activate
ActiveWindow.Close
Workbooks.Open Filename:="S:\Mfg_Quality\Daily_Metrics\Temp\LT23_temp.xlsx"
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("IQA_48HR.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
Windows("LT23_temp.xlsx").Activate
ActiveWindow.Close
Sheets("IQA_48HR").Select

Range("T18").Select
ActiveSheet.PageSetup.PrintArea = rngPrintArea.Address
Range("A1:O1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("IQA_48HR").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("IQA_48HR").Sort.SortFields.Add Key:=Range("N2:N45" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("IQA_48HR").Sort
.SetRange Range("A1:O45")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("T18").Select
ActiveWorkbook.Save
With Application
.ScreenUpdating = True: .DisplayAlerts = True
End With
End Sub
 
Upvote 0
I suspect that the problem is with all the .select & .Activate.
Try changing the top portion of code to this
Code:
With Sheets("IQA_48HR")
    x = .Cells(1, .Columns.Count).End(xlToLeft).Column
    y = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set rngPrintArea = .Range(.Cells(1, 1), .Cells(y, x))
End With
That should resolve the print area problem.
But I can't see why the sort is not happening.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top