Excel VBA Combine duplicates and add column values












0















Hi I want to create a code to arrange the data using vba but I don't know how.



I have a data that looks like this



     Col 1    |  Col 2   
1. Question 1 | Person 1
1. Question 1 | Person 2
1. Question 1 | Person 3
2. Question 2 | Person 1
2. Question 2 | Person 2
2. Question 2 | Person 3
3. Question 3 | Person 1
3. Question 3 | Person 2
3. Question 3 | Person 3


I want the output to look like this



Col 2    | Col 1
Person 1 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 2 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 3 | 1. Question 1
| 2. Question 2
| 3. Question 3


I don't know how to make this using vba. please help me on this.



thank you.










share|improve this question























  • Welcome to SuperUser! You may not need VBA. Have you looked at pivot tables?

    – Rey Juna
    Feb 7 at 3:09











  • Hi @ReyJuna, yes I've tried pivot tables but I have a desired format for the output. and I'm stuck on this problem.

    – gee
    Feb 7 at 6:27
















0















Hi I want to create a code to arrange the data using vba but I don't know how.



I have a data that looks like this



     Col 1    |  Col 2   
1. Question 1 | Person 1
1. Question 1 | Person 2
1. Question 1 | Person 3
2. Question 2 | Person 1
2. Question 2 | Person 2
2. Question 2 | Person 3
3. Question 3 | Person 1
3. Question 3 | Person 2
3. Question 3 | Person 3


I want the output to look like this



Col 2    | Col 1
Person 1 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 2 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 3 | 1. Question 1
| 2. Question 2
| 3. Question 3


I don't know how to make this using vba. please help me on this.



thank you.










share|improve this question























  • Welcome to SuperUser! You may not need VBA. Have you looked at pivot tables?

    – Rey Juna
    Feb 7 at 3:09











  • Hi @ReyJuna, yes I've tried pivot tables but I have a desired format for the output. and I'm stuck on this problem.

    – gee
    Feb 7 at 6:27














0












0








0








Hi I want to create a code to arrange the data using vba but I don't know how.



I have a data that looks like this



     Col 1    |  Col 2   
1. Question 1 | Person 1
1. Question 1 | Person 2
1. Question 1 | Person 3
2. Question 2 | Person 1
2. Question 2 | Person 2
2. Question 2 | Person 3
3. Question 3 | Person 1
3. Question 3 | Person 2
3. Question 3 | Person 3


I want the output to look like this



Col 2    | Col 1
Person 1 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 2 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 3 | 1. Question 1
| 2. Question 2
| 3. Question 3


I don't know how to make this using vba. please help me on this.



thank you.










share|improve this question














Hi I want to create a code to arrange the data using vba but I don't know how.



I have a data that looks like this



     Col 1    |  Col 2   
1. Question 1 | Person 1
1. Question 1 | Person 2
1. Question 1 | Person 3
2. Question 2 | Person 1
2. Question 2 | Person 2
2. Question 2 | Person 3
3. Question 3 | Person 1
3. Question 3 | Person 2
3. Question 3 | Person 3


I want the output to look like this



Col 2    | Col 1
Person 1 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 2 | 1. Question 1
| 2. Question 2
| 3. Question 3
-------------------------
Person 3 | 1. Question 1
| 2. Question 2
| 3. Question 3


I don't know how to make this using vba. please help me on this.



thank you.







microsoft-excel vba macros






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 7 at 1:18









geegee

324




324













  • Welcome to SuperUser! You may not need VBA. Have you looked at pivot tables?

    – Rey Juna
    Feb 7 at 3:09











  • Hi @ReyJuna, yes I've tried pivot tables but I have a desired format for the output. and I'm stuck on this problem.

    – gee
    Feb 7 at 6:27



















  • Welcome to SuperUser! You may not need VBA. Have you looked at pivot tables?

    – Rey Juna
    Feb 7 at 3:09











  • Hi @ReyJuna, yes I've tried pivot tables but I have a desired format for the output. and I'm stuck on this problem.

    – gee
    Feb 7 at 6:27

















Welcome to SuperUser! You may not need VBA. Have you looked at pivot tables?

– Rey Juna
Feb 7 at 3:09





Welcome to SuperUser! You may not need VBA. Have you looked at pivot tables?

– Rey Juna
Feb 7 at 3:09













Hi @ReyJuna, yes I've tried pivot tables but I have a desired format for the output. and I'm stuck on this problem.

– gee
Feb 7 at 6:27





Hi @ReyJuna, yes I've tried pivot tables but I have a desired format for the output. and I'm stuck on this problem.

– gee
Feb 7 at 6:27










1 Answer
1






active

oldest

votes


















0














Directly, without optimization and any checks:





