View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
bawpie bawpie is offline
external usenet poster
 
Posts: 16
Default Combine columns from seperate sheets into new sheet- macro improve

Well, after posting this problem at Mr Excel, I was pretty much handed a
re-written solution which is now working perfectly. Just in case anyone else
is ever curious as to how this can be done, here is the link:
http://www.mrexcel.com/forum/showthr...=1#post2206514

"bawpie" wrote:

I've managed to create a macro that will take up to ten columns from one
sheet and combine them with up to ten columns from another sheet. It's
working as well as I could have hoped, but I'm positive that there are ways
to make the macro far more effecient than it currently is. For instance, for
each column I have an individual process to handle picking the column from
sheet1 and combining it with the column from sheet2 10 times - I'm sure this
process could be written to loop 10 times off one bit of code, but I'm
uncertain how to do it. Ideally, however, I would like to be able to define
how many columns I wish to combine on a sheet - again I have a vague idea
that it would involve setting a variable input by the user and then possibly
using that variable in a for next loop but I really don't know enough to get
this working.

The code for the macro is below, I'm not sure if I can attach an example
workbook to this post? Hopefully it makes some sense, and any suggestions,
ideas or improvements you can suggest will be most appreciated!

Sub CombineMacro1()

Application.ScreenUpdating = False

'This part of the macro sets the variables for the first part of the macro
(taking data from first sheet and adding to combined sheet.)

Dim A As Range

Dim Sheet1 As String

Dim Sheet2 As String

Dim Column1 As String

Dim Column2 As String

Dim Column3 As String

Dim Column4 As String

Dim Column5 As String

Dim Column6 As String

Dim Column7 As String

Dim Column8 As String

Dim Column9 As String

Dim Column10 As String


'This part of the macro identifies columns from a specified sheet to move
via user input.

NameWorksheets:

Sheet1 = InputBox("Enter name of 1st worksheet to combine")

Sheet2 = InputBox("Enter name of 2nd worksheet to combine")

'This part of the macro adds 2 extra worksheets, combined and combined
reference.

ActiveWorkbook.Sheets.Add
after:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets .Count)
ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count) .Name = "Combined"

ActiveWorkbook.Sheets.Add
after:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets .Count)
ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count) .Name = "Combined Reference"

'This part of the macro populates combined reference sheet with headers from
sheet1 and sheet2.

Sheets(Sheet1).Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Combined Reference").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=True
Rows("1:1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.RowHeight = 12.75
Range("A1").Select
Sheets(Sheet2).Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Combined Reference").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=True
Range("A1").Select
Columns.AutoFit

'This part of the macro asks the user to input the columns they wish to
combine.

Application.ScreenUpdating = True

Sheets("Combined Reference").Select

Column1 = Application.InputBox _
(Prompt:="Enter name of 1st column to add to Combine Sheet from
" & Sheet1 & ".", Type:=2)

Column2 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & ". You have currently added " & Column1 & ".", Type:=2)

Column3 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & ".",
Type:=2)

Column4 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & ".", Type:=2)


Column5 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & "," & Column4 & ".", Type:=2)

Column6 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & "," & Column4 & "," & Column5 & ".", Type:=2)

Column7 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & "," & Column4 & "," & Column5 & _
"," & Column6 & ".", Type:=2)

Column8 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & "," & Column4 & "," & Column5 & _
"," & Column6 & "," & Column7 & ".", Type:=2)

Column9 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & "," & Column4 & "," & Column5 & _
"," & Column6 & "," & Column7 & "," & Column8 & ".", Type:=2)

Column10 = Application.InputBox _
(Prompt:="Enter name of next column to Combine Sheet from " &
Sheet1 & _
". You have currently added " & Column1 & "," & Column2 & "," &
Column3 & "," & Column4 & "," & Column5 & _
"," & Column6 & "," & Column7 & "," & Column8 & "," & Column9 &
".", Type:=2)

Application.ScreenUpdating = False

'This part of the macro looks for specified columns in specified sheets and
moves to combined sheet if found.

Sheets(Sheet1).Select
Set A = Rows(1).Find(What:=Column1, LookIn:=xlValues, lookat:=xlPart)
If Column1 = "" Then 'If the user has entered no value into
the input box then this part is skipped.

ElseIf A Is Nothing Then
MsgBox "No column by that name"

ElseIf A = Column1 Then
A.EntireColumn.Copy 'Because this is the first sheet to be
combined we can just copy the entire column from the sheet.
Sheets("Combined").Select
Range("A1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If

Set A = Rows(1).Find(What:=Column2, LookIn:=xlValues, lookat:=xlPart)
If Column2 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"
ElseIf A = Column2 Then
A.EntireColumn.Copy
Sheets("Combined").Select
Range("B1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If

Set A = Rows(1).Find(What:=Column3, LookIn:=xlValues, lookat:=xlPart)
If Column3 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"

ElseIf A = Column3 Then
A.EntireColumn.Copy
Sheets("Combined").Select
Range("C1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If

Set A = Rows(1).Find(What:=Column4, LookIn:=xlValues, lookat:=xlPart)
If Column4 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"

ElseIf A = Column4 Then
A.EntireColumn.Copy
Sheets("Combined").Select
Range("D1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If


Set A = Rows(1).Find(What:=Column5, LookIn:=xlValues, lookat:=xlPart)
If Column5 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"

ElseIf A = Column5 Then
A.EntireColumn.Copy
Sheets("Combined").Select
Range("E1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If

Set A = Rows(1).Find(What:=Column6, LookIn:=xlValues, lookat:=xlPart)
If Column6 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"

ElseIf A = Column6 Then
A.EntireColumn.Copy
Sheets("Combined").Select
Range("F1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If

Set A = Rows(1).Find(What:=Column7, LookIn:=xlValues, lookat:=xlPart)
If Column7 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"

ElseIf A = Column7 Then
A.EntireColumn.Copy
Sheets("Combined").Select
Range("G1").Select
ActiveSheet.Paste
Sheets(Sheet1).Select
Cells(1).Select
ElseIf A = "" Then

End If

Set A = Rows(1).Find(What:=Column8, LookIn:=xlValues, lookat:=xlPart)
If Column8 = "" Then

ElseIf A Is Nothing Then
MsgBox "No column by that name"