How do I Select multiple rows using multipe variables?

thediscopossum

New Member
Joined
May 1, 2014
Messages
2
I am currently attempting to select multiple rows (Rows are variables) to group via a macro.

The problem is, it is not selecting the rows i want.
It seems to be putting the variables together as opposed to adding the totals together
ie, if MSTextBox = 3 and AVTextBox = 4, It groups Rows 4-34. Not Rows 4-8

Also the variables are taken from a text box within a userform.

Private Sub MSTextBox_Change()
Dim MSTextBox As Integer
End Sub

Private Sub AVTextBox_Change()
Dim MSTextBox As Integer
End Sub

Private Sub FinishButton_Click()
Rows((MSTextBox + 1) & " : " & ((MSTextbox) + (AVTextBox))).Select
Selection.Rows.Group
End Sub

Any assistance is greatly appreciated
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi and Welcome to MrExcel,

The TextBox.Value property will be returned as a String data type.

It looks like you were trying to change the data type of the TextBoxes through statements like this, but that won't work.
Code:
Dim MSTextBox As Integer

Even though you are using an "+" addition operator, VBA concatenates the two strings instead of adding them.

Sometimes VBA will coerce Strings that have numeric values into Numbers, but a best practice in coding is to be explicit in using Conversion Functions to change the data type to what is needed.

Here's one way to code that.

Code:
Private Sub FinishButton_Click()

 If IsNumeric(MSTextBox.Value) And _
   IsNumeric(AVTextBox.Value) Then
   
   Rows(MSTextBox.Value & ":" & CStr( _
      CInt(MSTextBox.Value) + _
      CInt(AVTextBox.Value))).Group
 Else
   MsgBox "Invalid data: AV and MS must be numbers"
 End If
End Sub

It validates that the entries are numeric. You could go one step further and validate that the numbers are within a specified min and max value range.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,727
Messages
6,174,139
Members
452,546
Latest member
Rafafa

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