Sub ReSort(src As Range, dst As Range)
Dim i As Integer, j As Integer, tmp, temp()
' Copy source range
temp = src.Value
' Sort data
For i = LBound(temp, 1) To UBound(temp, 1) - 1
For j = i + 1 To UBound(temp, 1)
If (temp(i, 2) > temp(j, 2)) Or ((temp(i, 2) = temp(j, 2)) And (temp(i, 1) > temp(j, 1))) Then
tmp = temp(i, 1)
temp(i, 1) = temp(j, 1)
temp(j, 1) = tmp
tmp = temp(i, 2)
temp(i, 2) = temp(j, 2)
temp(j, 2) = tmp
End If
Next j
Next i
' Clear vertical dups
For i = UBound(temp, 1) - 1 To LBound(temp, 1) Step -1
If temp(i + 1, 2) = temp(i, 2) Then
temp(i + 1, 2) = ""
End If
Next i
' Swap columns
For i = LBound(temp, 1) To UBound(temp, 1)
tmp = temp(i, 1)
temp(i, 1) = temp(i, 2)
temp(i, 2) = tmp
Next i
' Store result
dst.Value = temp
End Sub


src may be equal to dst if you want to overwrite. For example,



Call ReSort(Range("A1:B9"), Range("A1:B9"))





share|improve this answer
























  • Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

    – gee
    Feb 7 at 6:13











  • @gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

    – Akina
    Feb 7 at 6:45











  • ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

    – gee
    Feb 7 at 7:40











  • @gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

    – Akina
    Feb 7 at 7:45













  • @gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

    – Akina
    Feb 8 at 7:39













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1402921%2fexcel-vba-combine-duplicates-and-add-column-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Directly, without optimization and any checks:





Sub ReSort(src As Range, dst As Range)
Dim i As Integer, j As Integer, tmp, temp()
' Copy source range
temp = src.Value
' Sort data
For i = LBound(temp, 1) To UBound(temp, 1) - 1
For j = i + 1 To UBound(temp, 1)
If (temp(i, 2) > temp(j, 2)) Or ((temp(i, 2) = temp(j, 2)) And (temp(i, 1) > temp(j, 1))) Then
tmp = temp(i, 1)
temp(i, 1) = temp(j, 1)
temp(j, 1) = tmp
tmp = temp(i, 2)
temp(i, 2) = temp(j, 2)
temp(j, 2) = tmp
End If
Next j
Next i
' Clear vertical dups
For i = UBound(temp, 1) - 1 To LBound(temp, 1) Step -1
If temp(i + 1, 2) = temp(i, 2) Then
temp(i + 1, 2) = ""
End If
Next i
' Swap columns
For i = LBound(temp, 1) To UBound(temp, 1)
tmp = temp(i, 1)
temp(i, 1) = temp(i, 2)
temp(i, 2) = tmp
Next i
' Store result
dst.Value = temp
End Sub


src may be equal to dst if you want to overwrite. For example,



Call ReSort(Range("A1:B9"), Range("A1:B9"))





share|improve this answer
























  • Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

    – gee
    Feb 7 at 6:13











  • @gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

    – Akina
    Feb 7 at 6:45











  • ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

    – gee
    Feb 7 at 7:40











  • @gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

    – Akina
    Feb 7 at 7:45













  • @gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

    – Akina
    Feb 8 at 7:39


















0














Directly, without optimization and any checks:





Sub ReSort(src As Range, dst As Range)
Dim i As Integer, j As Integer, tmp, temp()
' Copy source range
temp = src.Value
' Sort data
For i = LBound(temp, 1) To UBound(temp, 1) - 1
For j = i + 1 To UBound(temp, 1)
If (temp(i, 2) > temp(j, 2)) Or ((temp(i, 2) = temp(j, 2)) And (temp(i, 1) > temp(j, 1))) Then
tmp = temp(i, 1)
temp(i, 1) = temp(j, 1)
temp(j, 1) = tmp
tmp = temp(i, 2)
temp(i, 2) = temp(j, 2)
temp(j, 2) = tmp
End If
Next j
Next i
' Clear vertical dups
For i = UBound(temp, 1) - 1 To LBound(temp, 1) Step -1
If temp(i + 1, 2) = temp(i, 2) Then
temp(i + 1, 2) = ""
End If
Next i
' Swap columns
For i = LBound(temp, 1) To UBound(temp, 1)
tmp = temp(i, 1)
temp(i, 1) = temp(i, 2)
temp(i, 2) = tmp
Next i
' Store result
dst.Value = temp
End Sub


src may be equal to dst if you want to overwrite. For example,



Call ReSort(Range("A1:B9"), Range("A1:B9"))





share|improve this answer
























  • Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

    – gee
    Feb 7 at 6:13











  • @gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

    – Akina
    Feb 7 at 6:45











  • ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

    – gee
    Feb 7 at 7:40











  • @gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

    – Akina
    Feb 7 at 7:45













  • @gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

    – Akina
    Feb 8 at 7:39
















0












0








0







Directly, without optimization and any checks:





