whazzzzzupp17
New Member
- Joined
- Jul 23, 2018
- Messages
- 21
I created the code below that works perfectly, however, I have to copy it 12 times for it to work properly.
It copies data from another spreadsheet within tabs named for each month (Jan through Dec).
Can someone help me make this into a For loop so I don't have so much duplication and confusion? My script works perfect. I just need it to cycle through sheets Jan thorugh Dec.
The issue I had when I created a For loop, was I couldn't paste the values correctly within the next cell "CountR". I didn't know how to figure that out.
It copies data from another spreadsheet within tabs named for each month (Jan through Dec).
Can someone help me make this into a For loop so I don't have so much duplication and confusion? My script works perfect. I just need it to cycle through sheets Jan thorugh Dec.
The issue I had when I created a For loop, was I couldn't paste the values correctly within the next cell "CountR". I didn't know how to figure that out.
Code:
Option Explicit
Sub CopyDatData()
Dim Sheet As Worksheet
Dim CountR As Long
Dim sourceOne As Workbook
Dim DataRange As Range
'Disable updating to increase performance
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Define the workbook locations
Set sourceOne = Workbooks.Open("E:\Downloads\Spreadsheet2.xlsx", True, True)
sourceOne.Activate
'Copy Jan data
Windows("Spreadsheet.xlsx").Activate
Sheets("Jan").Select
Set Sheet = ActiveSheet
'Using the find function to locate the last row. Searching for "Grand Total - "
CountR = Sheet.Cells.Find("Grand Total -", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'Define range length and size.
Set DataRange = Range("A6:U" & CountR)
'Select the range.
DataRange.Select
Selection.Copy
'Paste within Original spreadsheet within the DataPull tab
Windows("Spreadsheet1.xlsm").Activate
Sheets("DataPull").Select
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Range(Cells(4, 1), Cells(CountR - 2, 1)).Select
Selection.FormulaR1C1 = "January 2018"
'Close the source file
Application.CutCopyMode = False
sourceOne.Close False 'False does not save the source file.
Set sourceOne = Nothing
'Re-enable updating
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub