VBA If Then Evaluations

NYEXCEL1

Board Regular
Joined
Apr 17, 2013
Messages
123
Having trouble creating a macro to both evaluate and/or replace data in the table (Headers in Blue) with the last 3 columns (smile headers). The result of the macro is shown below in Blue. The criteria is as follows:
  • In COlumn E (ACCT), for each row, if the cell is blank then the Number "2000" needs to be inserted otherwise what is there should not be affected
  • IN Column F (MEMO), What is there should stay but if there is text in the column titled "Business Purpose" then that should be added to the text in column F AFTER the Colon
  • IN COlumn H (INvoice Approver), The Column titled "DEPARTMENT" needs to be evaluated and if there is text in the cell it should replace the text in Column H otherwise nothing.
  • In Column I (Waiver Markup), For each row Column D should be evaluated, if the number is "5080" the corresponding row in column I should say "NOTHING", if another 4 digit number it should say "NONEED", if a date then leave what is already there.
The data sheet will be made hundreds of rows should the above should cover the entire worksheet to be safe.

Any assitance here would be great as the task is not hard but VBA seems to be the only way as formulas dont seem to be the most efficient.

Many thanks members!!


ABCDEFGHI
!TRNSTRNSIDTRNSTYPEDATEACCNTNAMEAMOUNTDOCNUMMEMO:):):)
!SPLSPLIDTRNSTYPEACCNTAMOUNTMEMOClientInvoice ApproverWaive Markup (Finance Approval Required)Business PurposeDepartmentREIMBEXP
TRNSBILL
5/5/2020​
PR Newswire
-1500​
103588132​
PR Newswire
SPLBILL
5080​
2395​
Program Expense:Joe's StoreJohn DoeNEntertainment Industry Corp/Tech:Tech:CETNOTHING
SPLBILL
5060​
-895​
Program Expense - Nonbillable:Joe's StoreJohn DoeEntertainment Industry Corp/Tech:Tech:CETNONEED



Final Result


!TRNSTRNSIDTRNSTYPEDATEACCNTNAMEAMOUNTDOCNUMMEMO:):):)
!SPLSPLIDTRNSTYPEACCNTAMOUNTMEMOClientInvoice ApproverWaive Markup (Finance Approval Required)Business PurposeDepartmentREIMBEXP
TRNSBILL
5/5/2020​
2000PR Newswire
-1500​
103588132​
PR Newswire
SPLBILL
5080​
2395​
Program Expense: Entertainment Industry Joe's StoreCorpNOTHINGEntertainment Industry CorpNOTHING
SPLBILL
5060​
-895​
Program Expense - Nonbillable: Entertainment Industry Joe's StoreCorpNONEEDEntertainment Industry CorpNONEED
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I assume your data starts in cell A3.
On a copy of your sheet, try the following:

VBA Code:
Sub IfEvaluations()
  Dim a As Variant, i As Long
  
  a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row).Value2
  For i = 1 To UBound(a)
    If a(i, 5) = "" Then a(i, 5) = "2000"
    If a(i, 10) <> "" Then a(i, 6) = a(i, 6) & " " & a(i, 10)
    If a(i, 11) <> "" Then a(i, 8) = a(i, 11)
    If a(i, 4) = "5080" Or a(i, 4) = 5080 Then
      a(i, 9) = "NOTHING"
    ElseIf Len(a(i, 4)) = 4 Then
      a(i, 9) = "NONEED"
    End If
  Next
  Range("A3").Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub
 
Upvote 0
Amazing! Thank you. Couple of questions if you dont mind.

1. if you can tell me in plain English what these lines of code are doing, so I can learn:

a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row).Value2
For i = 1 To UBound(a)

Range("A3").Resize(UBound(a, 1), UBound(a, 2)).Value = a

2. In the first Column, the code "TRNS" will appear quite often. Is there a line of code that beginning but not including the first instance, that a line is inserted and the code "ENDTRNS" be put in that Column Space?

3. At the Very end of the data set, in column A the code "ENDTRNS" be inserted in the first column after the last line of data



Many thanks again, a true talent!!
 
