haribole

New Member
Joined
Apr 10, 2018
Messages
19
Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim TotalTrans As Integer
Dim FoundRow As Range
Dim MyFolder, MyFile, MyFileName, MySheetName, ReportDate As String
Dim FileType As String
Dim MisFileName() As String, PaymentFileType() As String
Dim MisTrans() As Integer
Dim i, j, k, m As Integer
i = 2
j = 2
k = 1
m = 1
MyFolder = "D:\TotalCompare"
MyFile = Dir(MyFolder & "\*.txt")
Do While MyFile <> ""
    Workbooks.OpenText Filename:=MyFolder & "\" & MyFile, StartRow:=1, DataType:=xlDelimited, Comma:=True, Tab:=False, Semicolon:=False, Space:=False, Other:=False
       If Left(ActiveWorkbook.Name, 21) = "ReverseFile" Or Right(ActiveWorkbook.Name, 12) = "RES.txt" Then
           If Left(ActiveWorkbook.Name, 21) = "ReverseFile" Then
              MySheetName = Replace(ActiveWorkbook.Name, "txt", "")
              ReportDate = Right(Replace(MySheetName, ".", ""), 8)
              MyFileName = Replace((Replace(MySheetName, "_" & ReportDate & ".", "")), "ReverseFile", "")
              FileType = "M.I.S."
              TotalTrans = ActiveSheet.UsedRange.Rows.Count
            Else
              MyFileName = Replace(ActiveWorkbook.Name, ".RES.txt", "")
              FileType = "M.I.S."
              TotalTrans = ActiveSheet.UsedRange.Rows.Count
            End If
        ThisWorkbook.Worksheets(2).Activate
          ActiveSheet.Cells(i, 1).Value = i - 1
          Worksheets(2).Cells(i, 2).Value = MyFileName
          Worksheets(2).Cells(i, 3).Value = FileType
          Worksheets(2).Cells(i, 4).Value = TotalTrans
          i = i + 1
        Else
        MyFileName = Replace(ActiveWorkbook.Name, ".txt", "")
        TotalTrans = ActiveSheet.UsedRange.Rows.Count
          ThisWorkbook.Worksheets(1).Activate
          Worksheets(1).Cells(j, 1).Value = j - 1
          Worksheets(1).Cells(j, 2).Value = MyFileName
          Worksheets(1).Cells(j, 3).Value = "Source File"
          Worksheets(1).Cells(j, 4).Value = TotalTrans
          Worksheets(1).Cells(j, 6).Formula = ""="B" & j - "B" & j""
              'Here is the problem to insert formula
          j = j + 1
        End If
            For Each wb In Workbooks
              If Not wb.Name = ThisWorkbook.Name Then
                wb.Close SaveChanges:=False
              End If
            Next wb
     MyFile = Dir
  Loop
ThisWorkbook.Worksheets(2).Activate
ReDim MisFileName(1 To i - 2) As String
ReDim PaymentFileType(1 To i - 2) As String
ReDim MisTrans(1 To i - 2) As Integer
For k = 1 To i - 2 Step 1
MisFileName(k) = ActiveSheet.Cells(k + 1, 2)
PaymentFileType(k) = ActiveSheet.Cells(k + 1, 3)
MisTrans(k) = ActiveSheet.Cells(k + 1, 4)
Next k
ThisWorkbook.Worksheets(1).Activate
For m = 1 To i - 2 Step 1
  Set FoundRow = Worksheets(1).Range("B:B").Find(What:=MisFileName(m), SearchDirection:=xlNext, LookIn:=xlValues, lookat:=xlWhole)
   If FoundRow Is Nothing Then
     Worksheets(1).Cells(j, 1).Value = j
     Worksheets(1).Cells(j, 2).Value = MisFileName(m)
     Worksheets(1).Cells(j, 3).Value = PaymentFileType(m)
     Worksheets(1).Cells(j, 5).Value = MisTrans(m)
     Worksheets(1).Cells(j, 6).Formula = "="B" & j - "B" & j"
          j = j + 1
       Else
         ActiveSheet.Cells(FoundRow.Row, 5).Value = MisTrans(m)
    End If
Next m
Worksheets(1).Cells(j, 3) = "Total="
Worksheets(1).Cells(j, 4) = Application.WorksheetFunction.Sum(Range("D2:D" & j))
Worksheets(1).Cells(j, 5) = Application.WorksheetFunction.Sum(Range("E2:E" & j))
Worksheets(1).Cells(j, 6) = Application.WorksheetFunction.Sum(Range("F2:F" & j))
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about giving us a clue what you want? Subtracting the value in column B from the value in column B seems fairly pointless to me.
 
Upvote 0
Sorry it would be:
Worksheets(1).Cells(j, 6).Formula = "="B" & j - "C" & j"

Actually I want to subtract column C from Column B and insert the formula in Column D

Please help !!
 
Upvote 0
Column D is column 4, not 6. ;)

Code:
Worksheets(1).Cells(j, "D").FormulaR1C1 = "=RC2-RC3"

would be the simplest way.
 
Upvote 0
Thank you for your valuable solution !!

Is there any way to avoid "R1C1 Reference Style" ?

Please !!
 
Upvote 0
Yes, though I can't see any benefit:

Code:
Worksheets(1).Cells(j, "D").Formula = "=B" & j & "-C" & j
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,604
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top