Hi Guys
I am very stuck and I would appreciate your help..
I have 2 workbooks "Project Financial Summary" and "Cost Transactions By Cost Code and Project"
I have "Project Financial Summary"open
I need to:
-Find specific cell values in "Cost Transactions By Cost Code and Project" and inserted into specific cells in "Project Financial Summary"
-In particular: in "Cost Transactions By Cost Code and Project"
-every time the word "Description" is next to the word" Material", find the next word "Project Total" and return the value next to it to cell "E8"
in "Project Financial Summary"
-every time the word "Description" is next to the word" Expenses", find the next word "Project Total" and return the value next to it to cell "E9"
in "Project Financial Summary"
-Save "Project Financial Summary" and close "Cost Transactions By Cost Code and Project"
I here attach the two workbooks.
I know it's a bit complex but I hope someone can help me...also here attached my macro attempts..! (I know it's a mess..!!)
Thanks everybody..
Workbooks.Open Filename:= _
"C:\Users\RTR\Documents\Financial Summary\Cost Transactions By Cost Code and Project (Sage).xlsx"
ActiveWindow.Visible = False
Windows("Cost Transactions By Cost Code and Project (Sage).xlsx").Activate
' Declare variables used in this function
Dim foundIt As Boolean
Dim total As Double
Dim NoOfColumns As Integer
Dim NoOfRows As Integer
Dim SectionText As String
Dim wb As Workbooks
Dim ws As Worksheet
' Initialise variables
foundIt = False
total = 0
NoOfColumns = 15
NoOfRows = 300
SectionText = "Material"
' Loop through columns A to Z
For j = 1 To NoOfColumns
' Loop through rows 1 to 500
For i = 1 To NoOfRows
' If the current cell is "Description" and the cell 2 columns on is "Material"
If ThisWorkbook.ActiveSheet.Cells(i, j) = "Description" And ThisWorkbook.ActiveSheet.Cells(i, j + 2) = SectionText Then
' Flag that we have found the right section
foundIt = True
End If
' If we are in the right section and the current cell is the "project total"
If ThisWorkbook.ActiveSheet.Cells(i, j) = "Project Total" And foundIt = True Then
' Get the value from 2 cells on
total = ThisWorkbook.ActiveSheet.Cells(i, j + 2)
' debug show the captured value
MsgBox "Material £ " & total
' make sure we don't over write this number with data from the next section
' by killing the section found flag
foundIt = False
End If
Next
Next
' Initialise variables
foundIt = False
total = 0
NoOfColumns = 15
NoOfRows = 500
SectionText = "Expenses"
' Loop through columns A to Z
For j = 1 To NoOfColumns
' Loop through rows 1 to 500
For i = 1 To NoOfRows
' If the current cell is "Description" and the cell 2 columns on is "Expenses"
If ThisWorkbook.ActiveSheet.Cells(i, j) = "Description" And ThisWorkbook.ActiveSheet.Cells(i, j + 2) = SectionText Then
' Flag that we have found the right section
foundIt = True
End If
' If we are in the right section and the current cell is the "project total"
If ThisWorkbook.ActiveSheet.Cells(i, j) = "Project Total" And foundIt = True Then
' Get the value from 2 cells on
total = ThisWorkbook.ActiveSheet.Cells(i, j + 2)
' debug show the captured value
MsgBox "Expenses £ " & total
' make sure we don't over write this number with data from the next section
' by killing the section found flag
End If
Next
Next
' Columns("N:P").Select
With Selection
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Sheets("C:\Users\RTR\Documents\Financial Summary\Project Financial Summary.xlsx").Activate
Range("E8").Select
ActiveCell.FormulaR1C1 = _
"='[Cost Transactions By Cost Code and Project.xlsm]Cost Transactions By Cost Code'!R17C14"
Range("E9").Select
ActiveCell.FormulaR1C1 = _
"='[Cost Transactions By Cost Code and Project.xlsm]Cost Transactions By Cost Code'!R27C14"
Range("E10").Select
End Sub
Bookmarks