merging columns

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi the code below does not give me an error message but does not do the job either? Any help would be appreciated.
Code:
Sub col_merge()
    Dim x As Integer
    Dim y As Integer
    x = InputBox("first col#")
    y = InputBox("second col#")
    Workbooks(1).Worksheets(1).Columns("x:y").Merge
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What are you entering in the inputboxes?


Btw, your code as is will merge columns X & Y on Workbooks(1).Worksheets(1)
 
Last edited:
Upvote 0
Code:
    Workbooks(1).Worksheets(1).Columns("x:y").Merge

the code quoted is basically telling excel to merge the range x:y since you have it in quotes.
for whatever reason i found it easier to do the same thing using strings
so when it asks you which columns you would enter the letter rather than the number
this will however:
- merge anything in between the columns
- merge the entire sheet if you enter the numbers for the column

i'm sure somebody can give you a better code but this is a workaround to accomplish what you're trying to do
Code:
Sub col_merge()
    Dim wb As ThisWorkbook
    Dim ws As Worksheet
    Dim x As String
    Dim y As String
    
    Set ws = Worksheets(1)
    
    x = InputBox("first col#")
    y = InputBox("second col#")
    ws.Range(x & ":" & y).EntireColumn.Merge

End Sub
 
Upvote 0
@Lesawang, also just to point out that if you have a Personal Workbook then Workbooks(1) probably refers to that which you probably don't want.
 
Upvote 0
SOrry I entered 1 and 5. Thinking it will merge col#1 to col#5
I want to enter Column numbers and I want excel merge these columns. Yes I see now x and y are merged and that is not what I want. Thank you all.
 
Last edited:
Upvote 0
I don't think you will achieve that easily using column numbers, if they were adjacent columns then you could do the below but even then I don't think you will get the results you are expecting as when you merge cells it only keeps the top/left cells value and that will be centered halfway down the spreadsheet.

Code:
Sub col_merge()
    Dim x As Integer
    Dim y As Integer
    x = InputBox("first col#")
    y = InputBox("second col#")
    Application.DisplayAlerts = False
    With Worksheets(1)
        Union(.Columns(x), .Columns(y)).Merge
    End With
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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