I have dollar amounts sorted by month that I want to transfer from sheet entry
to sheet actuals
. I only want to do it for a specified month prompted from user. Each dollar amount has a corresponding cost center/ID and description in separate columns. I want to copy the amounts and description from entry
to the row in actuals
matching the same ID.
Sometimes, multiple amounts share the same ID so I need to place them one below the other.
Sub month()
Dim actualsWS As Worksheet
Dim dataWS As Worksheet
Dim answer As String
Dim month As String
Dim loc As Range
Dim start As Integer
Dim rowCol As Variant
Dim dropRow As Integer
Dim dropCol As Integer
Dim locActual As Range
'store worksheets into variables
Set actualsWS = ThisWorkbook.Worksheets("Actuals by Month")
Set dataWS = ThisWorkbook.Worksheets("FAS Data Entry")
MsgBox ("If macro dies, it's likely a missing cost center.") 'warning for user
answer = InputBox(prompt:="What month?") 'prompt user for current month
'set month from user input
Select Case answer
Case 1
month = "January"
'dropCol = 9
Case 2
month = "February"
'dropCol = 10
Case 3
month = "March"
'dropCol = 11
Case 4
month = "April"
'dropCol = 12
Case 5
month = "May"
'dropCol = 13
Case 6
month = "June"
'dropCol = 14
Case 7
month = "July"
'dropCol = 15
Case 8
month = "August"
'dropCol = 16
Case 9
month = "September"
'dropCol = 17
Case 10
month = "October"
'dropCol = 18
Case 11
month = "November"
'dropCol = 19
Case 12
month = "December"
'dropCol = 20
Case Else
MsgBox ("Bad month. Exiting...")
Exit Sub
End Select
Dim ccArray(99) 'cost center
Dim amountArray(99) As Currency 'amount
Dim descriptArray(99) 'description
Dim ccArrayLength As Integer
With dataWS
'locate starting range for current month
Set loc = .Cells.Find(month)
'check previous row for carried over month (revising asset) Will break if 2 or more
If loc.Offset(-1, 0) = "" Then 'if previous row is blank
start = loc.row 'starting row is same row as found location above
Else
start = loc.row - 1 'else it's 1 row above
End If
i = 0
'store data into arrays
Do While .Cells(start + i, 1) <> "" 'loop through column A while there is a month present (not blank)
ccArray(i) = .Cells(start + i, 11).Value 'store cost center
amountArray(i) = .Cells(start + i, 13).Value 'store amount
descriptArray(i) = .Cells(start + i, 9).Value 'store description
i = i + 1
Loop
End With
ccArrayLength = Application.Count(ccArray) 'store number of items
With actualsWS
.Columns("V:W").ClearContents 'clear previous data
'hide unnecessary months
If month = "January" Or month = "February" Or month = "March" Or month = "April" Then
.Columns("M:Q").EntireColumn.Hidden = True
End If
'drop data into respective cells onto "actuals by month" tab
For x = 0 To ccArrayLength - 1
On Error Resume Next
Set locActual = .Cells.Find(ccArray(x)) 'find cost center location
If locActual = 0 Then GoTo died 'not found
'Debug.Print locActual
'rowCol = locActual.Address
dropRow = .Range(locActual.Address).row 'store row of cost center
'for loop to find first empty cell to drop into
For y = 0 To 99 'no cost center should have over 99 rows
If .Cells(dropRow + y, 22).Value = "" Then 'column v blank
.Cells(dropRow + y, 22).Value = amountArray(x) 'drop amount
.Cells(dropRow + y, 22).Offset(0, 1).Value = descriptArray(x) 'drop description on row below
Exit For
End If
Next
Next
.Cells(8, 22).Select 'bring cursor to top
End With
died:
'quit before msgbox if on last item
If x = ccArrayLength Then Exit Sub
MsgBox ccArray(x) & " not found." 'cost center not found
Exit Sub
End Sub