Created
July 30, 2015 01:45
-
-
Save timothyylim/ed0bfe94930db892cbb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub test() | |
Dim LastRow As Long | |
Dim ColumnName As String | |
Dim i As Integer | |
Dim Description As String | |
Description = "DE Acc. Desc." | |
HomeDR = FindColumnNumber("Home DR") | |
HomeCR = FindColumnNumber("Home CR") | |
DEAcc = FindColumnNumber(Description) | |
TotalColumn = HomeCR + 1 | |
If CheckIfColumnExists(Description) = True Then | |
With ActiveSheet | |
LastRow = .Cells(.Rows.Count, DEAcc).End(xlUp).Row | |
End With | |
x = 0 | |
For i = 1 To LastRow | |
currentValue = Cells(i, DEAcc) | |
nextRow = i + 1 | |
nextValue = Cells(nextRow, DEAcc) | |
If Not IsEmpty(Cells(i, DEAcc)) Then | |
If TypeName(Cells(i, HomeDR).Value) = "Integer" Or TypeName(Cells(i, HomeDR).Value) = "Double" Then | |
x = x + Cells(i, HomeDR).Value | |
If TypeName(x) <> "String" Then | |
If currentValue <> nextValue Then | |
Cells(i, TotalColumn).Value = x | |
Cells(i, TotalColumn).NumberFormat = "0.00" | |
x = 0 | |
End If | |
End If | |
End If | |
If IsEmpty(Cells(i, HomeDR)) Then | |
x = x - Cells(i, HomeCR).Value | |
If TypeName(x) <> "String" Then | |
If currentValue <> nextValue Then | |
Cells(i, TotalColumn).Value = x | |
Cells(i, TotalColumn).NumberFormat = "0.00" | |
x = 0 | |
End If | |
End If | |
End If | |
End If | |
Next i | |
End If | |
End Sub | |
Function CheckIfColumnExists(ColumnToFind As String) As Boolean | |
Dim i As Integer | |
Dim found As Boolean | |
found = False | |
For i = 1 To 500 | |
If Cells(1, i).Value = ColumnToFind Then | |
found = True | |
End If | |
Next i | |
If found = False Then | |
MsgBox "DE Acc. Desc. not found in first row" | |
quickTest = False | |
Else | |
quickTest = True | |
End If | |
End Function | |
Function FindColumnNumber(ColumnToFind As String) As Integer | |
Dim i As Integer | |
For i = 1 To 500 | |
If Cells(1, i).Value = ColumnToFind Then | |
FindColumnNumber = i | |
End If | |
Next i | |
End Function | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment