Jump to content
groovyPost Forums

Help needed with Excel macros


Go to solution Solved by Simon H,

Recommended Posts

I have column B with 200 rows of true / false

I am trying to write a macro that will search column B from the top and when it finds the first true it will copy the contents from that row in column A and paste them to D1

Then I want to do the same thing in D2 but searching for the second true then in D3 searching for the third true.

e.g.If the first True is in cell B50 then I want cell D1 to display the contents of cell A50

If the second True is further down say in B75 I want cell D2 to display the contents of cell A75

If the third True is in cell B150 then I want cell D3 to display the contents of cell A150

It doesn’t matter if there are more than 3 True's I only need to find the first 3 wherever they are in column B

Link to comment
Share on other sites

  • Solution

You can actually do what you want with an array formula rather than a macro.

Presuming that your true false column starts at the top of column B in Cell B1 you need to copy and paste this into Cell C1,
=SUM(1(B1<$B$1:$B$50))+1+IF(ROW(B1)-ROW($B$1)=0,0,SUM(1(B1=OFFSET($B$1,0,0,INDEX(ROW(B1)-ROW($B$1)+1,1)-1,1))))

Now click on the formula bar and press Ctrl+Shift+Enter. The formula should now be in array brackets { }. This is important because without using CTRL+SHIFT+ENTER, you will get incorrect results.

Copy the formula down your range.

This ranks your TRUE's and FALSE's, giving TRUE values the lower #'s and FALSE values higher ones.So your first TRUE is ranked 1, 2nd TRUE is ranked 2, etc...

The rankings will automatically update if you change any TRUE to FALSE, or FALSE to TRUE

Now you can use INDEX/MATCH to get the data:

Copy and paste this into Cell D1=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,1),$C$1:$C$50,0),1)

Copy and paste this into Cell D2=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,2),$C$1:$C$50,0),1)

Copy and paste this into Cell D3=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,3),$C$1:$C$50,0),1)

This searches for the first, the second, and the third smallest number in Column C (1, 2, 3) and returns the value from the corresponding row in Column A.

If you are already using column C then you can put the array formula in any column but you will need to alter the formulas in D1, 2 and 3 to point to your chosen location of the array formula.

I believe this method is simpler than writing a macro and you can easily update it to look for more than the first 3 true values if needed

Link to comment
Share on other sites

  • 3 weeks later...

You can actually do what you want with an array formula rather than a macro.

Presuming that your true false column starts at the top of column B in Cell B1 you need to copy and paste this into Cell C1,

=SUM(1(B1<$B$1:$B$50))+1+IF(ROW(B1)-ROW($B$1)=0,0,SUM(1(B1=OFFSET($B$1,0,0,INDEX(ROW(B1)-ROW($B$1)+1,1)-1,1))))

Now click on the formula bar and press Ctrl+Shift+Enter. The formula should now be in array brackets { }. This is important because without using CTRL+SHIFT+ENTER, you will get incorrect results.

Copy the formula down your range.

This ranks your TRUE's and FALSE's, giving TRUE values the lower #'s and FALSE values higher ones.So your first TRUE is ranked 1, 2nd TRUE is ranked 2, etc...

The rankings will automatically update if you change any TRUE to FALSE, or FALSE to TRUE

Now you can use INDEX/MATCH to get the data:

Copy and paste this into Cell D1=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,1),$C$1:$C$50,0),1)

Copy and paste this into Cell D2=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,2),$C$1:$C$50,0),1)

Copy and paste this into Cell D3=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,3),$C$1:$C$50,0),1)

This searches for the first, the second, and the third smallest number in Column C (1, 2, 3) and returns the value from the corresponding row in Column A.

If you are already using column C then you can put the array formula in any column but you will need to alter the formulas in D1, 2 and 3 to point to your chosen location of the array formula.

I believe this method is simpler than writing a macro and you can easily update it to look for more than the first 3 true values if needed

 

It is very smart answer !!!! Congrates

Link to comment
Share on other sites

  • 2 weeks later...

You can actually do what you want with an array formula rather than a macro.

Presuming that your true false column starts at the top of column B in Cell B1 you need to copy and paste this into Cell C1,

=SUM(1(B1<$B$1:$B$50))+1+IF(ROW(B1)-ROW($B$1)=0,0,SUM(1(B1=OFFSET($B$1,0,0,INDEX(ROW(B1)-ROW($B$1)+1,1)-1,1))))

Now click on the formula bar and press Ctrl+Shift+Enter. The formula should now be in array brackets { }. This is important because without using CTRL+SHIFT+ENTER, you will get incorrect results.

Copy the formula down your range.

This ranks your TRUE's and FALSE's, giving TRUE values the lower #'s and FALSE values higher ones.So your first TRUE is ranked 1, 2nd TRUE is ranked 2, etc...

The rankings will automatically update if you change any TRUE to FALSE, or FALSE to TRUE

Now you can use INDEX/MATCH to get the data:

Copy and paste this into Cell D1=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,1),$C$1:$C$50,0),1)

Copy and paste this into Cell D2=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,2),$C$1:$C$50,0),1)

Copy and paste this into Cell D3=INDEX($A$1:$C$50,MATCH(SMALL($C$1:$C$50,3),$C$1:$C$50,0),1)

This searches for the first, the second, and the third smallest number in Column C (1, 2, 3) and returns the value from the corresponding row in Column A.

If you are already using column C then you can put the array formula in any column but you will need to alter the formulas in D1, 2 and 3 to point to your chosen location of the array formula.

I believe this method is simpler than writing a macro and you can easily update it to look for more than the first 3 true values if needed

 

I dont understand this but it looks good

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...