Spending an hour+ a day copying and pasting

FBLondon

New Member
Joined
Jan 26, 2018
Messages
1
Hello,

I have a the below table with x's (only mine is 100x300).

AdamJohnFredFrank
Applesxxx
Pearsx
Bananasxxx
Grapesxx
Orangesx

<tbody>
</tbody>

I spend an hour+ each day copying and pasting into the below format by copying sheet and=IF(Sheet1!A2="x",$A1,""), remove blanks, shift up, transpose, then split out over rows.
AdamApples
AdamBananas
JohnBananas
JohnGrapes
FredApples
FrankApples
FrankPears
FrankBananas
FrankGrapes
FrankOranges

<tbody>
</tbody>


I've searched and searched online for a solution but even have trouble even describing my problem to google.

Does anyone have a better way of doing this and save my life?

Thank you in advance
 

Stiuart_W

Well-known Member
Joined
Jul 3, 2013
Messages
516
I'm sure this is something that can be done very easily in VBA

I have to go home now and will be back in the office Monday. If you get no response by then reply in the thread and I'll help you out
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,006
This code will do it for you. You didn't specify where you wanted the result, so I have written it out onto Sheet2
Code:
With ActiveSheet


lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
inarr = Range(.Cells(1, 1), .Cells(lastrow, lastcol))


End With
With Worksheets("sheet2")
Range(.Cells(1, 1), .Cells(lastrow * lastcol, 2)) = ""
outarr = Range(.Cells(1, 1), .Cells(lastrow * lastcol, 2))
indi = 1
For i = 2 To lastcol
 For j = 2 To lastrow
   If inarr(j, i) = "x" Then
    outarr(indi, 1) = inarr(1, i)
    outarr(indi, 2) = inarr(j, 1)
    indi = indi + 1
   End If
 Next j
Next i


Range(.Cells(1, 1), .Cells(lastrow * lastcol, 2)) = outarr




End With
End Sub
 
Last edited:

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,647
FBLondon,

Welcome to the MrExcel forum.

If I understand you correctly, then here is a macro solution for you to consider, that uses two arrays in memory.

With your raw data in worksheet Sheet1, the results will be written to worksheet Sheet2.


Code:
Sub ReorganizeData()
' hiker95, 01/26/2018, ME1040673
Application.ScreenUpdating = False
Dim w1 As Worksheet, w2 As Worksheet
Dim a As Variant, i As Long, c As Long, n As Long, lr As Long, lc As Long
Dim o As Variant, j As Long
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  n = Application.CountIf(.Range(.Cells(2, 2), .Cells(lr, lc)), "=x")
  a = .Cells(1, 1).CurrentRegion
  ReDim o(1 To n, 1 To 2)
End With
For c = 2 To UBound(a, 2)
  For i = 2 To UBound(a, 1)
    If a(i, c) = "x" Then
      j = j + 1: o(j, 1) = a(1, c): o(j, 2) = a(i, 1)
    End If
  Next i
Next c
With w2
  .Cells(1, 1).CurrentRegion.ClearContents
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:

Forum statistics

Threads
1,082,040
Messages
5,362,804
Members
400,693
Latest member
jenlj

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top