Excel - return a value only if a condition is met via VBA

0 votes

I have some code that scans an xls file source and, based on the company name, extracts sales information specific to that firm and inserts it into the company's own file.

I want to have a condition that only returns a value in one of my columns if a condition is met, even though it probably isn't very beautiful.

It is the line in the code below that has a comment: Any assistance is much valued.

For i = 2 To LastRow
        If SourceSheet.Cells(i, 21).Value Like "CompanyName goes here*" Then
            'change the column numbers to the relevant number
            Product = SourceSheet.Cells(i, 11).Value
            Base Sales Value = SourceSheet.Cells(i, 27).Value 
            Partner = SourceSheet.Cells(i, 21).Value
            EndUser = SourceSheet.Cells(i, 7).Value
            License = SourceSheet.Cells(i, 13).Value
            PostingMonth = SourceSheet.Cells(i, 3).Value
            LicType = SourceSheet.Cells(i, 12).Value
            newuplift = SourceSheet.Cells(i, 15).Value
            UpliftValue = SourceSheet.Cells(i, 28).Value

            erow = DestSheet.Cells(DestSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            'change the column numbers to the relevant number
            DestSheet.Cells(erow, 1).Value = ProdType
            DestSheet.Cells(erow, 2).Value = License
            DestSheet.Cells(erow, 3).Value = Partner
            DestSheet.Cells(erow, 4).Value = EndUser
            DestSheet.Cells(erow, 5).Value = SOValue
            DestSheet.Cells(erow, 6).Value = PostingMonth
            DestSheet.Cells(erow, 7).Value = newuplift
            DestSheet.Cells(erow, 8).Value = LicType
            DestSheet.Cells(erow, 9).Value = UpliftValue 'TRYING TO PLACE A CONDITION HERE - SEE BELOW

' If newuplift = "Renewal" then place the Upliftvalue in row 9, otherwise set to "0"
        End If
    Next i
Jan 26, 2023 in Others by Kithuzzz
• 38,010 points

1 answer to this question.

0 votes

You could use IIf()

DestSheet.Cells(erow, 9).Value = IIf(newuplift = "Renewal", UpliftValue, 0)
answered Jan 26, 2023 by narikkadan
• 63,700 points

Related Questions In Others

0 votes
1 answer

(Excel) If cell is greater than <condition> then minus <number>

The IF function to calculate B5 (amount ...READ MORE

answered Nov 7, 2022 in Others by narikkadan
• 63,700 points
0 votes
1 answer

How to find a value in an excel column by vba code Cells.Find

Just use: Dim Cell As Range Columns("B:B").Select Set cell = ...READ MORE

answered Nov 17, 2022 in Others by narikkadan
• 63,700 points
0 votes
1 answer

How to fix a circular reference error by if condition in excel file?

Circular reference in this context refers to ...READ MORE

answered Nov 21, 2022 in Others by narikkadan
• 63,700 points
0 votes
1 answer

How can i add cells if condition is met

Both the AND function and nested if ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,700 points
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
0 votes
1 answer
0 votes
1 answer

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

You misunderstand the purpose of the function ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,700 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP