Okay, so i'm bad at VBA, excuse the code if it's in poor form. I recorded this macro and have been editing down to what I have below. The code below works and produces what I want as far as processing the data. The problem is when I go to import it into our database the text file is not the exact same fixed-width that it was previously so is rejected by our database. Tab, comma delimitted etc. is not an option for the format of the text file.
All the code does below is delimit a fixed width text file so that Accounts in Column A, Debits in Column B and Credits in Column C. Occassionally I have negative debits or negative credits. What I need to do is if the debit is negative, move it over to the credit column and add it to the credit that is already there (if any). Same thing with negative credits.
Additionally, debits and credits start in the third row. Rows 1 and 2 have header tags in them and the very last row (which will vary day to day) has a footer in column A.
Also, I uploaded a sample of the original text file (I know debits don't equal credits, not the point just to show formatting).
All the code does below is delimit a fixed width text file so that Accounts in Column A, Debits in Column B and Credits in Column C. Occassionally I have negative debits or negative credits. What I need to do is if the debit is negative, move it over to the credit column and add it to the credit that is already there (if any). Same thing with negative credits.
Additionally, debits and credits start in the third row. Rows 1 and 2 have header tags in them and the very last row (which will vary day to day) has a footer in column A.
Also, I uploaded a sample of the original text file (I know debits don't equal credits, not the point just to show formatting).
Code:
Sub Fix_Neg_DR_CR()
'
' Fix_Neg_DR_CR Macro
' Fix Negative debits and credits in gl output file. Open text file in excel, run macro, save as text again. then import to meditech.
'
' Keyboard Shortcut: Ctrl+f
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(28, 1), Array(39, 1)), TrailingMinusNumbers _
:=True
Range("D3").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]<0,-RC[-1]+RC[-2],IF(RC[-2]<0,"""",RC[-2]))"
Range("E3").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]<0,-RC[-3]+RC[-2],IF(RC[-2]<0,"""",RC[-2]))"
Range("D3:E3").Select
Selection.AutoFill Destination:=Range("D3:E5000")
Columns("D:E").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("D2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
Columns("B:C").Select
Range("C1").Activate
Range("D2").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:C").Select
Range("C1").Activate
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
End Sub