Monicasinha
Board Regular
- Joined
- Dec 26, 2022
- Messages
- 51
- Office Version
- 365
- Platform
- Windows
Hi
I am using the below code for
a. Opening a workbook,
b. Doing three calculations in that workbook ( Sum of columns, Product of two columns and divide by 12, Product of two columns)
c. Copy certain columns from this workbook to active workbook.
It takes little more than a minute to run the code. Can something be done to make it faster?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub importDataFromAnotherWorkbook()
Dim ws As Worksheet
Dim filter As String
Dim targetWorkbook As Workbook, wb As Workbook
Dim Ret As Variant
Dim LR As Long
Const x As Long = 12
Dim cl As Range
Dim rng As Range, MyResultsRng(1) As Range
Dim destinationLastRow As Long, i As Long
Dim TotalCoverage As Double
Dim Totalhours As Double
Dim TotalRevenue As Double
Application.ScreenUpdating = False
---------------------------------------------------------------------------------------------
‘Get sheet
FileToOpen = Application.GetOpenFilename(filefilter:="Excelfiles(*.xlsx),*xls*")
Set Openbook = Application.Workbooks.Open(FileToOpen)
‘Get sum of Values from Column Z to Column EO in Column EY ( Row wise)
Const FirstCol As Long = 26 ' "Z"
Const LastCol As Long = 145 ' "EO"
destinationLastRow = Openbook.Sheets("SHEET 1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 13 To destinationLastRow
Set MyResultsRng(1) = Openbook.Sheets("SHEET 1").Range("EY" & i)
For Each Cell In MyResultsRng(1)
Set rng = Openbook.Sheets("SHEET 1").Range(Openbook.Sheets("SHEET 1").Cells(i, FirstCol), Openbook.Sheets("SHEET 1").Cells(i, LastCol))
TotalCoverage = Application.WorksheetFunction.Sum(rng.Value)
With Cell
.Value = TotalCoverage
End With
Next Cell
Next i
Openbook.Sheets("SHEET 1").Range("EY13:EY5000").Copy
Openbook.Sheets("SHEET 1").Range("EY13:EY5000").PasteSpecial xlPasteValues
--------------------------------------------------------------------------------------------------------------------
‘Get product of Values from Column EY and Column EQ divided by 12 in Column EZ ( row wise)
For i = 13 To destinationLastRow
Set MyResultsRng(1) = Openbook.Sheets("SHEET 1").Range("EZ" & i)
For Each Cell In MyResultsRng(1)
Totalhours = Openbook.Sheets("SHEET 1").Range("EY" & i) / 12 * Openbook.Sheets("SHEET 1").Range("EQ" & i)
With Cell
.Value = Totalhours
End With
Next Cell
Next i
Openbook.Sheets("SHEET 1").Range("EZ13:EZ5000").Copy
Openbook.Sheets("SHEET 1").Range("EZ13:EZ5000").PasteSpecial xlPasteValues
Application.CutCopyMode = False
------------------------------------------------------------------------------------------------------------------------------------------
‘Get product of Values in Column EZ and Column ET in Column FA
For i = 13 To destinationLastRow
Set MyResultsRng(1) = Openbook.Sheets("SHEET 1").Range("FA" & i)
For Each Cell In MyResultsRng(1)
TotalRevenue = Openbook.Sheets("SHEET 1").Range("ET" & i) * Openbook.Sheets("SHEET 1").Range("EZ" & i)
With Cell
.Value = TotalRevenue
End With
Next Cell
Next i
---------------------------------------------------------------
Copy data from some columns from this workbook ( “Sheet 1”) to the active workbook
Openbook.Sheets("SHEET 1").Range("FA13:FA5000").Copy
Openbook.Sheets("SHEET 1").Range("FA13:FA5000").PasteSpecial xlPasteValues
Application.CutCopyMode = False
Openbook.Sheets("SHEET 1").Range("C13:C5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("D33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("H13:H5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("K33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("M13:M5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("L33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("E13:E5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("M33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("D13:D5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("S33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("EZ13:EZ5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("F33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("FA13:FA5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("G33").PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Openbook.Close False
'End If
Application.ScreenUpdating = True
MsgBox ("Data imported.")
End Sub
I am using the below code for
a. Opening a workbook,
b. Doing three calculations in that workbook ( Sum of columns, Product of two columns and divide by 12, Product of two columns)
c. Copy certain columns from this workbook to active workbook.
It takes little more than a minute to run the code. Can something be done to make it faster?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub importDataFromAnotherWorkbook()
Dim ws As Worksheet
Dim filter As String
Dim targetWorkbook As Workbook, wb As Workbook
Dim Ret As Variant
Dim LR As Long
Const x As Long = 12
Dim cl As Range
Dim rng As Range, MyResultsRng(1) As Range
Dim destinationLastRow As Long, i As Long
Dim TotalCoverage As Double
Dim Totalhours As Double
Dim TotalRevenue As Double
Application.ScreenUpdating = False
---------------------------------------------------------------------------------------------
‘Get sheet
FileToOpen = Application.GetOpenFilename(filefilter:="Excelfiles(*.xlsx),*xls*")
Set Openbook = Application.Workbooks.Open(FileToOpen)
‘Get sum of Values from Column Z to Column EO in Column EY ( Row wise)
Const FirstCol As Long = 26 ' "Z"
Const LastCol As Long = 145 ' "EO"
destinationLastRow = Openbook.Sheets("SHEET 1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 13 To destinationLastRow
Set MyResultsRng(1) = Openbook.Sheets("SHEET 1").Range("EY" & i)
For Each Cell In MyResultsRng(1)
Set rng = Openbook.Sheets("SHEET 1").Range(Openbook.Sheets("SHEET 1").Cells(i, FirstCol), Openbook.Sheets("SHEET 1").Cells(i, LastCol))
TotalCoverage = Application.WorksheetFunction.Sum(rng.Value)
With Cell
.Value = TotalCoverage
End With
Next Cell
Next i
Openbook.Sheets("SHEET 1").Range("EY13:EY5000").Copy
Openbook.Sheets("SHEET 1").Range("EY13:EY5000").PasteSpecial xlPasteValues
--------------------------------------------------------------------------------------------------------------------
‘Get product of Values from Column EY and Column EQ divided by 12 in Column EZ ( row wise)
For i = 13 To destinationLastRow
Set MyResultsRng(1) = Openbook.Sheets("SHEET 1").Range("EZ" & i)
For Each Cell In MyResultsRng(1)
Totalhours = Openbook.Sheets("SHEET 1").Range("EY" & i) / 12 * Openbook.Sheets("SHEET 1").Range("EQ" & i)
With Cell
.Value = Totalhours
End With
Next Cell
Next i
Openbook.Sheets("SHEET 1").Range("EZ13:EZ5000").Copy
Openbook.Sheets("SHEET 1").Range("EZ13:EZ5000").PasteSpecial xlPasteValues
Application.CutCopyMode = False
------------------------------------------------------------------------------------------------------------------------------------------
‘Get product of Values in Column EZ and Column ET in Column FA
For i = 13 To destinationLastRow
Set MyResultsRng(1) = Openbook.Sheets("SHEET 1").Range("FA" & i)
For Each Cell In MyResultsRng(1)
TotalRevenue = Openbook.Sheets("SHEET 1").Range("ET" & i) * Openbook.Sheets("SHEET 1").Range("EZ" & i)
With Cell
.Value = TotalRevenue
End With
Next Cell
Next i
---------------------------------------------------------------
Copy data from some columns from this workbook ( “Sheet 1”) to the active workbook
Openbook.Sheets("SHEET 1").Range("FA13:FA5000").Copy
Openbook.Sheets("SHEET 1").Range("FA13:FA5000").PasteSpecial xlPasteValues
Application.CutCopyMode = False
Openbook.Sheets("SHEET 1").Range("C13:C5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("D33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("H13:H5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("K33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("M13:M5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("L33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("E13:E5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("M33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("D13:D5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("S33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("EZ13:EZ5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("F33").PasteSpecial xlPasteValues
Openbook.Sheets("SHEET 1").Range("FA13:FA5000").Copy
ThisWorkbook.Worksheets("ACN Solution").Range("G33").PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Openbook.Close False
'End If
Application.ScreenUpdating = True
MsgBox ("Data imported.")
End Sub