Help with simplifying code

Snipes00

New Member
Joined
Aug 9, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hey All,

I have this code I compiled to help me copy over data from different workbooks. Now as you can see I only need data from certain columns but did not know how to write this more efficiently than I have here. So I have pretty much got 2 lines of code for every column I need to take data from. If anyone can help make this code better I would be very grateful. Thank you in advance.

VBA Code:
Option Explicit
Sub Declaration_BusinessActivities_OtherAssets_BaseStations()
Workbooks.Open ("C:\Users\ImaniS\OneDrive - Vodafone Group\Documents\Declaration Template\2022 Renewal Data\01.  Vodafone Information\1.5 Business Activities.xlsx")
    Dim lr1    As Long
    Dim lr2    As Long
    On Error Resume Next
    With Workbooks("1.5 Business Activities.xlsx").Sheets("Sheet1")
        lr1 = .Cells(Rows.Count, 1).End(xlUp).Row 'last used row in sheet Workbook
        .AutoFilterMode = False
        .Range("A1:BY" & lr1).AutoFilter Field:=1, Criteria1:="GR01" 'filter range on EVO Code
        lr2 = Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Cells(Rows.Count, 1).End(xlUp).Row + 1 'first unused row in sheet Declaration
        .Range("A9:A" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B10").PasteSpecial xlPasteAll 'Copy/Paste
        .Range("B9:B" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B11").PasteSpecial xlPasteAll
        .Range("C9:C" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B12").PasteSpecial xlPasteAll
        .Range("F9:F" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B13").PasteSpecial xlPasteAll
        .Range("H9:H" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B14").PasteSpecial xlPasteAll
        .Range("I9:I" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B15").PasteSpecial xlPasteAll
        .Range("J9:J" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B16").PasteSpecial xlPasteAll
        .Range("K9:K" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B19").PasteSpecial xlPasteAll
        .Range("L9:L" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B20").PasteSpecial xlPasteAll
        .Range("M9:M" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B21").PasteSpecial xlPasteAll
        .Range("O9:O" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B22").PasteSpecial xlPasteAll
        .Range("P9:P" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B23").PasteSpecial xlPasteAll
        .AutoFilterMode = False
    End With
    Application.CutCopyMode = False
Workbooks("1.5 Business Activities.xlsx").Close

Workbooks.Open ("C:\Users\ImaniS\OneDrive - Vodafone Group\Documents\Declaration Template\2022 Renewal Data\03. Property Damage & Business Interruption\3.3 Other Assets Declaration (inc Totals).xlsx")
    With Workbooks("3.3 Other Assets Declaration (inc Totals).xlsx").Sheets("Sheet1")
        .AutoFilterMode = False
        .Range("A1:BY" & lr1).AutoFilter Field:=1, Criteria1:="GR01" 'filter range on EVO Code
        .Range("N9:N" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("A27").PasteSpecial xlPasteValues  'Copy/Paste
        .Range("J9:J" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B27").PasteSpecial xlPasteValues
        .Range("L9:L" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("C27").PasteSpecial xlPasteValues
        .Range("S9:S" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("A36").PasteSpecial xlPasteValues
        .Range("U9:U" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B36").PasteSpecial xlPasteValues
        .Range("W9:W" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("C36").PasteSpecial xlPasteValues
        .AutoFilterMode = False
    End With
    Application.CutCopyMode = False
Workbooks("3.3 Other Assets Declaration (inc Totals).xlsx").Close

Workbooks.Open ("C:\Users\ImaniS\OneDrive - Vodafone Group\Documents\Declaration Template\2022 Renewal Data\03. Property Damage & Business Interruption\3.2 Base Station Declaration.xlsx")
    With Workbooks("3.2 Base Station Declaration.xlsx").Sheets("Sheet1")
        .AutoFilterMode = False
        .Range("A1:BY" & lr1).AutoFilter Field:=1, Criteria1:="GR01" 'filter range on EVO Code
        .Range("H9:H" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("A32").PasteSpecial xlPasteValues  'Copy/Paste
        .Range("J9:J" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B32").PasteSpecial xlPasteValues
        .Range("L9:L" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("C32").PasteSpecial xlPasteValues
        .Range("U9:U" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("D32").PasteSpecial xlPasteValues
        .Range("W9:W" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("E32").PasteSpecial xlPasteValues
        .Range("Y9:Y" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("F32").PasteSpecial xlPasteValues
        .AutoFilterMode = False
    End With
    Application.CutCopyMode = False
Workbooks("3.2 Base Station Declaration.xlsx").Close
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Here it is a bit shorter:

VBA Code:
Option Explicit

Sub Declaration_BusinessActivities_OtherAssets_BaseStations()
'
    Workbooks.Open ("C:\Users\ImaniS\OneDrive - Vodafone Group\Documents\Declaration Template\2022 Renewal Data\01.  Vodafone Information\1.5 Business Activities.xlsx")
'
    Dim ColumnCount     As Long
    Dim ColumnNumber    As Long
    Dim lr1             As Long, lr2    As Long
'
    On Error Resume Next
    With Workbooks("1.5 Business Activities.xlsx").Sheets("Sheet1")
        lr1 = .Cells(Rows.Count, 1).End(xlUp).Row 'last used row in sheet Workbook
'
        .AutoFilterMode = False
'
        .Range("A1:BY" & lr1).AutoFilter Field:=1, Criteria1:="GR01" 'filter range on EVO Code
'
        lr2 = Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Cells(Rows.Count, 1).End(xlUp).Row + 1 'first unused row in sheet Declaration
'
        For ColumnNumber = 1 To 3
            .Range(.Cells(9, ColumnNumber), .Cells(lr1, ColumnNumber)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B" & ColumnNumber + 9).PasteSpecial xlPasteAll 'Copy/Paste
        Next
'
        .Range("F9:F" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B13").PasteSpecial xlPasteAll
'
        For ColumnNumber = 8 To 10
            .Range(.Cells(9, 8), .Cells(lr1, 8)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B" & ColumnNumber + 6).PasteSpecial xlPasteAll 'Copy/Paste
        Next
'
        For ColumnNumber = 11 To 13
            .Range(.Cells(9, ColumnNumber), .Cells(lr1, ColumnNumber)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B" & ColumnNumber + 8).PasteSpecial xlPasteAll 'Copy/Paste
        Next
'
        For ColumnNumber = 15 To 16
            .Range(.Cells(9, ColumnNumber), .Cells(lr1, ColumnNumber)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B" & ColumnNumber + 7).PasteSpecial xlPasteAll 'Copy/Paste
        Next
'
        .AutoFilterMode = False
    End With
'
    Application.CutCopyMode = False
'
    Workbooks("1.5 Business Activities.xlsx").Close
'
'---------------------------------------------------------------------------------------------------------
'
    Workbooks.Open ("C:\Users\ImaniS\OneDrive - Vodafone Group\Documents\Declaration Template\2022 Renewal Data\03. Property Damage & Business Interruption\3.3 Other Assets Declaration (inc Totals).xlsx")
'
    With Workbooks("3.3 Other Assets Declaration (inc Totals).xlsx").Sheets("Sheet1")
        .AutoFilterMode = False
        .Range("A1:BY" & lr1).AutoFilter Field:=1, Criteria1:="GR01" 'filter range on EVO Code
'
        .Range("N9:N" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("A27").PasteSpecial xlPasteValues  'Copy/Paste
'
        .Range("J9:J" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("B27").PasteSpecial xlPasteValues
'
        .Range("L9:L" & lr1).SpecialCells(xlCellTypeVisible).Copy
        Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Range("C27").PasteSpecial xlPasteValues
'
        ColumnCount = 0
'
        For ColumnNumber = 19 To 23 Step 2
            ColumnCount = ColumnCount + 1
'
            .Range(.Cells(9, ColumnNumber), .Cells(lr1, ColumnNumber)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Cells(36, ColumnCount).PasteSpecial xlPasteValues
        Next
'
        .AutoFilterMode = False
    End With
'
    Application.CutCopyMode = False
'
    Workbooks("3.3 Other Assets Declaration (inc Totals).xlsx").Close
'
'---------------------------------------------------------------------------------------------------------
'
    Workbooks.Open ("C:\Users\ImaniS\OneDrive - Vodafone Group\Documents\Declaration Template\2022 Renewal Data\03. Property Damage & Business Interruption\3.2 Base Station Declaration.xlsx")
    With Workbooks("3.2 Base Station Declaration.xlsx").Sheets("Sheet1")
        .AutoFilterMode = False
        .Range("A1:BY" & lr1).AutoFilter Field:=1, Criteria1:="GR01" 'filter range on EVO Code
'
        ColumnCount = 0
'
        For ColumnNumber = 8 To 12 Step 2
            ColumnCount = ColumnCount + 1
'
            .Range(.Cells(9, ColumnNumber), .Cells(lr1, ColumnNumber)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Cells(32, ColumnCount).PasteSpecial xlPasteValues
        Next
'
        ColumnCount = 3
'
        For ColumnNumber = 21 To 25 Step 2
            ColumnCount = ColumnCount + 1
'
            .Range(.Cells(9, ColumnNumber), .Cells(lr1, ColumnNumber)).SpecialCells(xlCellTypeVisible).Copy
            Workbooks("Declaration_Template.xlsm").Sheets("TEMP").Cells(32, ColumnCount).PasteSpecial xlPasteValues
        Next
'
        .AutoFilterMode = False
    End With
'
    Application.CutCopyMode = False
'
    Workbooks("3.2 Base Station Declaration.xlsx").Close
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,548
Members
449,038
Latest member
Guest1337

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