Running MACROS in one WORKBOOK, COPY RESULTS to Another

Sphinx404

Board Regular
Joined
May 2, 2015
Messages
186
Office Version
  1. 365
Platform
  1. Windows
I'm at the end of my long process of VBA learning/Macro building. I am organizing the way my Macros work and in attempt at changing the look, layout and operation I've decided it would be best to have all my Macros stay in a locked workbook, but all my results return on a brand new workbook.

I suppose to start I need to create a macro that opens a new workbook and makes the users save it straight away. That's easy.

But the question is: what do I need to include in my code for the macro to return the results on my newly created workbook?

below is the first step in my macro code:

Code:
Sub Step1()
'this sub determines Airway Bill, sorting the AWB based on length


ActiveWorkbook.Sheets("HazShipper").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False


Dim mycell As Range


For Each mycell In Range("E2", Range("E" & Rows.Count).End(xlUp))


If Len(mycell) = 14 Then mycell.Offset(, 16).Value = Right(mycell.Value, 8)


If Len(mycell) = 21 Then mycell.Offset(, 16).Value = "UPS"


If Len(mycell) = 22 Then mycell.Offset(, 16).Value = "UPS"


If Len(mycell) <= 7 Then mycell.Offset(, 16).Value = "Analyze"


If Len(mycell) = 9 Then mycell.Offset(, 16).Value = "Unknown"


If Len(mycell) >= 23 Then mycell.Offset(, 16).Value = "Analyze"


Next mycell


Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub

Thank you VBA pros
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Or would it just be easier to run in the main workbook and copy all the results over to a new workbook, then resetting the master?

Trouble is, the guy I'm building this macro for wants to run each sub procedure individually... compare results... then move on to the next procedure. compare results.... etc. etc. etc.

The problem with this is that the macros return values in column Z:AV which means a lot of scrolling back and forth... it would be easier to have one workbook on one screen and the main workbook on the other screen (dual screen setup).
 
Upvote 0
Assuming your new workbook will have the same column and row layout as the original:
Code:
Sub Step1rev1()
'this sub determines Airway Bill, sorting the AWB based on length
Dim wb As Workbook, sh1 As Worksheet, sh2 As Worksheet
Dim mycell As Range
ActiveWorkbook.Sheets("HazShipper").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set sh1 = Sheets("HazShipper")
Set wb = Workbooks.Add
Set sh2 = wb.Sheets(1)
sh1.Rows(1).Copy sh2.Cells(1, 1)
    With sh1
        For Each mycell In .Range("E2", .Range("E" & Rows.Count).End(xlUp))
            If Len(mycell) = 14 Then mycell.Offset(, 16).Value = Right(mycell.Value, 8)
            If Len(mycell) = 21 Then mycell.Offset(, 16).Value = "UPS"
            If Len(mycell) = 22 Then mycell.Offset(, 16).Value = "UPS"
            If Len(mycell) <= 7 Then mycell.Offset(, 16).Value = "Analyze"
            If Len(mycell) = 9 Then mycell.Offset(, 16).Value = "Unknown"
            If Len(mycell) >= 23 Then mycell.Offset(, 16).Value = "Analyze"
        Next mycell
        .Range("U2", .Cells(Rows.Count, "U").End(xlUp)).Copy sh2.Cells(Rows.Count, "U").End(xlUp)(2)
        sh2.Range("U:U").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
This is untested.
 
Last edited:
Upvote 0
Assuming your new workbook will have the same column and row layout as the original:
Code:
Sub Step1rev1()
'this sub determines Airway Bill, sorting the AWB based on length
Dim wb As Workbook, sh1 As Worksheet, sh2 As Worksheet
Dim mycell As Range
ActiveWorkbook.Sheets("HazShipper").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set sh1 = Sheets("HazShipper")
Set wb = Workbooks.Add
Set sh2 = wb.Sheets(1)
sh1.Rows(1).Copy sh2.Cells(1, 1)
    With sh1
        For Each mycell In .Range("E2", .Range("E" & Rows.Count).End(xlUp))
            If Len(mycell) = 14 Then mycell.Offset(, 16).Value = Right(mycell.Value, 8)
            If Len(mycell) = 21 Then mycell.Offset(, 16).Value = "UPS"
            If Len(mycell) = 22 Then mycell.Offset(, 16).Value = "UPS"
            If Len(mycell) <= 7 Then mycell.Offset(, 16).Value = "Analyze"
            If Len(mycell) = 9 Then mycell.Offset(, 16).Value = "Unknown"
            If Len(mycell) >= 23 Then mycell.Offset(, 16).Value = "Analyze"
        Next mycell
[COLOR=#ff0000]        .Range("U2", .Cells(Rows.Count, "U").End(xlUp)).Copy sh2.Cells(Rows.Count, "U").End(xlUp)(2)
        sh2.Range("U:U").SpecialCells(xlCellTypeBlanks).EntireRow.Delete[/COLOR]
    End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
This is untested.


Worked great. Asked me to debug the highlighted text above, but it was an easy fix. The way you set this up gave me a few new ideas. THANK YOU! Until next time....
 
Last edited:
Upvote 0
Worked great. Asked me to debug the highlighted text above, but it was an easy fix. The way you set this up gave me a few new ideas. THANK YOU! Until next time....
You're welcome.
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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