VBA - How to write a code for: IF column A = "milk" then column B = "2.50".

Justyna P

New Member
Joined
Dec 21, 2024
Messages
1
Office Version
  1. 2024
Platform
  1. Windows
Hello, I'm a very new (I mean literally an hour) in using VBA and Macros and I'm trying to write my very first if statement. The issue is that when I try on a single cell everything is fine, but I am trying to apply it to the whole column! More specifically - if something occurs in column A then a value pops out in column B next to it. Like when there is a name of product in A then the price pops out in B. Can anyone help with that please?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
VBA Code:
Option Explicit

Sub srchCpy()
    With ThisWorkbook.Worksheets("Sheet1")
        .Range("B2:B" & .Range("A" & .Rows.Count).End(xlUp).Row).Formula = "=VLOOKUP(A2,Sheet2!A:B,2,FALSE)"
    End With
End Sub

The above assumes you are seeking dollar amounts to appear on sheet 1 in column B. The product names are located in sheet 1/column A beginning with row #2. Cell A1 has the term "Product". Your master price list is located in sheet 2 with product name in column A and associated price in column B. Neither column A or column B in sheet 2 has a header.
 

Attachments

  • Sheet 1.jpg
    Sheet 1.jpg
    29.2 KB · Views: 2
  • Sheet 2.jpg
    Sheet 2.jpg
    26.5 KB · Views: 2
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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