VBA - How to copy cell from a different worksheet with conditionals

yorgosnc

New Member
Joined
Dec 4, 2015
Messages
33
Hi guys, so I'm trying to copy the Text from one sheet to another with some conditions. So I have in the Column D, after the cell D7, some data organized by rows, and this data can vary. And if the cell in Column D is filled, the Column H will always have the same data as the Column D to Column "i" if they have more data.

What I need to do is, I need to copy the Text in the Column D, but if they have more then just one data filled in the Column H to Column "i" I need to put all the Text's filled. For example:

COLUMN DCOLUMN HCOLUMN ICOLUMN JCOLUMN K
P1001P1001
P1002P1002P1006
P1003P1003P1007P1008
P1004P1004
P1005P1005P1009P1010P1011

<tbody>
</tbody>



In the other sheet I need to organize in this way:

Column A
A4P1001
A5P1002 = P1006
A6P1003=P1007=P1008
A7P1004
A8P1005=P1009=P1010=P1011

<tbody>
</tbody>


What I came up so far is this:
Code:
Lr = Sheets("Folha Chave").Columns("D").Find("*", After:=.Range("D7"), SearchDirection:=xlNext, SearchOrder:=xlByRows, LookIn:=xlValues).Row

Set Rng = Sheets("Folha Chave").Range("D7:D" & Lr)
For Each cell In Rng
If cell.Value <> "" Then
Sheets("Agrupamento").Cells(4, 1) = cell.Value
End If
Next cell
The problem is, I can't figured out how to copy in sequence to my destination sheet. For instance, Cells(4,1) aka A4 is the first cell, but I want to copy the second data in the cell A5, etc..

Thanks in advance guys
smile.gif
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I improved a little bit of my code:

Code:
Lr = Sheets("Folha Chave").Columns("D").Find("*", After:=.Range("D7"), SearchDirection:=xlNext, SearchOrder:=xlByRows, LookIn:=xlValues).Row
NP = Application.WorksheetFunction.CountIf(Range("D7:D" & Lr), "<>" & "")


Set Rng = Sheets("Folha Chave").Range("D7:D" & Lr)
For Each cell In Rng
If cell.Value <> "" Then
For x = 4 To 4 + NP
Sheets("Agrupamento").Cells(x, 1) = cell.Value
Next x
End If
Next cell

But what i need is to jump cell and then jump the 'x' if you know what I mean.
 
Upvote 0
What about something like this not using VBA:

Code:
=E5&IF(F5="","","="&F5)&IF(G5="","","="&G5)&IF(H5="","","="&H5)

Using IF Statements and Concatenate.
In my example I started in E you'll start in H it looks like. But it says start with the value in H, IF there's a value in F then Concatenate it with the H value and an "=" between them if there isn't then it's blank. Build it from there to encompass the longest row of values you have and then drag it through on your other sheets.
 
Last edited:
Upvote 0
What about something like this not using VBA:

Code:
=E5&IF(F5="","","="&F5)&IF(G5="","","="&G5)&IF(H5="","","="&H5)

Using IF Statements and Concatenate.
In my example I started in E you'll start in H it looks like. But it says start with the value in H, IF there's a value in F then Concatenate it with the H value and an "=" between them if there isn't then it's blank. Build it from there to encompass the longest row of values you have and then drag it through on your other sheets.

It's a good solution, but I'm creating a macro that generates some sheets, and inside this new sheet, it's my problem, I can use your solution but in that case, I need to type the formulas, and what a need i'ts to give me the solution automatically, I'll try to use VBA to insert your formula. Cheers mate.
 
Upvote 0

Forum statistics

Threads
1,216,129
Messages
6,129,051
Members
449,484
Latest member
khairianr

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