antman2988
Board Regular
- Joined
- Jun 28, 2018
- Messages
- 78
Hello! For some reason, my code below isn't updating the range with the formula specified. I've tried
, but I still get the same result. Please help!
VBA Code:
.Formula
VBA Code:
Sub UpdateCrosswalkData()
Application.ScreenUpdating = False
Application.Calculation = xlManual
Dim book As Workbook
Dim ws As Worksheet
Dim closedBook As Workbook
Set book = ActiveWorkbook
Set ws = book.Worksheets(1)
Set closedBook = Workbooks.Open("c:\\TEMPLATE.xlsx")
closedBook.Worksheets(1).Copy After:=book.Sheets(2)
closedBook.Close SaveChanges:=False
'Declare constants to search for
Const searchProgName As String = "Program Name"
Const searchProj As String = "Project"
Call UpdateColumns(searchProj, searchProgName, ws)
ws.Activate
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
Sub UpdateColumns(ByVal proj As String, ByVal progName As String, ByVal ws As Worksheet)
Dim foundprog As Range
Dim foundproj As Range
Dim col As Long
Dim row As Long
Dim colProj As String
Dim rowProj As Long
Dim i As Range
Dim lastRow As Long
Dim masterSheet As String
'Attempt to find string values in table header row, get column of found cell
On Error Resume Next
Set foundprog = ws.Rows(1).Find(progName, LookAt:=xlWhole)
col = foundprog.Column
row = foundprog.row
Set foundproj = ws.Rows(1).Find(proj, LookAt:=xlWhole)
colProj = Split(foundproj.Address(1, 0), "$")(0)
rowProj = foundproj.row
On Error GoTo 0
masterSheet = INDEXSheetName()
lastRow = ws.Cells.Find(What:="*", _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).row
For Each i In Range(Cells(row + 1, col), Cells(lastRow, col)).Cells
i.Value = "=INDEX('" & masterSheet & "'!$P:$P,MATCH('" & ws.Name & "'!" & colProj & i.row & ",'" & masterSheet & "'!$B:$B,0))"
Next i
End Sub
Function INDEXSheetName()
Dim sheet As Worksheet
'Loop through sheets to find project master sheet
For Each sheet In ActiveWorkbook.Worksheets
If sheet.Name Like "*Project Master*" Then
INDEXSheetName = sheet.Name
Exit For
End If
Next sheet
End Function
Last edited: