VBA Copy paste special values based on cell reference

TomRain

New Member
Joined
Apr 4, 2019
Messages
3
Hi All,

I'm having some issues with VBA. I want the code to look up cells within a range, that when greater than zero, copies and pastes adjacent columns as values in the same location (to remove the formulas).

The lookup range is J10:J371, and if greater than zero, I want adjacent cells in column J,K,L to copy and paste as values.

For example, J52 = 2000, then J52:L52 copies and pastes as values in the same place.

I saw something similar on the below thread but I can't quite get it to work on mine.

https://www.mrexcel.com/forum/excel...aste-special-values-based-cell-reference.html

Thank you in advance.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
2,006
Hello and welcome

This should do what you ask:

Code:
Sub HardWriteValue()
    Dim c As Range
    For Each c In Range("J10:J371")
        If c > 0 Then
            Application.EnableEvents = False
            c = c
            c.Offset(, 1) = c.Offset(, 1)
            c.Offset(, 2) = c.Offset(, 2)
            Application.EnableEvents = True
        End If
    Next c
End Sub
 

TomRain

New Member
Joined
Apr 4, 2019
Messages
3
Hello and welcome

This should do what you ask:

Code:
Sub HardWriteValue()
    Dim c As Range
    For Each c In Range("J10:J371")
        If c > 0 Then
            Application.EnableEvents = False
            c = c
            c.Offset(, 1) = c.Offset(, 1)
            c.Offset(, 2) = c.Offset(, 2)
            Application.EnableEvents = True
        End If
    Next c
End Sub

Thank you so much for the quick response, it worked perfectly! One more thing, what would I need to add to ensure the macro does the same process across all sheets within my file simultaneously?

Thanks again.
 

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
2,006
Just loop through all worksheets:

Code:
Sub HardWriteValue()
    Dim c As Range
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        For Each c In ws.Range("J10:J371")
            If c > 0 Then
                Application.EnableEvents = False
                c = c
                c.Offset(, 1) = c.Offset(, 1)
                c.Offset(, 2) = c.Offset(, 2)
                Application.EnableEvents = True
            End If
        Next c
    Next ws
End Sub
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,129,791
Messages
5,638,333
Members
417,021
Latest member
moon miner

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
Top