Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 14
Default Help with VBA Text

Instead of always selecting every 20th row...I want VBA to prompt me
the quantity of rows. I think I can tell VBA to prompt the qty of
rows I want to select instead of defaulting to 20. Here is the VBA
string:

Sub SelectEveryNthRow()
' Initialize ColsSelection equal to the number of columns in the
' selection.
ColsSelection = Selection.Columns.Count
' Initialize RowsSelection equal to the number of rows in your
' selection.
RowsSelection = Selection.Rows.Count
' Initialize RowsBetween equal to Twenty.
RowsBetween = 20
' Initialize Diff equal to one row less than the first row number
of
' the selection.
Diff = Selection.Row - 1
' Resize the selection to be 1 column wide and the same number of
' rows long as the initial selection.
Selection.Resize(RowsSelection, 1).Select
' Resize the selection to be every twentieth row and the same
number of
' columns wide as the original selection.
Set FinalRange = Selection. _
Offset(RowsBetween - 1, 0).Resize(1, ColsSelection)
' Loop through each cell in the selection.
For Each xCell In Selection
' If the row number is a multiple of 3, then . . .
If xCell.Row Mod RowsBetween = Diff Then
' ...reset FinalRange to include the union of the current
' FinalRange and the same number of columns.
Set FinalRange = Application.Union _
(FinalRange, xCell.Resize(1, ColsSelection))
' End check.
End If
' Iterate loop.
Next xCell
' Select the requested cells in the range.
FinalRange.Select
End Sub

What can I change to make this adjustment?

Thanks

Brandon G.
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Help with VBA Text

You can use inputbox to get a response from the user. But this doesn't have to
be numeric.

But you can use application.inputbox to ask the user for a number.

Option Explicit
Sub SelectEveryNthRow2()

Dim myRng As Range
Dim myNthRng As Range
Dim HowMany As Long
Dim iCtr As Long

Set myRng = Selection.Areas(1)

HowMany = CLng(Application.InputBox(Prompt:="how many rows?", Type:=1))

If HowMany < 1 Then
Exit Sub
End If

If HowMany myRng.Rows.Count Then
MsgBox "Hmmmm. Can't do that with this selection!"
Exit Sub
End If

'skip the first row
For iCtr = myRng.Row - 1 + HowMany To myRng.Rows.Count Step HowMany
If myNthRng Is Nothing Then
Set myNthRng = myRng.Rows(iCtr)
Else
Set myNthRng = Union(myNthRng, myRng.Rows(iCtr))
End If
Next iCtr

If myNthRng Is Nothing Then
MsgBox "Something bad happened"
Else
myNthRng.Select
End If

End Sub


"Brandon G." wrote:

Instead of always selecting every 20th row...I want VBA to prompt me
the quantity of rows. I think I can tell VBA to prompt the qty of
rows I want to select instead of defaulting to 20. Here is the VBA
string:

Sub SelectEveryNthRow()
' Initialize ColsSelection equal to the number of columns in the
' selection.
ColsSelection = Selection.Columns.Count
' Initialize RowsSelection equal to the number of rows in your
' selection.
RowsSelection = Selection.Rows.Count
' Initialize RowsBetween equal to Twenty.
RowsBetween = 20
' Initialize Diff equal to one row less than the first row number
of
' the selection.
Diff = Selection.Row - 1
' Resize the selection to be 1 column wide and the same number of
' rows long as the initial selection.
Selection.Resize(RowsSelection, 1).Select
' Resize the selection to be every twentieth row and the same
number of
' columns wide as the original selection.
Set FinalRange = Selection. _
Offset(RowsBetween - 1, 0).Resize(1, ColsSelection)
' Loop through each cell in the selection.
For Each xCell In Selection
' If the row number is a multiple of 3, then . . .
If xCell.Row Mod RowsBetween = Diff Then
' ...reset FinalRange to include the union of the current
' FinalRange and the same number of columns.
Set FinalRange = Application.Union _
(FinalRange, xCell.Resize(1, ColsSelection))
' End check.
End If
' Iterate loop.
Next xCell
' Select the requested cells in the range.
FinalRange.Select
End Sub

