Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

combine Right function with IF function by vba

0

Hi,

I  have  this  part of  my  code 

.Range("F2:F" & R) = .Evaluate("IF(B2:B" & R & "=""FIRST BALANCE"","""",1)")

I  try  to  add  Right  function but doesn't succeed  and  I  don't  know  where  is  exact location  should  add  it .

what  I  want to  match part  of  this  FIRST BALANCE  because 

I have  FIRST BALANCE FOR SALES , FIRST BALANCE FOR PURCHASE

currently  will  just  search for  whole  words FIRST BALANCE.

the  code  will create report in last sheet by matching new data among sheets based on matching sheets names with  the  last part of item are  existed in column B for DAILY sheet based on the  same date and amount 
if the  last item in column B is  matched with  sheet name  when match the  others sheets and  the  same  amount  and  date  then  will ignore it , if  there is no  last item doesn't contain sheet name to match  with  the  others  sheets in DAILY sheet then also will ignore  it .
it  will  copy  new data in two cases:
1-by matching new data among sheets based on matching sheets names with  the  last part of item are  existed in column B for DAILY sheet based on the  different date and amount .
2- by new data are existed in the  others sheets but not is  existed in DAILY sheet totally 

how  can  I  use  RIGHT function  with this  line ,please?

Answer
Discuss

Discussion

Hi Omaran

Can't see this in context to provide an answer- please edit your question to attach a representative Excel file using the Add Files... button and I'll answer over the weekend hopefully 
John_Ru (rep: 6152) Aug 25, '23 at 4:10 pm
Hi John,
so you  have  no  idea how add RIGHT function  in  this  line?
I didn't attach file  becuase I  have sensitive data and  I  thought  maybe solve  without  post  the  whole code and file , sorry!
Omaran (rep: 8) Aug 25, '23 at 4:20 pm
Omaran. I know well how to use Right in general but it's late here and I don't know what your typical (not sensitive) data is like. Feels like you could use Right on just the last character to differentiate  but if you don't want to provide a representative file (and continue to be rude to me) I won't help. Your call! 
John_Ru (rep: 6152) Aug 25, '23 at 6:08 pm
(and continue to be rude to me)
why  you  think  that ?!
just  I ask  you , sorry  if  I  use bad  way  to  ask  you !
but if you don't want to provide a representative file 
I  don't  say  that. just  I  inform  you  why  I don't  attacing .
well, I will see  what  I  can  do  about  random  data
thanks  for  your  time .
Omaran (rep: 8) Aug 25, '23 at 6:27 pm
Omaran

Unfortunately (in English at least) your phrase "... so you  have  no  idea..." is quite insulting. Nevertheless please upload some "random data" (aka representative data) in an Excel file and I will reply to you tomorrow (UK time).
John_Ru (rep: 6152) Aug 25, '23 at 6:32 pm
attached file  with highlighted red should  be  excluded it  .
based on original code  will just  exclude FIRST BALANCE  words
Omaran (rep: 8) Aug 26, '23 at 4:25 am
Add to Discussion

Answers

0
Selected Answer

Omaran

I had a problem running your sheet but to ignore any entries starting with "FIRST BALANCE" (13 characters), you need to the Left function (not Right). Therefore you should replace your line:

       .Range("F2:F" & R) = .Evaluate("IF(B2:B" & R & "=""FIRST BALANCE"","""",1)") 

with (changes in bold):

    R = .[A1].CurrentRegion.Rows.Count - 1
    If F > 1 And R > 0 Then
       .Range("F2:F" & R) = Application.Evaluate("IF(Left(B2:B" & R & ", 13)=" _
            & Chr(34) & "FIRST BALANCE" & Chr(34) & ", " & Chr(34) & Chr(34) & ",1)")

