copying all columns of a row from an array to a "target" cell ?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
to all

Looking at different ideas about searching through a range of cells for the value in the 1st column of an array (array is Nrow*10columns)
Sort of vlookup() or match() if you like. Nothing new/hasn't been done before . Used List of() in the past (or was it a Dictionary ?) as can use
VBA Code:
.Exist
bur I am very rusty with all this. Maybe turning the s1t column of the array into a
VBA Code:
Collection
might be faster
A few steps are broken down as a 1st pass (easier for me) but I suspect one can merge a few line eg setting range


VBA Code:
im myarray As Variant ' this is a Nrow * 10column
'myarray = rgfull.Value  'to be added at some point

Dim wktarget As Worksheet: Set wktarget = Worksheets("target")
Dim irowtarget As Integer: irowtarget = 2
Dim icoltarget As Integer: icoltarget = 2
Dim rgID As Range

With wktarget
    'Worksheets("target").Range(Cells(irowtarget, icoltarget)).Select  ' this is not working
    .Range("B2").Select
    '.Range(Selection, Selection.End(xlDown)).Select
    Set rgID = .Range(Selection, Selection.End(xlDown))
End With

For Each c In rgID
    
    For ii = 0 To UBound(myarray)
    'find the row in the array matching the value of cell being processed
    'if found it then copy the data in the array row ii to the targetsheet
        If myarray(ii, 0) <> c.Value Then
            GoTo Skiptonext
        Else
            'past row ii, column 1 to 9 to cell(.Activecell.row,3)
            datafromarray = myarray(ii,1:ii,9) <---syntax required!!
            wktarget.Range(c.Address.Row, 3).Value = datafromarray
        End If
Skiptonext:
    Next

Next c
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
There are enough problems with what you have posted that I find myself wanting to recommend that you start over; but it's hard to tell what you are trying to accomplish.

1. Avoid using Select or Selection. This is almost always possible to do. Instead define range objects and work on them directly.
2. If you want to find something in a range, search the range (range.find). Don't complicate things with arrays unless there is a demonstrated need, such as unacceptably slow performance. Most of the time there is not.
3. Declare your variables. This will make life easier for you and for anyone trying to help you. If you are going to declare some, then declare them all. You have declared several, but then inexplicably failed to declare several more (c,ii, datafromarray). Adding Option Explicit to the top of each code module will force variable declaration and is good practice.

VBA Code:
Sub RangeFindExample()
    Dim wktarget As Worksheet
    Dim rgID As Range, rgfull As Range, R As Range, c As Range
    
    Set wktarget = ActiveSheet    'Worksheets("target")
    Set rgfull = wktarget.Range("G1:P20")
    
    With wktarget
        Set rgID = .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
    End With
    
    'loop thru each cell in range rgID and search for that value in range rgfull
    For Each c In rgID
        Set R = rgfull.Find(What:=c.Value, Lookat:=xlWhole)
        If Not R Is Nothing Then
            Debug.Print "Found '" & R.Value & "' at address " & R.Address
            c.Offset(0, 3).Value = "Found '" & R.Value & "' at address " & R.Address
        Else
            c.Offset(0, 3).Value = "('" & c.Value & "' not found)"
        End If
    Next c
End Sub

Book2
BCDEFGHIJKLMNOP
1DataResultschylousquaeregreeterhyocholicdewaxesdeadpansimmergingsapotechevrehyocholic
2quaereFound 'quaere' at address $H$1quotabiosestonarcheusquotabayonetunheavyfacitcircinalcanicidequota
3biosestonFound 'bioseston' at address $H$2tarriersduhatcaravanswhitenslipolyticthwartersbeautidonorriveretwhitens
4duhatFound 'duhat' at address $H$3josephinetodidaewillywawdishclothcallantspeckinessboodledommarathitowneesdishcloth
5todidaeFound 'todidae' at address $H$4unseatedcedronmedicareescripthuckthattensureframingoenomausescript
6bandlessFound 'bandless' at address $M$6chockedambsacesvinedmenschesforestdillweedbandlessaperitivechimbleymensches
7ricassoFound 'ricasso' at address $N$7thwarterscaprylicepithecapeshkarheptanoicrhynchoteventedricassotopplepeshkar
8redbudsFound 'redbuds' at address $O$9bialifaulklandtrimoricservagegestorgullunseatedorangeatfeedbagservage
9gravid('gravid' not found)dihedronsgamorijugularurucuryamantillomoneyworthickwalllineallyredbudsurucury
10shogunalFound 'shogunal' at address $P$10circinalungildedgrecianexpressedpipetmoiddominoroytdialonianshogunal
11chylousFound 'chylous' at address $G$1urbanistsFonoscinefaulklandfrizzliermalikzadibeakersimbreatheridderscodolfrizzlier
12nonoscine('nonoscine' not found)humuleneformylatemacrouridbootlaceswoodymuddyingegosdrokpasculkbootlaces
13formylateFound 'formylate' at address $H$12thairmgnawingtrochidaeveriscopefoggiertesstanninedtheophiledogfishveriscope
14gnawingFound 'gnawing' at address $H$13unseatedfibrinosescalepangambolledscandiumsunsalnessepicarpalvellumscedrongambolled
15fibrinoseFound 'fibrinose' at address $H$14shoranpancakesisinglassturtleholostomegranulesrefaceroadworksmonmouthlamp
16Jumper('Jumper' not found)sculkoographpapyrindirdoutseekmarybudmanibanjoistryegrassdird
17oographFound 'oograph' at address $H$16champacsmolossefifishnodosaurphysickerpauperatebloditeisostereroadworksnodosaur
18molosseFound 'molosse' at address $H$17shawneesfiliusrelongapolloniacamelothotbedsklvakeglernonesuchapollonia
19filiusFound 'filius' at address $H$18younkersrudydeforcingcesareansbodockidionfaulklandjumelleovotestiscesareans
20rudyFound 'rudy' at address $H$19intsvepiphegussaghavartshogunalmesonasalbassifumewortfungoidalparalaliashoegunal
21epiphegusFound 'epiphegus' at address $H$20
Sheet1
 
Upvote 0
Solution
Thanks
Looks like I forgot about .Find() and based on your example might do what I had in mind.

I was only thinking about array() as I may have to do something with the data before copying so an array might be better. My workflow seems to suggest that doing what I need to do post copying using xls directly might be better long term (for the user). So array might be out anyway.

There are a lot of things I need to test. For example I cannot have .end(xlup) to set rgID. Must be xldown. Small things that I need to finalise but still checking/agreeing a lot things about data format
———

The use of selection was a left-over of some testing, I never actually use it

Variables are always defined - again it appears just because of copy-paste to test bits of coding

John
 
Upvote 0
The use of selection was a left-over of some testing, I never actually use it

Variables are always defined - again it appears just because of copy-paste to test bits of coding

John

No one here is a mind reader. In the future, please explain this kind of thing in advance.
 
Upvote 0

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

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