excel formula to equal data from another sheet based on cell value

demonicsoul

New Member
Joined
Nov 9, 2011
Messages
22
Office Version
  1. 365
Hi,

From the screenshots below, if sheet1.column A has a value of 1, then in Sheet2, make cells A1 and A2 equal the value in Sheet1.Cell(B1) and so on going down the list. So basically, whenever there is a 1, the value from column B should be in two rows on Sheet2 and if there is a 2, then value in column B is only in 1 row in Sheet2. Is there a formula or VBA that will satisfy this logic? Thanks

1691694415276.png
1691694432216.png
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Something like this code should do it.

I had the source and destination on the same worksheet
Source Data in Columns A, and B
Output values in Column E (the range address will need to be changed to output the results a different sheet
like Range("Sheet2!A" & n) = ...
But other than this change the code should work for you.

There is no code to clear the column of previous values in this version.

VBA Code:
Option Explicit
Sub ExpandMenu()
  Dim lr As Long
  Dim r, i, n As Long
  lr = Range("A" & Rows.Count).End(xlUp).Row
  n = 1
  For r = 1 To lr
    For i = 1 To IIf(Range("A" & r) = 1, 2, 1) 'if you want more than 2 entries of the value change the number here (e.g. the 2 to a 3 for example)
      Range("E" & n) = Range("B" & r)
      n = n + 1
    Next i
  Next r
End Sub


Book1
ABCDE
11chickenchicken
22beefchicken
31chickenbeef
43fishchicken
5chicken
6fish
Sheet1
 
Upvote 0
Solution

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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