looping through a column and if cell value = Range(c4) then give value in next column

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi,

I want to loop through a column and if the values in this cells are the same as my c4 cell value then I want all the corresponding values in column 34 in a a new column. I have the code below but all it does is give me the first value in the 34 column and repeats it.

Code:
For Each cell In Range("D:D")
If cell.Value = Worksheets("x").Range("C4").Value Then
Worksheets("y").Range("BD:BD") = cell.Offset(i, 34).Value
End If
Next


End Sub

Thanks in advance for your help
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
It's not entirely clear.

What is the name of the sheet Range("D:D") is in?
In what row does the offset value go into, in worksheets("y") column BD? E.g. what is value of i in above and where does i change in your code - inside the loop or outside?
 
Upvote 0
Maybe something like
Code:
Sub CopyData()
    Dim i As Long, lr As Long
    With Worksheets("x")
        lr = .Cells(Rows.Count, 4).End(xlUp).Row
        For i = 1 To lr
            If .Range("D" & i).Value = .Range("C4").Value Then
                Worksheets("y").Range("BD" & i) = .Range("D" & i).Offset(i, 34).Value
            End If
        Next
    End With
End Sub
 
Upvote 0
Hi,

Please see my response below

It's not entirely clear.

What is the name of the sheet Range("D:D") is in?
say for instance this is in worksheet(Y)
In what row does the offset value go into, in worksheets("y") column BD? E.g. what is value of i in above and where does i change in your code - inside the loop or outside?
i is from first to last row.
 
Upvote 0
Still not clear, try by testing on a copy of your file:
Code:
Sub Macro1()
    
    Dim x       As Long

    Application.ScreenUpdating = False
    
    With Sheets("y")
        If .AutoFilterMode Then .AutoFilterMode = False
        x = .Cells(.Rows.Count, 4).End(xlUp).Row
        With .Cells(1, 4).Resize(x)
            .AutoFilter field:=1, Criteria1:=Sheets("x").Cells(4, 3).Value
            .Offset(1, 52).Resize(x - 1, 1).SpecialCells(xlCellTypeVisible).Value = .Offset(1, 34).Resize(x - 1, 1).SpecialCells(xlCellTypeVisible).Value
        End With
        .AutoFilterMode = False
    End With
        
    Application.ScreenUpdating = True
    
End Sub
A properly explained example with actual values and figures as if you're talking to someone who cannot see your PC monitor at all would help if above doesn't work.
 
Last edited:
Upvote 0
To be more clear ...

worksheet 1
c4 = " a"

wksheet 2
A B C
x 1
x 2
y 3
z 4


I need to check wksheet2 column A values and if this cell values is the same as wksheet1 c4 then i need to corresponding values in wksheet2 col B put into a new column.
 
Upvote 0
Try:
Code:
Sub Macro1()

    Dim x   As Long
    Dim LR  As Long
    Dim C4  As Variant
    
    C4 = Sheets("x").Cells(4, 3).Value
    
    Application.ScreenUpdating = False
        
    With Sheets("y")
        LR = .Cells(.Rows.Count, 1).End(xlUp).Row
        For x = 1 To LR
            If .Cells(x, 1).Value = C4 Then .Cells(x, 56).Value = .Cells(x, 38).Value
        Next x
    End With
    
    Application.ScreenUpdating = True
                
End Sub
 
Last edited:
Upvote 0
Perfect !...

This does the job


Much Appreciated !

Try:
Code:
Sub Macro1()

    Dim x   As Long
    Dim LR  As Long
    Dim C4  As Variant
    
    C4 = Sheets("x").Cells(4, 3).Value
    
    Application.ScreenUpdating = False
        
    With Sheets("y")
        LR = .Cells(.Rows.Count, 1).End(xlUp).Row
        For x = 1 To LR
            If .Cells(x, 1).Value = C4 Then .Cells(x, 56).Value = .Cells(x, 38).Value
        Next x
    End With
    
    Application.ScreenUpdating = True
                
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,179
Messages
6,129,329
Members
449,502
Latest member
TSH8125

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