Excel copy cell if condition met

Hi Sam,

No worries man, a macro can be written for this.

I just want to make sure we are not confusing columns with rows here.
Can you confirm that either the data being moved is located in a row or that the x's are located in a row.

One more thing. You want this to be automatic. What did you have in mind:
1) when x is entered, move data.
2) run the code at the end of the day to make sure all data is entered and then remove the x's so they aren't moved twice.
3) run the code at the end of the day to make sure all data is entered and then remove all data being moved.

If you choose for option 1 then please specify move as cut/paste or copy/paste.

Please note that the more column/row references you provide the more accurate the code will be.

Best regards,
Trowa

  • #2

Hi Zem32619,

Welcome to MrExcel!!

Try this:

VBA Code:

Option Explicit
Sub Macro1()

    Dim ws As Worksheet
    Dim lngMyRow As Long
    
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Sheet name containing the data. Change to suit if necessary.
    
    For lngMyRow = 2 To ws.Cells(Rows.Count, "O").End(xlUp).Row
        If StrConv(ws.Range("O" & lngMyRow), vbProperCase) = "Limit" Then
            ws.Range("L" & lngMyRow).Value = ws.Range("N" & lngMyRow).Value
        End If
    Next lngMyRow
      
    Application.ScreenUpdating = True

End Sub

Regards,

Robert

  • #4

Hi Robert. Exactly what I wanted it to do. Thank you so much. Great help.

Thanks for letting us know and you're welcome

Sometimes you need to copy cells that meeting certain criteria. And here we will introduce 2 useful methods to copy the target cells.

In an Excel worksheet, there will certainly be a lot of data and information. However, those cells contain values that meet certain criterion will separate in different range. As a result, it will be difficult when you need to collect them.

In this worksheet, you need to copy cells that is larger than 350. And if you sort those cells one by one and then copy then, you are sure to spend a lot of time and energy.

Excel copy cell if condition met

Thus, now you can use the two methods below to finish this task.

Method 1: Copy the Filter Result

If the target cells are in a range, you can use the filter feature in Excel. You can set multiple criteria for the column and then copy the filter result.

  1. Place your cursor within the target range.
  2. And then click the button “Sort & Filter” in the toolbar.
  3. Next choose the option “Filter” in the drop-down list.
    Excel copy cell if condition met
  4. Now click the button in the column header.
  5. Next choose the option “Number Filters”
  6. In the sub menu, choose the option that you need. Here we will choose the “Greater Than”.
    Excel copy cell if condition met
  7. In the “Custom AutoFilter” window, set the criteria that you need.
  8. And then click the button “OK”.
    Excel copy cell if condition met

Therefore, only the cells meet the criterion will show in the worksheet. Other cells will be hidden.

Excel copy cell if condition met

When the filter process ends, you can perform the copy and paste process.

  1. Select the result in the worksheet.
  2. And then press the shortcut keys “Ctrl + C” on the keyboard to copy the range.
  3. Now click another cell in the worksheet.
  4. Next press the shortcut keys “Ctrl +V” to paste those cells.
  5. And then press the button “Esc” to quit the copy mode.

You can see that only the cells meet with the criteria will be pasted to the new range.

Excel copy cell if condition met

However, you will meet with troubles if you paste to the range that share the same rows with the original range. Here only two numbers are pasted into the new range.

Excel copy cell if condition met

The reason is actually very simple. In the original range, the cells that don’t meet the criterion are hid. And the whole rows that contain those cells are also hid. Therefore, in the new range, you can only see part of the result. Other values are in the hidden rows. In order to show the result, you can clear the criteria in the filter feature. And then you can see the whole result in the worksheet.

Excel copy cell if condition met

Method 2: Use VBA Macro

When those target cells are not in a column, you cannot use the filter feature. And in order to finish this task, now you can use VBA macro.

  1. Press the shortcut keys “Alt + F11” on the keyboard to open the Visual Basic editor.
  2. And then click the tab “Insert” in the toolbar.
  3. After that, choose the option “Module” in the sub menu. Therefore, you have inserted a new module in this project.
    Excel copy cell if condition met
  4. In this step, copy the following VBA codes into the new module.
Sub Copy_Cells()
  Dim c As Range, i As Integer, j As Integer
  i = 0
  For Each c In Range("B2:B13")
    If c > 350 Then
      Cells(2 + i, 6) = Cells(c.Row, c.Column - 1)
      Cells(2 + i, 7) = c
    End If
    i = i + 1
  Next c
   For j = 13 To 2 Step -1
    If Cells(j, 6) = "" Then
      Range("F" & j, "G" & j).Delete Shift:=xlUp
    End If
  Next j
End Sub

In this macro, we will find the cells that meet the criteria. And next the values will be copied to a new range. When the copy and paste process ends, this macro will delete the blank cells in the new range. In your actual worksheet, you need to change the criteria and the range in the codes according to your actual need.

  1. Here click the button “Run Sub” in the tool bar or press the button “Enter” on the keyboard to run the macro.

Next come back to the worksheet, you will see that the result is already in the worksheet.

Excel copy cell if condition met

You can also using VBA macros to find cells that are not in a column. Thus, when you need to copy cells in multiple ranges, you can modify the macro and use it.

A Comparison of the Two Methods

In this table, we will show you the advantages and disadvantages of the two methods.

Comparison

Copy the Filter Result

Use VBA Macro

Advantages

If you are not familiar with Excel VBA, you can use this method. You can also use this method to copy cells that are in different ranges.

Disadvantages

When the cells are separate in different area in the worksheet, you cannot filter the data by this method. Using VBA macros will sometimes generate errors in your worksheet.

Hence, the next time you need to copy cells based on certain criteria, you can choose the most suitable method.

Excel Safety is Cause for Concern

The safety of Excel is very essential for you, and you should pay more attention on the Excel safety. Many users have lost their data and information in Excel due to various reasons. If you cannot guarantee the safety of your files, you are sure to suffer a lot. The result of Excel corruption can be disastrous. Whenever you meet with errors in Excel, you can repair Excel xls data corruption by a useful third-party tool. with this tool at hand, you will never lose your data and information.

Author Introduction:

Anna Ma is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair Word doc damage and outlook repair software products. For more information visit www.datanumen.com

How do I copy a cell value based on a condition?

Copy Conditional Formatting Using Paste Special.
Select cell B2..
Right-click and copy it (or use the keyboard shortcut Control + C).
Select the entire range where you want to copy the conditional formatting (C2:C11 in this example).
Right-click anywhere in the selection..
Click on the Paste Special option..

How do you copy an IF function cell?

To use this method:.
Select a cell within your data..
Use the shortcut CTRL SHIFT L to display the filter buttons..
Filter out any records that you don't want to copy..
Select a cell within your data..
Use the shortcut CTRL A to select all the data..
Use the shortcut CTRL C to copy the data (this will copy visible records only).

How do I auto populate data in Excel based on another cell?

Click and hold the left mouse button, and drag the plus sign over the cells you want to fill. And the series is filled in for you automatically using the AutoFill feature.

How do you copy cells in Excel if column contains specific value text?

Paste Special options.
Select the cells that contain the data or other attributes that you want to copy..
On the Home tab, click Copy ..
Click the first cell in the area where you want to paste what you copied..
On the Home tab, click the arrow next to Paste, and then select Paste Special..
Select the options you want..