OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for everyone's help. I will post feedback on any possible solutions given.
I'm having a bit of trouble with some code I put together and I already have had some help with it.
First Issue:
The line within the following code is giving me a run time error, and when I hit debug it seems to complete, but it won't run the rest of the code.
Error: "Run-time error '1004': Application-defined or object-defined error"
Code of Line with Error:
Second Issue:
If the value "NonDisc. CFAnnual(M$)" appears in any cell within column R, and or the value " CumDisc.CF (M$) " appears in any cell within column S, I want to clear those cells, but nothing happens. Those cells are a part of a data set which come from a database program so the formatting is an issue. It has some hard returns, so I though maybe if I could just search for the first few letters such as Non Disc. versus "NonDisc. CFAnnual(M$)" and Cum versus " CumDisc.CF (M$) ", but with what I found on the interent and within this website, I could not piece anything together to work. Here is that code with that error:
Code in it's Entirety
Once again thanks and I look forward to anyone's help.
I'm having a bit of trouble with some code I put together and I already have had some help with it.
First Issue:
The line within the following code is giving me a run time error, and when I hit debug it seems to complete, but it won't run the rest of the code.
Error: "Run-time error '1004': Application-defined or object-defined error"
Code of Line with Error:
Code:
If Cells(i, 18).Value = "Total" And Cells(i - 1, 21) <> "" Then
Second Issue:
If the value "NonDisc. CFAnnual(M$)" appears in any cell within column R, and or the value " CumDisc.CF (M$) " appears in any cell within column S, I want to clear those cells, but nothing happens. Those cells are a part of a data set which come from a database program so the formatting is an issue. It has some hard returns, so I though maybe if I could just search for the first few letters such as Non Disc. versus "NonDisc. CFAnnual(M$)" and Cum versus " CumDisc.CF (M$) ", but with what I found on the interent and within this website, I could not piece anything together to work. Here is that code with that error:
Code:
If Cells(i, 18).Value = "NonDisc. CFAnnual(M$)" And Cells(i, 2).Value = "CumDisc.CF M$)" Then
Cells(i, 18).Value = "" And Cells(i, 19).Value = ""
Code in it's Entirety
Code:
Sub Total()
'Activate the sheet
Worksheets("Data.Raw.2").Activate
'Find the last row.
Dim LastRow As Long
'LastRow = Cells(Rows.Count, 18).End(xlUp).Row
LastRow = Cells.Find(What:="*", _
after:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Find the last column.
Dim LastColumn As Long
LastColumn = Cells.Find(What:="*", _
after:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
'Fix Total Line within the Data Set
Dim i As Long
For i = LastRow To 1 Step -1
'Fixes total line if it is offset by one row
If Cells(i, 18).Value = "Total" And Cells(i - 1, 21) <> "" Then
Range("U" & i - 1, Cells(i - 1, LastColumn)).Cut
Range("U" & i, Cells(i, LastColumn)).Select
ActiveSheet.Paste
End If
Next i
'For i = LastRow To 1 Step -1
If Cells(i, 18).Value = "NonDisc. CFAnnual(M$)" And Cells(i, 2).Value = "CumDisc.CF M$)" Then
Cells(i, 18).Value = "" And Cells(i, 19).Value = ""
End If
Next i
'Move Total Line within the Data Set
For i = LastRow To 1 Step -1
If LCase(Cells(i, 18).Value) = "total" Then
Range("R" & i, Cells(i, LastColumn)).Cut
Range("A" & i + 1).Insert xlShiftDown
End If
Next i
End Sub
Once again thanks and I look forward to anyone's help.