Optimizing Code

tqn626

New Member
Joined
Jul 1, 2013
Messages
49
I have this code i'm using to extract multiple tabs into one. Is there a better way to run this? Any help would be appreciated.

Code:
Sub PullData()
Dim i As Double
Dim wscount As Double
Dim imaxrow As String
Dim Datastart As String
Dim lowrow As String
Dim uprow As String
Dim x As Double
Dim Ship As Double
Dim ShipCount As String




wscount = ActiveWorkbook.Worksheets.Count


'Clear Current Data
Worksheets("Raw Data").Range("A2:R5000").Clear
'Range("a2:h20000").Select
'Selection.Clear
'Range("a2").Select


'Starting Worksheet to End
Datastart = Worksheets("Raw Data").Range("u6").Value


For i = Datastart To wscount


    'Rows of data to be extracted
    lowrow = Worksheets("raw data").Range("u7").Value
    uprow = Worksheets("raw data").Range("u8").Value
    'Use to offset columns to next dataset
        For Each e In Array(117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150)
        
        'Starting row to copy data
            For irow = lowrow To uprow
                If Worksheets(i).Visible Then
                
                    If Worksheets(i).Cells(irow, 1).Value = "" Or Worksheets(i).Cells(irow, 1).Value = 0 Then
                        'do nothing
                        
                        Else
                        If Worksheets(i).Cells(irow, 3).Find("Total") Is Nothing Then
                 
                            With Worksheets("Raw Data")
                     
                        
                             'Team
                            .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(1, 6).Address
                            'Sales Lead
                            .Cells(Rows.Count, "b").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(1, 3).Address
                            'Customer
                            .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(2, 3).Address
                            'Price Code
                            .Cells(Rows.Count, "d").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(2, 6).Address
                            'Classification
                            .Cells(Rows.Count, "E").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, 6).Address
                              'Category
                            .Cells(Rows.Count, "f").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, 2).Address
                              'Product Code
                            .Cells(Rows.Count, "g").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, 3).Address
    
                            '# Stores
                            .Cells(Rows.Count, "l").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, e + 36).Address
                             '2020 Forecast
                            .Cells(Rows.Count, "m").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, e + -77).Address
                            '2019 Cases
                            .Cells(Rows.Count, "n").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, e + -78).Address
                               '2020 AOP $
                            .Cells(Rows.Count, "o").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, e + -40).Address
                               '2019 $
                            .Cells(Rows.Count, "p").End(xlUp).Offset(1, 0) = "='" & Worksheets(i).Name & "'!" & Worksheets(i).Cells(irow, e + -41).Address
                            
                            
                             End With
                             'Next Ship
                             'Else
                        End If
                    End If
                'Worksheet not visble so do nothing
                
                End If
            Next irow
            
        Next e
    Next i
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I tried doing the cell value. But because of the offset, if a value is missing it throws all the numbers off.

Code:
                            .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = Worksheets(i).Cells(1, 6).Value
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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