DragonPrinces
New Member
- Joined
- Apr 30, 2013
- Messages
- 32
Hi, I have been working on an automation macro but i have basically spliced this together from all over the place and wanted to ask if anyone here can guide me as to how to make it more efficient, remove unneeded steps etc.
The basic idea with this macro is in columns O and P i am pulling data from another sheet if C meets certain criteria. then in D E G J i am able to populate these based on if C meets a criteria and I4 is a specific variable.
I am then using the macro to copy paste all these formulas to the respective columns ther recopy pate as values then because the "" in my IFs are not real blanks (i need true empty cells for another checking formula) i use a combination of TextToColumn (if there is something in the column) and ClearContents to truly empty the cells.
so yea i am just kinda trying to learn from someone more knowledgeable as to how i may have done either all of this better or individual bits better.
Table Image of for reference
The basic idea with this macro is in columns O and P i am pulling data from another sheet if C meets certain criteria. then in D E G J i am able to populate these based on if C meets a criteria and I4 is a specific variable.
I am then using the macro to copy paste all these formulas to the respective columns ther recopy pate as values then because the "" in my IFs are not real blanks (i need true empty cells for another checking formula) i use a combination of TextToColumn (if there is something in the column) and ClearContents to truly empty the cells.
so yea i am just kinda trying to learn from someone more knowledgeable as to how i may have done either all of this better or individual bits better.
Table Image of for reference
VBA Code:
Sub FleetTrip()
If MsgBox("Click OK to run macro (Populate from TGO)", _
vbOKCancel + vbQuestion) = vbCancel Then Exit Sub
Application.ScreenUpdating = False
' Driver and reason formula + copy paste
Range("O10").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND([@Activity]<=1,[@Activity]>0),""Fleet"",IF(AND(OR(R4C9=""usaid gbv"",R4C9=""anglo"",R4C9=""sobc""),IFERROR(XLOOKUP(R3C3&""|15 - Reason for travel|""&[@[Days of month]],'Raw KMs'!R2C20:R9415C20,'Raw KMs'!R2C19:R9415C19),""blank"")=""blank"",[@Activity]>1,OR([@[USAID GBV]]=""x"",[@ANGLO]=""x"",[@SIB]=""x"")),""No Info"",XLOOKUP(R3C3&""|15 - Reason for " & _
"travel|""&[@[Days of month]],'Raw KMs'!R2C20:R9415C20,'Raw KMs'!R2C19:R9415C19))),"""")" & _
""
Range("P10").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND([@Activity]<=1,[@Activity]>0),""Vehicle start"",IF(AND(OR(R4C9=""usaid gbv"",R4C9=""anglo"",R4C9=""sobc""),IFERROR(XLOOKUP(R3C3&""|15 - Reason for travel|""&[@[Days of month]],'Raw KMs'!R2C20:R9415C20,'Raw KMs'!R2C19:R9415C19),""blank"")=""blank"",[@Activity]>1,OR([@[USAID GBV]]=""x"",[@ANGLO]=""x"",[@SIB]=""x"")),""No Info"",XLOOKUP(R3C3&""|15 - Rea" & _
"son for travel|""&[@[Days of month]],'Raw KMs'!R2C20:R9415C20,'Raw KMs'!R2C17:R9415C17))),"""")" & _
""
Range("O10:P10").Select
Selection.Copy
Range("O10:P40").Select
ActiveSheet.Paste
Range("O10:P40").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' GBV "x" formula + copy paste
Range("D10").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND([@Activity]>1,R4C9=R9C4),""x"",""""),"""")"
Range("D10").Select
Selection.Copy
Range("D10:D40").Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' Anglo "x" formula + copy paste
Range("E10").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND([@Activity]>1,R4C9=R9C5),""x"",""""),"""")"
Range("E10").Select
Selection.Copy
Range("E10:E40").Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' SOBC "x" formula + copy paste
Range("J10").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND([@Activity]>1,R4C9=""SOBC""),""x"",""""),"""")"
Range("J10").Select
Selection.Copy
Range("J10:J40").Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' TRA "x" formula + copy paste
Range("G10").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(AND([@Activity]<=1,[@Activity]>0,[@Driver]=""Fleet""),""x"",""""),"""")"
Range("G11").Select
Range("G10").Select
Selection.Copy
Range("G10:G40").Select
ActiveSheet.Paste
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' All columns convert to general
'On Error Resume Next
Range("O10:O40").Select
If Application.WorksheetFunction.CountBlank(Selection) = 31 Then
Selection.ClearContents
Else
Selection.TextToColumns Destination:=Range("O10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End If
Range("P10:P40").Select
If Application.WorksheetFunction.CountBlank(Selection) = 31 Then
Selection.ClearContents
Else
Selection.TextToColumns Destination:=Range("P10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End If
Range("D10:D40").Select
If Application.WorksheetFunction.CountBlank(Selection) = 31 Then
Selection.ClearContents
Else
Selection.TextToColumns Destination:=Range("D10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End If
Range("E10:E40").Select
If Application.WorksheetFunction.CountBlank(Selection) = 31 Then
Selection.ClearContents
Else
Selection.TextToColumns Destination:=Range("E10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End If
Range("G10:G40").Select
If Application.WorksheetFunction.CountBlank(Selection) = 31 Then
Selection.ClearContents
Else
Selection.TextToColumns Destination:=Range("G10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End If
Range("J10:J40").Select
If Application.WorksheetFunction.CountBlank(Selection) = 31 Then
Selection.ClearContents
Else
Selection.TextToColumns Destination:=Range("J10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:="-", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End If
'On Error GoTo 0
Application.CutCopyMode = False
Range("B10").Select
Application.ScreenUpdating = True
MsgBox ("Done")
End Sub