I have created a macro to move data from one worksheet to another. I would like to streamline the codes in red since the range of the rows is the same but different columns. Not sure how to shorten it. Please help!
I would like to copy E3:E19 from workbook 1 to F12:F28 workbook2.
E31: E43 from workbook 1 to F46:60 Workbook2
..........................................................
H3:H19 from workbook 1 to I12:I28 workbook2.
H31:H43 from workbook 1 to I46:I60 Workbook2
...AND ETC.
Here is the macro:
Option Explicit
Sub StoreMonthly()
Dim destinationWorkBook As Workbook, destinationWorkSheet As Worksheet, fromWorkSheet As Worksheet, fromWorkBook As Workbook, LastRow As Long
Dim directory As String, fromFileName As String, destinationFileName As String
Dim ws As Worksheet, blnOpened As Boolean
directory = "C:\XXXX"
fromFileName = "From.xls"
destinationFileName = "To.xls" 'change name here
'This is a seperator
If WbOpen(destinationFileName) = True Then
Set destinationWorkBook = Workbooks(destinationFileName)
blnOpened = False
Else
If Right(directory, 1) <> Application.PathSeparator Then
directory = directory & Application.PathSeparator
End If
Set destinationWorkBook = Workbooks.Open(directory & destinationFileName)
blnOpened = True
End If
Set destinationWorkSheet = destinationWorkBook.Sheets("Tosheet") 'change destination sheet name here
'End seperator
'This is a seperator
If WbOpen(fromFileName) = True Then
Set fromWorkBook = Workbooks(fromFileName)
blnOpened = False
Else
If Right(directory, 1) <> Application.PathSeparator Then
directory = directory & Application.PathSeparator
End If
Set fromWorkBook = Workbooks.Open(directory & fromFileName)
blnOpened = True
End If
Set fromWorkSheet = fromWorkBook.Sheets("Fromsheet") 'change destination sheet name here
'End seperator
Call ToggleEvents(False)
'This is a seperator
destinationWorkSheet.Range("F12:F28").Value = fromWorkSheet.Range("E3:E19").Value
destinationWorkSheet.Range("F31:F43").Value = fromWorkSheet.Range("E20:E32").Value
destinationWorkSheet.Range("F46:F60").Value = fromWorkSheet.Range("E33:E47").Value
destinationWorkSheet.Range("H12:H28").Value = fromWorkSheet.Range("I3:I19").Value
destinationWorkSheet.Range("H31:H43").Value = fromWorkSheet.Range("I20:I32").Value
destinationWorkSheet.Range("H46:H60").Value = fromWorkSheet.Range("I33:I47").Value
If blnOpened Then
destinationWorkBook.Close SaveChanges:=True
fromWorkBook.Close SaveChanges:=True
End If
Call ToggleEvents(True)
End Sub
Sub ToggleEvents(blnState As Boolean)
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub
Function WbOpen(wbName As String) As Boolean
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function
I would like to copy E3:E19 from workbook 1 to F12:F28 workbook2.
E31: E43 from workbook 1 to F46:60 Workbook2
..........................................................
H3:H19 from workbook 1 to I12:I28 workbook2.
H31:H43 from workbook 1 to I46:I60 Workbook2
...AND ETC.
Here is the macro:
Option Explicit
Sub StoreMonthly()
Dim destinationWorkBook As Workbook, destinationWorkSheet As Worksheet, fromWorkSheet As Worksheet, fromWorkBook As Workbook, LastRow As Long
Dim directory As String, fromFileName As String, destinationFileName As String
Dim ws As Worksheet, blnOpened As Boolean
directory = "C:\XXXX"
fromFileName = "From.xls"
destinationFileName = "To.xls" 'change name here
'This is a seperator
If WbOpen(destinationFileName) = True Then
Set destinationWorkBook = Workbooks(destinationFileName)
blnOpened = False
Else
If Right(directory, 1) <> Application.PathSeparator Then
directory = directory & Application.PathSeparator
End If
Set destinationWorkBook = Workbooks.Open(directory & destinationFileName)
blnOpened = True
End If
Set destinationWorkSheet = destinationWorkBook.Sheets("Tosheet") 'change destination sheet name here
'End seperator
'This is a seperator
If WbOpen(fromFileName) = True Then
Set fromWorkBook = Workbooks(fromFileName)
blnOpened = False
Else
If Right(directory, 1) <> Application.PathSeparator Then
directory = directory & Application.PathSeparator
End If
Set fromWorkBook = Workbooks.Open(directory & fromFileName)
blnOpened = True
End If
Set fromWorkSheet = fromWorkBook.Sheets("Fromsheet") 'change destination sheet name here
'End seperator
Call ToggleEvents(False)
'This is a seperator
destinationWorkSheet.Range("F12:F28").Value = fromWorkSheet.Range("E3:E19").Value
destinationWorkSheet.Range("F31:F43").Value = fromWorkSheet.Range("E20:E32").Value
destinationWorkSheet.Range("F46:F60").Value = fromWorkSheet.Range("E33:E47").Value
destinationWorkSheet.Range("H12:H28").Value = fromWorkSheet.Range("I3:I19").Value
destinationWorkSheet.Range("H31:H43").Value = fromWorkSheet.Range("I20:I32").Value
destinationWorkSheet.Range("H46:H60").Value = fromWorkSheet.Range("I33:I47").Value
If blnOpened Then
destinationWorkBook.Close SaveChanges:=True
fromWorkBook.Close SaveChanges:=True
End If
Call ToggleEvents(True)
End Sub
Sub ToggleEvents(blnState As Boolean)
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub
Function WbOpen(wbName As String) As Boolean
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function