Upvote 0
1. if you can tell me in plain English what these lines of code are doing, so I can learn:

VBA Code:
  'Fill the array 'a' with the data from cell A3 to column L and the last row with data
  a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row).Value2
  
  'cycle from 1 to the number of lines in array 'a'
  For i = 1 To UBound(a)

  'Resize the output, starting at A3 and
  'increasing the number of rows according to the number of rows in array (a,1) and
  'increasing the number of columns according to the number of columns in array (a,2)
  Range("A3").Resize(UBound(a, 1), UBound(a, 2)).Value = a

2. In the first Column, the code "TRNS" will appear quite often. Is there a line of code that beginning but not including the first instance, that a line is inserted and the code "ENDTRNS" be put in that Column Space?
I do not understand, the macro has a problem? or is it a new requirement? You can explain it with an example that you have in your data in post #1.

3. At the Very end of the data set, in column A the code "ENDTRNS" be inserted in the first column after the last line of data
I do not understand, the macro has a problem? or is it a new requirement? You can explain it with an example that you have in your data in post #1.
 
Upvote 0
Thanks for your reply. Macro works great. my questions were add on once i ran the program.

For Question #2 & #3, I realize my question is the same and the answer is in Blue Text below. For context there will be many transactions that begin with "TRNS" followed by "SPL" in rows underneath. The number of lines with "SPL" will vary so a blank line will need to be inserted after at the end of the last "SPL" in each segment and then "ENDSTRNS" be added.





ABCDEFGHI
!TRNSTRNSIDTRNSTYPEDATEACCNTNAMEAMOUNTDOCNUMMEMO:):):)
!SPLSPLIDTRNSTYPEACCNTAMOUNTMEMOClientInvoice ApproverWaive Markup (Finance Approval Required)Business PurposeDepartmentREIMBEXP
TRNSBILL5/5/2020PR Newswire-1500103588132PR Newswire
SPLBILL50802395Program Expense:Joe's StoreJohn DoeNEntertainment IndustryCorp/Tech:Tech:CETNOTHING
SPLBILL5060-895Program Expense - Nonbillable:Joe's StoreJohn DoeEntertainment IndustryCorp/Tech:Tech:CETNONEED
ENDSTRNS
TRNSBILL5/5/2020PR Newswire-1500103588132PR Newswire
SPLBILL50802395Program Expense:Joe's StoreJohn DoeNEntertainment IndustryCorp/Tech:Tech:CETNOTHING
SPLBILL5060-895Program Expense - Nonbillable:Joe's StoreJohn DoeEntertainment IndustryCorp/Tech:Tech:CETNONEED

ENDSTRNS
TRNSBILL5/5/2020PR Newswire-1500103588132PR Newswire
SPLBILL50802395Program Expense:Joe's StoreJohn DoeNEntertainment IndustryCorp/Tech:Tech:CETNOTHING

ENDSTRNS
 
Upvote 0
Try this

VBA Code:
Sub IfEvaluations()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, n As Long
  
  'Fill the array 'a' with the data from cell A3 to column L and the last row with data
  a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row + 1).Value2
  n = WorksheetFunction.CountIf(Range("A:A"), "TRNS")
  ReDim b(1 To UBound(a, 1) + n, 1 To UBound(a, 2))
  'cycle from 1 to the number of lines in array 'a'
  For i = 1 To UBound(a) - 1
    k = k + 1
    For j = 1 To UBound(a, 2)
      b(k, j) = a(i, j)
    Next
    
    If a(i, 5) = "" Then b(k, 5) = "2000"
    If a(i, 10) <> "" Then b(k, 6) = a(i, 6) & " " & a(i, 10)
    If a(i, 11) <> "" Then b(k, 8) = a(i, 11)
    If a(i, 4) = "5080" Or b(k, 4) = 5080 Then
      b(k, 9) = "NOTHING"
    ElseIf Len(a(i, 4)) = 4 Then
      b(k, 9) = "NONEED"
    End If
  
    If a(i + 1, 1) = "TRNS" Then
      k = k + 1
      b(k, 1) = "ENDSTRNS"
    End If
  Next
  '
  b(k + 1, 1) = "ENDSTRNS"
  Range("A3").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Percect.

