I have some VBA code which goes through a couple of thousand lines of data, performs a SUMIF and then supposedly performs a "Calculate Now". It runs fine in the VBA, my problem is that in the output, the format is coming up as "General" and no matter how many times i manually click "Calculate Now" it still shows 0. I have to go into the cell and then click out of it for it to realise that it is a number/formula and behave that way. Then when i F9 the sheet, it recalcs just fine.
Any ideas why that happens?
The VBA code I am using is below:
Sub MySub()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim WorkbookPathName As String
Dim wb As Workbook
Dim StrDate As String
StrDate = Format(Range("G6"), "dd-mm-yyyy")
Set wb = Workbooks.Add
WorkbookPathName = "MyPath" & StrDate
wb.SaveAs Filename:=WorkbookPathName, _
FileFormat:=51
wb.Activate
Dim FileSys As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String
Dim dteFile As Date
Const myDir As String = "MyDir"
'set up filesys objects
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)
Set FileSysL = New FileSystemObject
Set myFolderL = FileSys.GetFolder(myDir)
'loop through each file and get date last modified. If largest date then store Filename
dteFile = Workbooks("Myworkbook.xlsm").Sheets("Start").Range("G6").Value
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename = objFile.Name
End If
Next objFile
Workbooks.Open myDir & "\" & strFilename, ReadOnly:=True
Workbooks(strFilename).Activate
Range("A1:CA1").AutoFilter Field:=32, Criteria1:=Array( _
"filter for some text which is irrelevant to the point here"), Operator:=xlFilterValues
Range("A1:CA1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
wb.Activate
Range("A1").Select
ActiveSheet.Paste
Workbooks(strFilename).Activate
ThisWorkbook.Saved = True
ActiveWorkbook.Close
wb.Activate
Sheets("Sheet1").Name = "Data"
Application.Calculation = xlCalculationAutomatic
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Country"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[2]="""","""",LEFT(RC[28],2))"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow)
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.FormulaR1C1 = "lvl"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[3]="""","""", CONCATENATE(RC[29]&RC[41]))"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow)
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Lvl Net"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=SUMIF(C[1],RC[1],C[13])/COUNTIF(C[1],RC[1])"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow)
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "Final"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[2]>0,""Buy"",""Sell"")"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A" & LastRow)
Const sName As String = "Summary" ' Source Worksheet Name
Const cName As String = "Data" ' Criteria Worksheet Name
Const dColTitle As String = "My Column"
Const dCol As String = "A"
Dim dws As Worksheet: Set dws = ActiveSheet
dws.Columns(dCol).Insert xlToRight, xlFormatFromLeftOrAbove
With dws.Columns(dCol)
.Cells(1).Value = dColTitle
.Cells(2).Resize(LastRow - 1).FormulaR1C1 = _
"=SUMIF('" & sName & "'!C[19],'" & cName & "'!RC[32],'" _
& sName & "'!C)"
End With