entering row and col for a cell in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
Is it possible to enter both row, col in the inputbox at same time. Please see the code below. I have to ask user to enter row, col 4 times. I was thinking if I can ask user only 2 times to enter row and col of each cell at same time. Thank you

' cell merge
Sub cellmerge()
Dim x As Integer
Dim y As Integer
Dim n As Integer
Dim m As Integer
x = InputBox("first cell row")
y = InputBox("first cell col")
n = InputBox("second cell row")
m = InputBox("second cell col")
ThisWorkbook.ActiveSheet.Range(Cells(x, y), Cells(n, m)).Merge
End Sub

ps: I used indent but i lose it when i save this post!
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You could parse the result of the InputBox like this:

Code:
' cell merge
Sub cellmerge()
Dim x As Long, y As Long, n As Long, m As Long
Dim inpt As String, splt As Variant

    inpt = InputBox("Enter row,column:")
    splt = Split(inpt, ",")
    x = splt(0)
    y = splt(1)
    inpt = InputBox("Enter second row,column:")
    splt = Split(inpt, ",")
    n = splt(0)
    m = splt(1)
    ThisWorkbook.ActiveSheet.Range(Cells(x, y), Cells(n, m)).Merge
End Sub

To maintain indentation, use code tags. Like this:

[ code]

... code goes here ...

[ /code]

Remove the spaces from the codes though. Or you could just select your entire code and click the # button from the menu, which will automatically insert them.
 
Last edited:
Upvote 0
Have a look at
Code:
Application.InputBox

Also when posting code please use code tags, the # icon in the reply window
 
Upvote 0
Hi
Is it possible to enter both row, col in the inputbox at same time. Please see the code below. I have to ask user to enter row, col 4 times. I was thinking if I can ask user only 2 times to enter row and col of each cell at same time. Thank you

' cell merge
Sub cellmerge()
Dim x As Integer
Dim y As Integer
Dim n As Integer
Dim m As Integer
x = InputBox("first cell row")
y = InputBox("first cell col")
n = InputBox("second cell row")
m = InputBox("second cell col")
ThisWorkbook.ActiveSheet.Range(Cells(x, y), Cells(n, m)).Merge
End Sub
If you use this code, the user can input the range normally (for example, like this C4:K9) or, alternately, they can simply select the range just like selecting any range (mostly likely using the mouse, but using keystrokes is also possible).



ps: I used indent but i lose it when i save this post!
If you select the code and then click the # symbol in the ribbon at the top, it will automatically place code tags around the selected code and your spacing will be preserved.
 
Upvote 0
If you use this code, the user can input the range normally (for example, like this C4:K9) or, alternately, they can simply select the range just like selecting any range (mostly likely using the mouse, but using keystrokes is also possible).
Hmm! Looks like I had forgotten to post the code line :oops:

Application.InputBox("Either enter or select the range to merge", Type:=8).Merge
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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