Last question, it looks like the formatting in the fourth column is being changed from date to general and from general to date for some reason. In summary, what is in blue is accurate, what seems to follow after has issues.

Is there a way to format using the logic "If the first column has "TRNS" then the format of the cell in column 4 in same row is date" "If SPL then general 4 digit number"?


That should be the last change




!TRNSTRNSIDTRNSTYPEDATE
!SPLSPLIDTRNSTYPEACCNT
!ENDTRNS
TRNSBILL
5/5/2020
SPLBILL
5080
SPLBILL
5060
ENDSTRNS
TRNSBILL
43942​
SPLBILL
5080​
SPLBILL
11/27/1913​
ENDSTRNS
TRNSBILL
43970​
SPLBILL
11/27/1913​
SPLBILL
5060​
ENDSTRNS
TRNSBILL
43980​
SPLBILL
11/27/1913​
 
Upvote 0
Change this
a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row + 1).Value2

For this
a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row + 1).Value

Try again
 
Upvote 0
Unfortunately no. Below are the results

!TRNSTRNSIDTRNSTYPEDATE
!SPLSPLIDTRNSTYPEACCNT
!ENDTRNS
TRNSBILL
5/5/2020​
SPLBILL
5080​
SPLBILL
5060​
ENDSTRNS
TRNSBILL
4/21/2020​
SPLBILL
5080​
SPLBILL
11/27/1913​
ENDSTRNS
TRNSBILL
5/19/2020​
SPLBILL
11/27/1913​
SPLBILL
5060​
ENDSTRNS
TRNSBILL
5/29/2020​
SPLBILL
11/27/1913​
ENDSTRNS
TRNSBILL
6/19/2020​
SPLBILL
5080​
ENDSTRNS
TRNSBILL
5/29/2020​
SPLBILL
11/27/1913​
ENDSTRNS
TRNSBILL
5/28/2020​
SPLBILL
5080​
ENDSTRNS
TRNSBILL
6/4/2020​
SPLBILL
5080​
ENDSTRNS
TRNSBILL
6/15/2020​
SPLBILL
5080​
ENDSTRNS
TRNSBILL
4/27/2020​
SPLBILL
11/27/1913​
SPLBILL
5080​
ENDSTRNS
TRNSBILL
6/18/2020​
SPLBILL
5080​
ENDSTRNS
TRNSBILL
4/27/2020​
SPLBILL
11/27/1913​
SPLBILL
5080​
ENDSTRNS
 
Upvote 0
Try this:

VBA Code:
Sub IfEvaluations()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, n As Long
  
  'Fill the array 'a' with the data from cell A3 to column L and the last row with data
  Columns("D:D").NumberFormat = "General"
  a = Range("A3:L" & Range("A:L").Find("*", , , xlPart, xlByRows, xlPrevious).Row + 1).Value
  n = WorksheetFunction.CountIf(Range("A:A"), "TRNS")
  ReDim b(1 To UBound(a, 1) + n, 1 To UBound(a, 2))
  'cycle from 1 to the number of lines in array 'a'
  For i = 1 To UBound(a) - 1
    k = k + 1
    For j = 1 To UBound(a, 2)
      b(k, j) = a(i, j)
      If j = 4 Then
        If Len(b(k, j)) > 4 Then
          b(k, j) = Format(b(k, j), "mm/dd/yyyy")
        End If
      End If
    Next
    
    If a(i, 5) = "" Then b(k, 5) = "2000"
    If a(i, 10) <> "" Then b(k, 6) = a(i, 6) & " " & a(i, 10)
    If a(i, 11) <> "" Then b(k, 8) = a(i, 11)
    If a(i, 4) = "5080" Or b(k, 4) = 5080 Then
      b(k, 9) = "NOTHING"
    ElseIf Len(a(i, 4)) = 4 Then
      b(k, 9) = "NONEED"
    End If
  
    If a(i + 1, 1) = "TRNS" Then
      k = k + 1
      b(k, 1) = "ENDSTRNS"
    End If
  Next
  '
  b(k + 1, 1) = "ENDSTRNS"
  Range("A3").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top