Repeating macro for each cell in A (with offset)

anonymouslurker

New Member
Joined
Apr 2, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi everyone! Right now I'm having troubles with how to repeat my macro for each cell in A. I have some offsets so it becomes very confusing.

Here is my code. I recorded the macro while ticking 'Use Relative References' on.

Hopefully someone can help me. Thank you very much in advance :)

VBA Code:
Sub MacroTest()
'
' MacroTest Macro
'
'
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(1, 8).Range("A1:A83").Select
Selection.Copy
ActiveWindow.ScrollRow = 1
ActiveCell.Offset(-1, -2).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ActiveCell.Offset(1, -6).Range("A1:F1").Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(-1, 0).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1:J83").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi & welcome to MrExcel.
Can you please describe, in words, what you are trying to do?
 
Upvote 0
Hi there! Basically I want to reformat my file with transpose.

PeopleQuestionAnswer
Person 1Question 1Answer 1
Person 1Question 2Answer 2
Person 2Question 1Answer 3
Person 2Question 2Answer 4
Person 3Question 1Answer 5
Person 3Question 2Answer 6

Here above is a very simple version of my table. In reality there are lots of questions (way more than 2), thousands of people (definitely much more than 3!), and other columns between People and Question. To make my file easier to view I want to reformat it into something like this:

PeopleQuestion 1Question 2
Person 1Answer 1Answer 2
Person 2Answer 3Answer 4
Person 3Answer 5Answer 6

First I transposed the questions so the header looks like the second table above.

Then I recorded a macro. I selected cell A2 (where Person 1 is), then selected the whole row 2, then inserted a new row above it, like what I did below. Now the blank row is row 2.

PeopleQuestion 1Question 2
Person 1Question 1Answer 1
Person 1Question 2Answer 2
Person 2Question 1Answer 3
Person 2Question 2Answer 4
Person 3Question 1Answer 5
Person 3Question 2Answer 6

Next, I copied the all Person 1's answers (Answer 1 and Answer 2), and pasted them specially (transpose) in the blank cell in Question 1. So now the blank cells in Question 1 and 2 have Answer 1 and 2, respectively.

Afterwards, I copied the 'Person 1' text (in underline) to the blank cell in People. Finally, I selected all records related to person 1 (in italic) and delete them shifting cells up. Now the first Person 2 record is in row 3.

What I want the code to do is for it to loop (selecting person 2 next) and then do the same steps as I describe above, going on and on and on until the last person. But I don't know how...

Hope this makes sense.

Help would be appreciated. Thank you :)
 
Upvote 0
Will the number of questions always be the same, or can that vary?
What columns are the Questions & and answers in?
Is the Name in col A starting in A2?
 
Upvote 0
Number of questions will always be the same.
Question is in G, Answers is in I.
Yes, the name starts from A2. A1 is the header for the name.

Thanks :)
 
Upvote 0
Ok, thanks for that, forgot to ask how many questions, is it 83?
 
Upvote 0
Yep, there are 83 questions.
Row 83 contains information of the 82nd question and Person 1's answer to it.
Row 85 contains information of the 1st question and Person 2's answer to it.

Thank you! :)
 
Upvote 0
Ok, this will put the output on another sheet, change sheet names in red to suit
Rich (BB code):
Sub anonymouslurker()
   Dim Ary As Variant, Nary As Variant
   Dim i As Long, nr As Long, j As Long
   
   Ary = Sheets("Master").Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2) + 80)
   For i = 2 To UBound(Ary) Step 83
      nr = nr + 1
      For j = 1 To 6
         Nary(nr, j) = Ary(i, j)
      Next j
      For j = 1 To 83
         Nary(nr, j + 6) = Ary(i + j - 1, 9)
      Next j
   Next i
   Sheets("Sheet2").Range("A2").Resize(nr, UBound(Nary, 2)).Value = Nary
End Sub
 
Upvote 0
Hi there,
So I created a new Sheet in the same workbook named Sheet2.
Then I tried to run ThisWorkbook.anonymouslurker macro but it says 'Type mismatch'?

Thank you :)
 
Upvote 0
Do you have any blank rows or columns in your data?
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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