IF cells in column A are greater/less than zero, then only paste those cells in column B

Jeevz_87

New Member
Joined
Sep 21, 2021
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi All,


I have an issue with vba code that im trying to write where I want to only copy cells of a certain value over into another column on the same sheet.

I need my code to loop through cells in column A, find all cells with a value greater or less than zero and paste those cells into column B in the same worksheet. The paste should not overwrite any values in column B that already contain values. I also want the code to select the entire column every time for both procedures

So far I have been experimenting with a piece of code I modified for another task but not sure sure how to adapt it with the IF statement in this case;

VBA Code:
Sub Copy_P123()

    Range("Z12").Select 'Column A selection
    
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("BL12").Select 'Column B paste
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

Seems like every task I'm currently doing in VBA requires me to ask new questions as the requirements are never the same, hence I'll always ask weird and wonderful questions.

I would be greatly appreciative of any help re: the above :)

Many thanks,


Jeevz
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
a value greater or less than zero

I intepret that as cells that do not equal 0

One way
VBA Code:
Sub Copy_P123()
    Dim WS As Worksheet
    Dim CellRange As Range, R As Range

    Set WS = ActiveSheet
    With WS
        Set CellRange = .Range("A2", .Range("A" & .Rows.count).End(xlUp))    'alt range to last cell in column w/data
    End With

    For Each R In CellRange
        If R.Value <> 0 And VBA.IsNumeric(R.Value) Then
            If Trim(R.Offset(0, 1).Value) = "" Then
                R.Offset(0, 1).Value = R.Value
            End If
        End If
    Next R
End Sub
 
Upvote 0
Oh, I'm late. Anyway, here's what I came up with. Paste this macro into a standard module.
I'm assuming that row 1 in sheet 1 is for headers and data starts from A2.
VBA Code:
Option Explicit
Sub CopyLessOrGreater()
    With Sheets(1)
        .AutoFilterMode = False
        With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
            .AutoFilter Field:=1, Criteria1:="<>0"
            On Error Resume Next
            .Offset(1).SpecialCells(xlCellTypeVisible).Copy .Range("B" & .Cells(Rows.Count, "B").End(xlUp).Row + 1)
            On Error GoTo 0
        End With
        .AutoFilterMode = False
    End With
End Sub
 
Upvote 0
Hi rlv01 & rollis13,

Yes you'd be correcting in assuming as much.

The idea here is to be able to loop each row in column A (starting at AB12 for example) and seeing which cells are greater than or less than 0. Then pasting those figures, in the same rows into column B (BN12). I dont mind the pasted figures overwriting anything already in the cell.

1666559834345.png
1666559957975.png


If I can make this work, the next phase will be to apply it to the remaining balance of column, but of course it depends if this initial macro works first in the above fashion.
 
Upvote 0
=IF(AND(B43<>0),B43,"")

Kitap3.xlsx.xlsm
BC
4311
4422
4511
4622
4711
48-1-1
49-1-1
50-2-2
510 
03.02.23
Cell Formulas
RangeFormula
C43:C51C43=IF(AND(B43<>0),B43,"")
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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