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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0
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.
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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