Sub ReSort(src As Range, dst As Range)
Dim i As Integer, j As Integer, tmp, temp()
' Copy source range
temp = src.Value
' Sort data
For i = LBound(temp, 1) To UBound(temp, 1) - 1
For j = i + 1 To UBound(temp, 1)
If (temp(i, 2) > temp(j, 2)) Or ((temp(i, 2) = temp(j, 2)) And (temp(i, 1) > temp(j, 1))) Then
tmp = temp(i, 1)
temp(i, 1) = temp(j, 1)
temp(j, 1) = tmp
tmp = temp(i, 2)
temp(i, 2) = temp(j, 2)
temp(j, 2) = tmp
End If
Next j
Next i
' Clear vertical dups
For i = UBound(temp, 1) - 1 To LBound(temp, 1) Step -1
If temp(i + 1, 2) = temp(i, 2) Then
temp(i + 1, 2) = ""
End If
Next i
' Swap columns
For i = LBound(temp, 1) To UBound(temp, 1)
tmp = temp(i, 1)
temp(i, 1) = temp(i, 2)
temp(i, 2) = tmp
Next i
' Store result
dst.Value = temp
End Sub


src may be equal to dst if you want to overwrite. For example,



Call ReSort(Range("A1:B9"), Range("A1:B9"))





share|improve this answer













Directly, without optimization and any checks:





Sub ReSort(src As Range, dst As Range)
Dim i As Integer, j As Integer, tmp, temp()
' Copy source range
temp = src.Value
' Sort data
For i = LBound(temp, 1) To UBound(temp, 1) - 1
For j = i + 1 To UBound(temp, 1)
If (temp(i, 2) > temp(j, 2)) Or ((temp(i, 2) = temp(j, 2)) And (temp(i, 1) > temp(j, 1))) Then
tmp = temp(i, 1)
temp(i, 1) = temp(j, 1)
temp(j, 1) = tmp
tmp = temp(i, 2)
temp(i, 2) = temp(j, 2)
temp(j, 2) = tmp
End If
Next j
Next i
' Clear vertical dups
For i = UBound(temp, 1) - 1 To LBound(temp, 1) Step -1
If temp(i + 1, 2) = temp(i, 2) Then
temp(i + 1, 2) = ""
End If
Next i
' Swap columns
For i = LBound(temp, 1) To UBound(temp, 1)
tmp = temp(i, 1)
temp(i, 1) = temp(i, 2)
temp(i, 2) = tmp
Next i
' Store result
dst.Value = temp
End Sub


src may be equal to dst if you want to overwrite. For example,



Call ReSort(Range("A1:B9"), Range("A1:B9"))






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 7 at 4:57









AkinaAkina

1,35528




1,35528













  • Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

    – gee
    Feb 7 at 6:13











  • @gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

    – Akina
    Feb 7 at 6:45











  • ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

    – gee
    Feb 7 at 7:40











  • @gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

    – Akina
    Feb 7 at 7:45













  • @gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

    – Akina
    Feb 8 at 7:39





















  • Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

    – gee
    Feb 7 at 6:13











  • @gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

    – Akina
    Feb 7 at 6:45











  • ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

    – gee
    Feb 7 at 7:40











  • @gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

    – Akina
    Feb 7 at 7:45













  • @gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

    – Akina
    Feb 8 at 7:39



















Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

– gee
Feb 7 at 6:13





Hi I've tried your code but it gives me an error on copy of source. "Runtime error "9"" Subscript out of range

– gee
Feb 7 at 6:13













@gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

– Akina
Feb 7 at 6:45





@gee Trace the code. Detect the operator which causes the error, and look all variables values in Locals window.

– Akina
Feb 7 at 6:45













ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

– gee
Feb 7 at 7:40





ohh I figured it but I have another error on LBOUND and UBOUND function. it say that "Run-time error '6': Overflow". I not familiar with those functions.

– gee
Feb 7 at 7:40













@gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

– Akina
Feb 7 at 7:45







@gee I have another error on LBOUND and UBOUND function o_O Check in Locals - temp variable datatype must be 2-dimentional Array of Variants after temp = src.Value code line execution... Check if these functions are available itself (execute example code from help). And check that you copypast my code into new common module, and no global variables which interfere with variable names used in my code.

– Akina
Feb 7 at 7:45















@gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

– Akina
Feb 8 at 7:39







@gee what I have more than 282 rows? I have tested my code on 1000 rows array - 0.582 sec, and on 10000 rows array - 32.5 sec. Replacing the bubble sorting code with more effective one (for example, quicksort) may increase performance. I declare Call ReSort(Range("A:B"), Range("A:B")) In that case you tries to scan the whole (1M) rows... If you want to use column-ranged addresses, create wrapping procedure which detects the last used cell in a source range, build its address as rectangle-ranged, build proper destination range, then call my code.

– Akina
Feb 8 at 7:39




















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1402921%2fexcel-vba-combine-duplicates-and-add-column-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