copy spreadsheets from other workbook and sum the cell if criteria matched

willy292

New Member
Joined
Oct 2, 2017
Messages
2
I am pretty green in VBA, have 1 master spreadsheet namely CEO(i.e. provide an insight on every monday for last week performance , i want to verify if certain data is correct from 10 different spreadsheet (i.e. last weekend data, each date with 2 spreadsheet)
The spreadsheet path is saved in one of the spreadsheet namely link , CEO's link is in B3 while the weekly data is from B18 to B27
I dont know how to put those code into function so that I can call the function 10 times ? would any master please help me ? Thank you so much!

Sub handlesheet()
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim Sht As Worksheet
Dim i As Long
Dim lrow As Long
Dim b As Long

'initalize spreadsheet

Sheets("CEO").Cells.Clear
Sheets("A1").Cells.Clear
Sheets("A2").Cells.Clear
Sheets("B1").Cells.Clear
Sheets("B2").Cells.Clear
Sheets("C1").Cells.Clear
Sheets("C2").Cells.Clear
Sheets("D1").Cells.Clear
Sheets("D2").Cells.Clear
Sheets("E1").Cells.Clear
Sheets("E2").Cells.Clear

Set wbTarget = ThisWorkbook

wbPath = ThisWorkbook.Sheets("Link").Range("B3").Value

'Copy CEO data to the CEO tab
Set wbSource = Workbooks.Open(wbPath)
With wbSource.Sheets("Sheet1").UsedRange
'Now, paste to y worksheet:
wbTarget.Sheets("CEO").Range("A1").Resize( _
.Rows.Count, .Columns.Count) = .Value
End With
wbSource.Close

' how to rewrite with a sub function so that I can call 10 times for A1, A2,B1,B2,C1,C2,D1,D2,E1,E2
' copy the spreadsheet from source to A1 tab, After that , if any cell in column A (ie. from A6) is a number greater than 0 ,sum up the related cell in column D and 'put the total in another spreadsheet on the same workbook fro statistic use


'A1
wbPath = ThisWorkbook.Sheets("Link").Range("B18").Value

Filename = Dir(wbPath, vbNormal)
If Filename <> "" Then

Set wbSource = Workbooks.Open(wbPath)
With wbSource.Sheets("Sheet1").UsedRange
'Now, paste to y worksheet:
wbTarget.Sheets("A1").Range("A1").Resize( _
.Rows.Count, .Columns.Count) = .Value
End With
wbSource.Close

lrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To lrow
If IsNumeric(Cells(i, 1)) And Cells(i, 1) > 0 Then
Cells(i, 10).Value = Cells(i, 4).Value

End If

Next i

End If
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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