What can I change to make this adjustment?

Thanks

Brandon G.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 14
Default Help with VBA Text

On Nov 16, 11:06 am, Dave Peterson wrote:
You can use inputbox to get a response from the user. But this doesn't have to
be numeric.

But you can use application.inputbox to ask the user for a number.

Option Explicit
Sub SelectEveryNthRow2()

Dim myRng As Range
Dim myNthRng As Range
Dim HowMany As Long
Dim iCtr As Long

Set myRng = Selection.Areas(1)

HowMany = CLng(Application.InputBox(Prompt:="how many rows?", Type:=1))

If HowMany < 1 Then
Exit Sub
End If

If HowMany myRng.Rows.Count Then
MsgBox "Hmmmm. Can't do that with this selection!"
Exit Sub
End If

'skip the first row
For iCtr = myRng.Row - 1 + HowMany To myRng.Rows.Count Step HowMany
If myNthRng Is Nothing Then
Set myNthRng = myRng.Rows(iCtr)
Else
Set myNthRng = Union(myNthRng, myRng.Rows(iCtr))
End If
Next iCtr

If myNthRng Is Nothing Then
MsgBox "Something bad happened"
Else
myNthRng.Select
End If

End Sub





"Brandon G." wrote:

Instead of always selecting every 20th row...I want VBA to prompt me
the quantity of rows. I think I can tell VBA to prompt the qty of
rows I want to select instead of defaulting to 20. Here is the VBA
string:


Sub SelectEveryNthRow()
' Initialize ColsSelection equal to the number of columns in the
' selection.
ColsSelection = Selection.Columns.Count
' Initialize RowsSelection equal to the number of rows in your
' selection.
RowsSelection = Selection.Rows.Count
' Initialize RowsBetween equal to Twenty.
RowsBetween = 20
' Initialize Diff equal to one row less than the first row number
of
' the selection.
Diff = Selection.Row - 1
' Resize the selection to be 1 column wide and the same number of
' rows long as the initial selection.
Selection.Resize(RowsSelection, 1).Select
' Resize the selection to be every twentieth row and the same
number of
' columns wide as the original selection.
Set FinalRange = Selection. _
Offset(RowsBetween - 1, 0).Resize(1, ColsSelection)
' Loop through each cell in the selection.
For Each xCell In Selection
' If the row number is a multiple of 3, then . . .
If xCell.Row Mod RowsBetween = Diff Then
' ...reset FinalRange to include the union of the current
' FinalRange and the same number of columns.
Set FinalRange = Application.Union _
(FinalRange, xCell.Resize(1, ColsSelection))
' End check.
End If
' Iterate loop.
Next xCell
' Select the requested cells in the range.
FinalRange.Select
End Sub


What can I change to make this adjustment?


Thanks


Brandon G.


--

Dave Peterson- Hide quoted text -

- Show quoted text -


thx so much
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text does not display in "Text boxs" and when wrapping text in a c Esteban Excel Discussion (Misc queries) 1 March 8th 07 11:59 PM
Counting a mixed text/number column based on text in another colum Sierra Vista Steve Excel Discussion (Misc queries) 3 December 17th 06 05:30 PM
merged cells into one text cell, size varies dependant on text dat Jazzylady825 Excel Discussion (Misc queries) 0 December 9th 05 08:26 PM
convert a range of lowercase text to upper text or vice versa jackdaw Excel Worksheet Functions 2 May 16th 05 09:31 PM
Macro or Function to make text size to suite text Length? lbbss Excel Discussion (Misc queries) 4 December 14th 04 07:53 PM


All times are GMT +1. The time now is 12:07 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"