Note that is really a single line of code but split with " _" (return) to make it easier to read. Chr(34) returns the double quotation mark ("), which is ASCII (decimal) character 34.

Where R is 14 say, that equates to the string for evaluation:

IF(Left(B2:B14, 13)="FIRST BALANCE", "",1)

BUT that will evaluate the formula based on the active sheet (not the sheet picked in the loop).

Revision 27 August 2023

I'd suggest that you delete your lines:

    For F = 1 To Index - 1
   With Worksheets(F)
        S(F) = .Name
     If F > 1 Then
        R = .[A1].CurrentRegion.Rows.Count - 1
       .Range("F2:F" & R) = .Evaluate("IF(B2:B" & R & "=""FIRST BALANCE"","""",1)")
     Else
       .UsedRange.Columns(6).Clear
     End If
   End With
    Next

and use a simple loop (and use an alternative method to determine the last row in A) instead:

    For F = 1 To Index - 1
        With Worksheets(F)
        S(F) = .Name
        'get last used row in A
        R = .Range("A" & Rows.Count).End(xlUp).Row
        '
        If F > 1 And R > 1 Then
            'R = .[A1].CurrentRegion.Rows.Count - 1
            For n = 2 To R
                If Left(.Range("B" & n), 13) = "FIRST BALANCE" Then
                    .Range("F" & n) = ""
                    Else
                    .Range("F" & n) = 1
                End If
            Next n

            Else
            .UsedRange.Columns(6).Clear
        End If
        End With
    Next

I got a compliation error when I tried to run the corrected code but didn't try to fix your code. I think the sheet numbers are a bit confused (in my non-Arabic version of Excel) but suspect at the least you have to first define what Index is, something like:

Sub Demo1()

    Dim S$(), F&, R&, W, Rf As Range, Index As Long



    Index = Sheets.Count + 1





  ReDim S(1 To Index - 1)

and also probably (following that) define which sheet is to be cleared (unless it's always run from a given sheet):

Worksheets("REPORT FOR MISSED").UsedRange.Offset(1).Clear

or whatever is the right sheet name.

Hope this helps. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

thanks 
I tested  your  suggestion but  still shows 
FIRST BALANCE FOR SALES , FIRST BALANCE FOR PURCHASE
Omaran (rep: 8) Aug 26, '23 at 8:53 am
Did you try the revised Answer (uding a For loop)? Sorry but I realised my initial Answer (following your Evaluate method) wouldn't work but I didn't mark my revision since I had to dash out). 

Kindly comment but note I'm out for a few hours now. 
John_Ru (rep: 6152) Aug 26, '23 at 9:07 am
Thanks
there  is  problem about TOTAL row for  the  last sheet"AMER" 
the  code copy the  TOTAL row  ,should  not  copy .
Omaran (rep: 8) Aug 26, '23 at 2:53 pm
Okay, I'll try to find time to look at that but it's your code... 
John_Ru (rep: 6152) Aug 26, '23 at 5:05 pm
Ali. I couldn't run the code again (don't know why) but realised that my suggested loop was wrong (since you'd used .UsedRange to determine the last row per sheet). I've revised my Answer (see after "Revision 27 August 2023") to use an alternative method to determine the last row in A) and corrected the loop (so it will ignore your Totals row). This code extract is now in my Answer::
    For F = 1 To Index - 1
        With Worksheets(F)
        S(F) = .Name
        'get last used row in A
        R = .Range("A" & Rows.Count).End(xlUp).Row
        '
        If F > 1 And R > 1 Then
            'R = .[A1].CurrentRegion.Rows.Count - 1
            For n = 2 To R
                If Left(.Range("B" & n), 13) = "FIRST BALANCE" Then
                    .Range("F" & n) = ""
                    Else
                    .Range("F" & n) = 1
                End If
            Next n
            
            Else
            .UsedRange.Columns(6).Clear
        End If
        End With
    Next


Hope this fixes things for you. If so, please remember to mark my Answer as Selected.
John_Ru (rep: 6152) Aug 27, '23 at 9:55 am
Ali.
this  is not  my  name , buddy!
I couldn't run the code again (don't know why)
sometimes occurs odd things in excel .
I've revised my Answer (see after "Revision 27 August 2023") to use an alternative method to determine the last row in A) and corrected the loop (so it will ignore your Totals row). This code extract is now in my Answer::
perfect ,everything  is  ok 
thank  you   for  your time & assistance .
Omaran (rep: 8) Aug 27, '23 at 12:37 pm
Glad that worked for you Omaran (sorry but I was answering another user Ali at much the same time). Thanks for selecting my Answer.
John_Ru (rep: 6152) Aug 27, '23 at 2:46 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login