Hi Berries
See if this code solves the problem you were having and if it satisfies regarding the file name in column C
Sub ForumAnswer()
Dim vWks As Variant
Dim LR As Long
'Full process is only run if Master Schedule is not open
On Error GoTo ErrorHandler
'Request user close Master Schedule if it is open
Windows("Master Direct Cost Schedules (Abridged version for testing).xls").Activate
MsgBox "Please close Master Direct Cost Schedules before running this update"
Exit Sub
ErrorHandler:
'Open materials schedules
Application.ScreenUpdating = False
Workbooks.Open "C:\Documents and Settings\tony.perry\Desktop\Master Direct Cost Schedules (Abridged version for testing).xls", _
ReadOnly:=True, UpdateLinks:=False
' Workbooks.Open "C:\Documents and Settings\Administrator\Desktop\Berries\Test macro\Master Direct Cost Schedules (Abridged version for testing).xls", _
' ReadOnly:=True, UpdateLinks:=False
For Each vWks In Array("A category materials 1", "B category materials 1", "C category materials 1", _
"D category materials")
'Select 1st sheet required
'//////////Can we change the selected sheet once process has looped through all columns??//////////
Sheets(vWks).Select
ActiveSheet.Unprotect Password:="Secret"
lastcol = Worksheets(vWks).Range("J1:IV1").Cells.SpecialCells(xlCellTypeConstants).Count
LastRow = Cells(65536, 2).End(xlUp).Row
colcnt = lastcol + 10 - 1
'This is the 1st column containing data.
'**********This reference needs to move across to next column with each loop**********
' Sheets(vWks).Range("J1").Select
For i = 10 To colcnt
'This part of code to loop as it is
Cells(4, i).Resize(LastRow, 1).AutoFilter Field:=1, Criteria1:="<>"
LR = Range("B" & Rows.Count).End(xlUp).Row
If LR = 4 Then
MsgBox "You have no Data in Column" & i
Else
Cells(1, i).Copy
x = ActiveSheet.Name
ThisWorkbook.Activate
Range("Offset(LastAnalysisCode, 1,0,1,1)").PasteSpecial
ActiveCell.Offset(0, 2).Value = x
Application.CutCopyMode = False
Windows("Master Direct Cost Schedules (Abridged version for testing).xls").Activate
'++++++++++This reference needs to move across to next column with each loop++++++++++
'Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:="<>"
'Balance of code to loop as it is
Range("B5:B" & LR).Copy
ThisWorkbook.Activate
Range("Offset(EndOflist, 1,1,1,1)").PasteSpecial
Range("LastAnalysisCode").Copy
Range("CodeRequired").PasteSpecial
End If
Windows("Master Direct Cost Schedules (Abridged version for testing).xls").Activate
ActiveSheet.AutoFilterMode = False
'Process should revert back to section commencing with ********** comment
'until it has processed the last column containing data in row 1
'and then (if possible) it should select the next sheet to begin loop
Next i
Next vWks
ThisWorkbook.Activate
Call FillColBlanks_Offset
Application.ScreenUpdating = True
End Sub
Sub FillColBlanks_Offset()
'by Rick Rothstein 2009-10-24
'fill blank cells in column with value above
'http://www.contextures.com/xlDataEntry02.html
Dim Area As Range, LastRow As Long
On Error Resume Next
LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, _
LookIn:=xlFormulas).Row
For Each Area In ActiveCell.EntireColumn(3).Resize(LastRow). _
SpecialCells(xlCellTypeBlanks).Areas
Area.Value = Area(1).Offset(-1).Value
Next
End Sub
Let me know of issues.
John
Bookmarks