Hi,
I am new to the forum and would appreciate your help with the following please. I am a novice VBA user and am having difficulty with the following code.
Basically I am trying to categorise expenses. I have 2 worksheets in a single workbook, the 1st ("CSVData") contains a list of all expense items,
the 2nd ("Data") contains the categories for the expenses to be posted against. In the example, I have only listed 2 of the categories.
The code attempts to search "CSVData" for any expense items against the 2 categories listed in "Data", and post the corresponding cost amounts under the 2 categories:
"RITCHIES" and "Telstra DDebit".
The code currently successfully searches the data in "CSVData" and pastes the relevant amounts into "Data", but it fails after the 1st successfull run.
It returns an error "Run-time error '9': Sbscript out of range"
Any assistance would be much appreciated. I have attached my working file.
Code:
I am new to the forum and would appreciate your help with the following please. I am a novice VBA user and am having difficulty with the following code.
Basically I am trying to categorise expenses. I have 2 worksheets in a single workbook, the 1st ("CSVData") contains a list of all expense items,
the 2nd ("Data") contains the categories for the expenses to be posted against. In the example, I have only listed 2 of the categories.
The code attempts to search "CSVData" for any expense items against the 2 categories listed in "Data", and post the corresponding cost amounts under the 2 categories:
"RITCHIES" and "Telstra DDebit".
The code currently successfully searches the data in "CSVData" and pastes the relevant amounts into "Data", but it fails after the 1st successfull run.
It returns an error "Run-time error '9': Sbscript out of range"
Any assistance would be much appreciated. I have attached my working file.
Code:
Code:
Sub CallFunction()
Call fncCategoriseCosts
End Sub
Code:
Public Function fncCategoriseCosts()
Sheets("CSVData").Select
Dim Cat(1 To 2) As String
Cat(1) = "RITCHIES"
Cat(2) = "Telstra DDebit"
Sheets("Data").Select
Dim Cat2(1 To 2) As String
Cat2(1) = ActiveSheet.Cells.Find("RITCHIES") 'Array("RITCHIES")
Cat2(2) = ActiveSheet.Cells.Find("Telstra DDebit") 'Array("Telstra DDebit")
Application.ScreenUpdating = False
Dim xlCalc As XlCalculation
xlCalc = Application.Calculation
'SET DATA RANGES
'________________
'CSVData" Tab
Sheets("CSVData").Select
Dim myRange1 As Range
Set myRange1 = Range("C2:C600")
Worksheets("Data").Select
Dim myRange2 As Range
Set myRange2 = Range("A1:B1")
'______________________________________________________________________________________________________________
'CATEGORISE EXPENDITURE
For Test = 1 To 30
Dim rCell As Range
'COPY DATA
Sheets("CSVData").Select
'_____________________________________________________________________________________________________
For Each rCell In myRange1
Do While rCell.Value Like "*" & Cat(Test) & "*"
If rCell.Value Like "*" & Cat(Test) & "*" Then
rCell.Activate
ActiveCell.Offset(0, -1).Select
Selection.Copy
Exit For
End If
Loop
Next rCell
'PASTE DATA
Sheets("DATA").Select
For Each rCell In myRange2
Do While rCell.Value = Cat2(Test)
If rCell.Value = Cat2(Test) Then
rCell.Activate
If IsEmpty(rCell) = False Then
ActiveCell.Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues
End If
Exit For
End If
Loop
Next rCell
Next Test
Application.CutCopyMode = False
End Function
Attachments
Last edited by a moderator: