KillerDragonKC
New Member
- Joined
- Sep 9, 2015
- Messages
- 20
I first would like to thank all of the helpful people on this forum who were able to not only provide answers but also direct me to some very good resources, that will only guide me to better understand excel and use it more effectively.
Second, I would like to apologize to my math teachers of the past when the statement was made, "I'll never use this a+b= crud in the real world". Boy was I wrong.
I have created a script to pull in data from a text file and do a number of things. Which works like a charm with all of the help I have received.
I just wanted to post the code here to see if there was anything that I should change to make it cleaner or faster, that someone out there may see.
Second, I would like to apologize to my math teachers of the past when the statement was made, "I'll never use this a+b= crud in the real world". Boy was I wrong.
I have created a script to pull in data from a text file and do a number of things. Which works like a charm with all of the help I have received.
I just wanted to post the code here to see if there was anything that I should change to make it cleaner or faster, that someone out there may see.
Code:
Dim DataWS As Worksheet, SumWS As Worksheet, ImportWS As Worksheet
Dim today As String
Dim lr As Long, i As Long, lc As Long
Dim oFile As Variant
Dim WB As Workbook
Dim CatCELL As Range
Sub Pending_Adjustments()
MsgBox ("Please select pending adjustment report.")
oFile = Application.GetOpenFilename(FileFilter:="Text Files, *.txt", MultiSelect:=False)
If oFile = False Then
MsgBox ("No pending adjustment file selected. Import canceled.")
Exit Sub
Else
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set WB = ActiveWorkbook
Set DataWS = WB.Sheets("Data")
Set ImportWS = WB.Sheets("Import")
With DataWS.QueryTables.Add(Connection:="TEXT;" & oFile, Destination:=DataWS.Range("$A$1"))
.Name = "Pending_Adjustments"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 8
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1)
.TextFileFixedColumnWidths = Array(4, 2, 3, 3, 3, 20, 21, 8, 4, 3, 9, 10, 13, 2, 4, 17)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("Data").Select
'Filter out irrelevant data
lr = Range("A" & Rows.Count).End(xlUp).Row
For i = lr To 2 Step -1
If Not Range("A" & i).Value Like "90*" Then Rows(i).Delete
Next i
'
Range("Q1").Select
ActiveCell.FormulaR1C1 = "Current Adj Type"
Columns("L:L").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("L:L").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("L:L").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("L1").Select
ActiveCell.FormulaR1C1 = "DSO"
Range("M1").Select
ActiveCell.FormulaR1C1 = "T2BD BY END OF MONTH"
Range("N1").Select
ActiveCell.FormulaR1C1 = "ACCOUNT HAS PPBD"
Cells.Select
Cells.EntireColumn.AutoFit
Range("N3").Select
Columns("O:O").Select
Selection.NumberFormat = "0.00"
Range("A1").Select
Range("L2").Select
ActiveCell.FormulaR1C1 = "=TODAY()-RC[-1]"
Columns("L:L").Select
Selection.NumberFormat = "0"
Range("L2").Select
Selection.AutoFill Destination:=Range("L2:L" & lr)
'CALCULATING IF ADJ WILL T2BD BY END OF MONTH
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND(RC[-1]>(INDEX({365,90},MATCH(RC[-5],{220,221},0))-(Import!R3C5-TODAY())),RC[-1]<INDEX({365,90},MATCH(RC[-5],{220,221},0))),""YES"",""NO""),""NO"")"
Columns("M:M").Select
Selection.NumberFormat = "0"
Range("M2").Select
Selection.AutoFill Destination:=Range("M2:M" & lr)
Columns("M:M").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("M1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "T2BD BY END OF MONTH"
'END OF T2BD CALC
Range("L2:L260").Select
ActiveWindow.SmallScroll Down:=-12
Columns("L:L").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("L1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "DSO"
Columns("U:AI").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.ScrollColumn = 20
ActiveWindow.ScrollColumn = 18
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 15
ActiveWindow.ScrollColumn = 14
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Columns("T:T").Select
Selection.NumberFormat = "General"
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
'Delete blank cells after import and dso
Range("T2").Select
CurrentADJType
T2_BD
PP_BD
Formulas
Formulas2
ADJPVT
Sheets("Data").Select
ActiveSheet.UsedRange.Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
On Error Resume Next
'Range("A2:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'************** Delete import and save workbook ********
today = Year(Date) & "-" & Month(Date) & "-" & Day(Date)
ImportWS.Delete
Sheets("T2BD").Delete
Sheets("PPBD").Delete
Sheets("SUMMARY").Select
Rbco = Sheets("Data").Range("B3").Value
WB.SaveAs fileName:=WB.Path & "\RGN_ " & Rbco & "_Pending Adjustment Report " & today & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub CurrentADJType()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
For Each c In Sheets("Data").Range("T2:T" & lr)
Select Case True
Case (Range("H" & c.Row) = 220) And (Range("I" & c.Row) <> 23) And (Range("I" & c.Row) <> 73) And (Range("L" & c.Row) < 365)
c.Value = "SALES ADJ CASH APP"
Case (Range("H" & c.Row) = 220) And (Range("I" & c.Row) = 23) And (Range("L" & c.Row) < 365)
c.Value = "O2 CAP NON MCR"
Case (Range("H" & c.Row) = 220) And (Range("I" & c.Row) = 73) And (Range("L" & c.Row) < 365)
c.Value = "O2 CAP MCR"
Case (Range("H" & c.Row) = 221) And (Range("L" & c.Row) < 90)
c.Value = "SALES ACCOM MANUAL"
Case (Range("H" & c.Row) = 221) And (Range("L" & c.Row) >= 90)
c.Value = "PATIENT PAY BD NET"
Case (Range("H" & c.Row) = 225)
c.Value = "2 PCT SEQUESTRATION"
Case (Range("H" & c.Row) = 240)
c.Value = "REFUND"
Case (Range("H" & c.Row) = 220) And (Range("L" & c.Row) >= 365) And (Range("P" & c.Row) <> "0")
c.Value = "BD NET OTHER"
Case (Range("H" & c.Row) = 242) And (Range("P" & c.Row) <> "0")
c.Value = "BD NET OTHER"
Case (Range("H" & c.Row) = 220) And (Range("L" & c.Row) >= 365) And (Range("P" & c.Row) = "0")
c.Value = "PATIENT PAY BD NET"
Case (Range("H" & c.Row) = 242) And (Range("P" & c.Row) = "0")
c.Value = "PATIENT PAY BD NET"
Case (Range("H" & c.Row) = 245)
c.Value = "XFER"
Case (Range("H" & c.Row) = 246)
c.Value = "XFER"
Case (Range("H" & c.Row) = 247)
c.Value = "XFER"
Case Else: c.Value = "UNKNOWN"
End Select
Next c
DataWS.Cells.EntireColumn.AutoFit
deleteBlankRows
End Sub
'Begin to pull those records identifed as t2bd and create named range to be used to identify whole accounts as t2bd not just specific lines
Sub T2_BD()
Dim DataWS As Worksheet, SumWS As Worksheet, ImportWS As Worksheet, T2BD As Worksheet, PPBD As Worksheet
Dim today As String
Dim lr As Long, i As Long, lc As Long, lt As Long, lp As Long
Dim oFile As Variant
Dim WB As Workbook
Dim CatCELL As Range
ActiveWorkbook.Sheets.Add.Name = "T2BD"
Set DataWS = Sheets("Data")
Set T2BD = Sheets("T2BD")
lr = DataWS.Range("A1").End(xlUp).Row
lt = T2BD.Range("A1").End(xlUp).Row
With DataWS
.Rows("1:1").AutoFilter
.Range("$A$1:$T$" & lr).AutoFilter Field:=13, Criteria1:="=YES", Operator:=xlAnd
.Cells.SpecialCells(xlCellTypeVisible).Copy T2BD.Range("A1")
'.Rows("1").EntireRow.Hidden = True
'.Columns("A").SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilterMode = False
'.Rows("1").EntireRow.Hidden = False
'T2BD.Range("$A$1:$T$" & lt).RemoveDuplicates Columns:=5, Header:=xlYes
Sheets("T2BD").Select
Range("A1").Select
Range("A:D,F:L,O:T").Select
Range("O1").Activate
Selection.Delete Shift:=xlToLeft
Range("A1").Select
RMV_DUPE_CLMN_E
End With
T2BD.Cells.EntireColumn.AutoFit
deleteBlankRows
Sheets("Data").Select
Sheets("T2BD").Select
ActiveSheet.UsedRange.Select
Selection.Name = "T2BDLST"
End Sub
'Begin to pull those records identifed as ppbd and create named range to be used to identify whole accounts as t2bd not just specific lines
Sub PP_BD()
Dim DataWS As Worksheet, SumWS As Worksheet, ImportWS As Worksheet, T2BD As Worksheet, PPBD As Worksheet
Dim today As String
Dim lr As Long, i As Long, lc As Long, lp As Long, lt As Long
Dim oFile As Variant
Dim WB As Workbook
Dim CatCELL As Range
ActiveWorkbook.Sheets.Add.Name = "PPBD"
Set DataWS = Sheets("Data")
Set PPBD = Sheets("PPBD")
lr = DataWS.Range("A1").End(xlUp).Row
lp = PPBD.Range("A1").End(xlUp).Row
With DataWS
.Rows("1:1").AutoFilter
.Range("$A$1:$T$" & lr).AutoFilter Field:=20, Criteria1:="=PATIENT PAY BD NET", Operator:=xlAnd
.Cells.SpecialCells(xlCellTypeVisible).Copy PPBD.Range("A1")
'.Rows("1").EntireRow.Hidden = True
'.Columns("A").SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilterMode = False
'.Rows("1").EntireRow.Hidden = False
'PPBD.Range("$A$1:$T$" & lp).RemoveDuplicates Columns:=5, Header:=xlYes
Sheets("PPBD").Select
Range("A1").Select
Range("A:D,F:L,O:T").Select
Range("O1").Activate
Selection.Delete Shift:=xlToLeft
Range("A1").Select
RMV_DUPE_CLMN_E
End With
PPBD.Cells.EntireColumn.AutoFit
deleteBlankRows
Sheets("Data").Select
Sheets("PPBD").Select
PPBDY
ActiveSheet.UsedRange.Select
Selection.Name = "PPBDLST"
End Sub
Sub deleteBlankRows()
On Error Resume Next
Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
Sub RMV_DUPE_CLMN_E()
Const ccol As String = "A"
Dim dic As Object, a, u()
Dim r&, c&, i&, x&
Set dic = CreateObject("scripting.dictionary")
r = Cells.Find("*", , , , xlByRows, xlPrevious).Row
c = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
ReDim u(1 To r, 1 To 1)
a = Cells(1, ccol).Resize(r)
For i = 1 To r
dic(a(i, 1)) = dic(a(i, 1)) + 1
If dic(a(i, 1)) > 1 Then u(i, 1) = 1: x = x + 1
Next i
Cells(c + 1).Resize(r) = u
Cells(1).Resize(r, c + 1).Sort Cells(c + 1), Header:=xlNo
If x > 0 Then Cells(1).Resize(x, c + 1).Delete xlUp
End Sub
Sub PPBDY()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Sheets("PPBD").Select
Range("C2").Select
ActiveCell.FormulaR1C1 = "YES"
Columns("C:C").Select
Selection.NumberFormat = "@"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & lr)
Columns("C:C").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
End Sub
Sub Formulas()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Data").Select
'FORMULA FOR T2BD RESULTS TO ALL OF PATIENT NOT JUST ONE INVOICE
Range("M2").Select
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-8],T2BDLST,2,FALSE),""NO"")"
Columns("M:M").Select
Selection.NumberFormat = "@"
Range("M2").Select
Selection.AutoFill Destination:=Range("M2:M" & lr)
Columns("M:M").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'FORMULA FOR PPBD RESULT ON DATA TAB
'Range("N2").Select
'ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-9],PPBDLST,3,FALSE),""NO"")"
'Columns("N:N").Select
'Selection.NumberFormat = "@"
'Range("N2").Select
'Selection.AutoFill Destination:=Range("N2:N" & lr)
'Columns("N:N").Select
'Selection.Copy
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub Formulas2()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Data").Select
'FORMULA FOR PPBD RESULTS TO ALL OF PATIENT NOT JUST ONE INVOICE
Range("N2").Select
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-9],PPBDLST,3,FALSE),""NO"")"
Columns("N:N").Select
Selection.NumberFormat = "@"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N" & lr)
Columns("N:N").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'FORMULA FOR PPBD RESULT ON DATA TAB
'Range("N2").Select
'ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-9],PPBDLST,3,FALSE),""NO"")"
'Columns("N:N").Select
'Selection.NumberFormat = "@"
'Range("N2").Select
'Selection.AutoFill Destination:=Range("N2:N" & lr)
'Columns("N:N").Select
'Selection.Copy
'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub ADJPVT()
ActiveWorkbook.Sheets.Add.Name = "SUMMARY"
Sheets("Data").Select
ActiveSheet.UsedRange.Select
Selection.Name = "DATALST"
Sheets("SUMMARY").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"DATALST", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="SUMMARY!R1C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion14
Sheets("SUMMARY").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("T2BD BY END OF MONTH")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("ACCOUNT HAS PPBD")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Next Aprv")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Current Adj Type")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("P")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Amount"), "Sum of Amount", xlSum
Range("C1:H1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("C2:H2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("C3:H3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("C1:H1").Select
ActiveCell.FormulaR1C1 = _
"ACCOUNTS THAT HAVE INVOICES THAT WILL TRANSITION TO BAD DEBT BY END OF MONTH"
Range("C2:H2").Select
ActiveCell.FormulaR1C1 = _
"ACCOUNTS THAT HAVE INVOICES CURRENTLY CONSIDERED TO BE PATIENT PAY BAD DEBT"
Range("C3:H3").Select
ActiveCell.FormulaR1C1 = "USER ADJUSTMENTS ARE CURRENTLY PENDING TO"
Range("C4").Select
ActiveSheet.PivotTables("PivotTable1").PivotSelect "", xlDataAndLabel, True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub