I just wanted to thank you guys, especially Andrew, who has steered me regarding using .Select. I took this code,
Sub IPIupdate()
Application.ScreenUpdating = False
Dim CanName As String, fName As String
Dim numCount As Long, numCount2 As Long, mthCount As Integer, iCount2 As Long
Dim rngName As Range, dtRng As Range, cpyRng1 As Range, cpyRng2 As Range
mthCount = 12
Do Until fName <> ""
fName = InputBox("Enter the source file name", "Source File Name")
Loop
OpenFile (fName)
Windows("IndustryPriceIndex.xls").Activate
Application.Worksheets("Mthly").Select
For Each iCount In Range("Cansim")
numCount = 0
CanName = iCount.Value
Windows(fName).Activate
For iCount2 = 1 To Sheets.Count
With Sheets(iCount2)
Sheets(iCount2).Select
Range("A1:Q80").Select
Set rngName = Cells.Find(what:=iCount)
If Not rngName Is Nothing Then
rngName.Select
ActiveCell.Offset(1, 0).End(xlToRight).End(xlDown).Select
Do While ActiveCell = ".."
ActiveCell.Offset(0, -1).Select
numCount = numCount + 1
Loop
numCount2 = mthCount - numCount
ActiveCell.Offset(0, -numCount2).Resize(, numCount2 + 1).Select
Set cpyRng1 = Selection
Windows("IndustryPriceIndex.xls").Activate
iCount.End(xlToRight).Select
ActiveCell.Offset(0, -numCount2 + 1).Resize(, numCount2 + 1).Select
Set cpyRng2 = Selection
cpyRng2.Cells.Value = cpyRng1.Cells.Value
End If
If Not rngName Is Nothing Then Exit For
End With
Next iCount2
Next iCount
Application.ScreenUpdating = True
End Sub
and changed it to this code,
Option Explicit
Sub IndPrcIndex()
Application.ScreenUpdating = False
Dim fName As String
Dim numCount1 As Integer, iCount As Variant, dataFind As Variant
Dim rngName As Range
Dim dFind1 As Range, dFind2 As Range, dSelect1 As Range, cell As Range
Do Until fName <> "" ''''Prompt user for the name of the source file to update from''''
fName = InputBox("Enter the source file name", "Source File Name")
Loop
OpenFile fName ''''Open the file''''
For Each cell In Application.Workbooks("IndustryPriceIndex.xls").Worksheets("Mthly").Range("Cansim") ''''Loop through each cell in Range("Cansim")''''
For iCount = 1 To Windows(fName).Application.Sheets.Count ''''Required to search each sheet to find the Cansim number''''
With Application.Workbooks(fName).Sheets(iCount).Range("A1:Q80") ''''The range from the source to search in''''
Set rngName = Application.Workbooks(fName).Sheets(iCount).Cells.Find(cell) ''''Set the Range if the Cansim number is found''''
If Not rngName Is Nothing Then ''''If the Cansim number is found, perform the following actions''''
'MsgBox (cell) ''''This is for testing purposes only''''
Set dFind1 = rngName.Offset(1, 0).End(xlToRight).End(xlDown)
numCount1 = 0
Set dFind2 = dFind1.Offset(0, -12).Resize(, 12) ''''Select the data range''''
For Each dataFind In dFind2 ''''Find the data to be transferred''''
If dataFind.Value <> ".." Then
numCount1 = numCount1 + 1 ''''Count each cell that has data in it''''
ElseIf dataFind.Value = ".." Then
Set dSelect1 = dataFind.Offset(0, -numCount1).Resize(, numCount1) ''''Set new range with only data values''''
With Application.Workbooks("IndustryPriceIndex.xls").Worksheets("Mthly")
cell.End(xlToRight).Offset(0, -((numCount1) - 2)).Resize(, numCount1).Cells.Value = dSelect1.Cells.Value ''''set position to add values into''''
End With
End If
If dataFind.Value = ".." Then Exit For
Next dataFind
End If
If Not rngName Is Nothing Then Exit For
End With
Next iCount
Next cell
Application.ScreenUpdating = True
End Sub
It was not easy and took me a fair bit of time to do but I'm glad I did it because I learned a lot while fighting throught the code to make it work. It also reduced the total time to update by about 35 seconds (which seems like forever when you are waiting for code to finish).
Bookmarks