Dim a range query

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
I have wrote the following to look down column A until it finds a match with ‘FindDate’ (a named range that holds the date I wish to find). It then pastes ‘InserThis’ (a named range with word I would like inserted). This is offset by 22 columns. It works fine if ‘InsertThis’ is only one cell.

Is it possible make ‘InsertThis’ a range or cells, so more than one cell is pasted when a match is found?

Sub TestInsertOne()
Dim ArrayResult As Range
Set ArrayResult = Range("InsertThis")
'Start from cell.
Range("A3").Select
'Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
'How many cells to concatenate and what range to equal.
If ActiveCell = Range("FindDate") Then
'Where to paste the result.
ActiveCell.Offset(0, 22) = ArrayResult
Else
End If
'Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Can't you just actually copy/paste?
Code:
ArrayResult.Copy ActiveCell.Offset(0,22)
 
Upvote 0
Thank you very much Norie. That has done the trick. Thanks.
 
Upvote 0
As an extra question, if that's ok. How would I transpose the values before being pasted into the sheet?
 
Upvote 0
Hi,

Sorry to be a pest. But, I'm well and truly stuck. I tried everything I know and have done a load of searching. But no answer yet.

The code pastes in no problem, thanks to Norie. But the paste also copies the format and as an addition I would like to transpose the range.

Is this possible, or should I be looking for a different solution.
 
Upvote 0
To copy and transpose values you'll need to use PasteSpecial.

Something like this:
Code:
ArrayResult.Copy 

ActiveCell.Offset(0,22) xlPasteValue, Transpose:=True
You'll need to check the constant for the first argument, it might need an s, ie Values.
 
Upvote 0
Thanks for the reply. I've replaced the line:

ArrayResult.Copy ActiveCell.Offset(0, 22) with the lines you have written. But, the second line turns red. I'm at the limit of what I know regarding vb, so sorry if I'm being a bit thick.
 
Upvote 0
I'm going to give this one last try before, scrappping it and trying to write another way of doing it. I have tried adding:

With ActiveCell
.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True

But it gives me a Complie error: Else without if

I would appreciate any help offered. But I guess some things are best scrapped and started again.
 
Upvote 0
Well it looks like it wasn't only the 's' you needed to check.

I missed out what is probably the most important part of a paste special.
Rich (BB code):
ArrayResult.Copy 

ActiveCell.Offset(0,22).PasteSpecial Paste:=xlPasteValues, Transpose:=True
By the way, it is just values you want to copy?

You seem to have changed to formats in the code you just posted.:)
 
Upvote 0
Now that is strange. Just as I had just found the answer (which is exactly the same as yours). Then said hurray, I recived an email with your answer.

Thank you for your time. It's very much appreciated and I've learn't something new.
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

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