Find/match dates, and paste cellvalue in column X at the same row

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hello

I have column A in Workbook wbPort where I have listed all dates since 01.01.2005 until 2014

In workbook wb I have a column R where some of the cells has a value, and in column A I have the dates.

I would like to find the cell where there's a value (Column R), then take that date (Column A) and find the matching date in wbPort and paste the value in column X at the same row as date.

I've tried this code, but I'm not sure if this is any good way to solve it.

Code:
 For Each c In rng
            If Abs(c.Value) < 0.01 Then
            rowVal = c.Value.Row
            
            finddate = ws1.Range("A" & rowVal).Value
            Res = Application.Match(CLng(finddate), ws1.Range("A2:A3482"), 0)
            
                c.Value.Copy Destination:wsPort.Cells(Res + 1, X)
            End If
        Next

Kind regards
Espen
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Hi Espen.

Using the Find method:
Code:
For Each c In rng
 If Abs(c.Value) < 0.01 Then
  Set Res = ws1.Range("A2:A3482").Find(Range("A" & c.Row).Value, LookIn:=xlValues)
   If Not Res Is Nothing Then
    c.Value.Copy wbPort.Sheets(1).Range("X" & Res.Row) 'edit sheet name
   End If
 End If
Next
Assumptions are that wbPort is an object variable for the second workbook and that sheet 1 is where the data is stored.
 
Upvote 0

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Thanks, it looks like it will work. But I now discovered that I don't know how to get those cells in my range to be selected...

I select multiple cells manually, and then run the code and the idea was that the code would pick up only the selected cells, and that these would be defined in "For each c in rng"

Here is what I got so far:

Code:
Set rng = ActiveCell


   
    Intersect(ActiveSheet.UsedRange, Columns(1)).Copy
    wsPort.Activate
    wsPort.Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=True

Kind regards
Espen
 
Upvote 0

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Just an update:

This code works for my copy purpose:

Code:
Set rng = Selection


    rng.Copy


    wsPort.Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
       :=False, Transpose:=True

Have a nice evening :)
 
Upvote 0

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
ADVERTISEMENT
Thanks, it looks like it will work. But I now discovered that I don't know how to get those cells in my range to be selected...

I select multiple cells manually, and then run the code and the idea was that the code would pick up only the selected cells, and that these would be defined in "For each c in rng"

Kind regards
Espen

If you are making a munual selection and then want to loop through those items, you can use this approach:
Select several items on a worksheet, they can be contiguous or non-contiguous, makes no difference.
Code:
Sub example()  
For Each c In Selection
 MsgBox c.Value
Next
End Sub
 
Upvote 0

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Thanks. But if I want to copy those cells, and paste them horizontal to another worksheet should it be something like this:


Code:
Sub example() 
 For Each c In Selection
i = i + 1
wsPort.Cells(1, i).Value = c
NextEnd Sub

Kind regards
Espen
 
Upvote 0

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Thanks. But if I want to copy those cells, and paste them horizontal to another worksheet should it be something like this:


Code:
Sub example() 
 For Each c In Selection
i = i + 1
wsPort.Cells(1, i).Value = c
NextEnd Sub

Kind regards
Espen

I think it would need to = c.Value
 
Upvote 0

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Aha. Thanks a lot, I'll try it right away.

Kind regards
Espen
 
Upvote 0

Forum statistics

Threads
1,195,994
Messages
6,012,750
Members
441,724
Latest member
Aalbid

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