MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 2nd, 2002, 08:42 PM   #1
brettvba
MrExcel MVP
 
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
Default

Public sht, k, i, j, numrow
Sub SelectNumRows()
Dim m, n
sht = ActiveSheet.Name
numrow = (CLng(InputBox("Enter Number Of Extra Rows Needed!", "Hello")))
If numrow = Empty Then Exit Sub
Range("A4").Select
Selection.End(xlDown).Select
m = ActiveCell.Offset(0, 2).Address
n = ActiveCell.Offset(0, 3).Address
SelectNumRowCode
Range(m).Select
SelectNumRowCode
Range(n).Select
SelectNumRowCode
End Sub


Sub SelectNumRowCode()
i = ActiveCell.Address
j = ActiveCell.Offset((numrow), 0).Address
k = (i) & ":" & (j)
Worksheets(sht).Range(k).filldown

End Sub

_________________


[ This Message was edited by: brettvba on 2002-05-02 19:44 ]

[ This Message was edited by: brettvba on 2002-05-02 19:45 ]
brettvba is offline   Reply With Quote
Old May 2nd, 2002, 10:10 PM   #2
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Try,

Code:
Sub test()
Dim lastrow As Long, numrow As Long
Dim MyArr, x

On Error Resume Next
numrow = WorksheetFunction.Max(0, (CLng(InputBox("Enter Number Of Extra Rows Needed!", "Hello"))))
If numrow = Empty Then Exit Sub
On Error GoTo 0

MyArr = Array("A", "C", "D")
With ActiveSheet
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
        For x = 0 To UBound(MyArr)
            .Range(MyArr(x) & lastrow & ":" & MyArr(x) & lastrow + numrow).FillDown
        Next x
End With

End Sub
In testing the routines, I tried to enter a negative number. It actually works by doing a "FillUp." Try it. I thought that was pretty cool.

Anyway, the MAX() function guards against that possibility. Remove it if desired.

The array MyArr will allow you to change/add/delete the columns you want without much code editing.

OK, I'm outta here! Bye!
Jay Petrulis is offline   Reply With Quote
Old May 2nd, 2002, 10:15 PM   #3
brettvba
MrExcel MVP
 
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
Default

thanks Jay Im not quite up with arrays yet
brettvba is offline   Reply With Quote
Old May 2nd, 2002, 10:25 PM   #4
Bertie Bagshot
 
Join Date: Feb 2002
Posts: 36
Default

Here's another way :-

Sub SelectNumRows()
Dim numRow#
On Error Resume Next
numRow = (CLng(InputBox("Enter Number Of Extra Rows Needed!", "Hello")))
On Error GoTo 0
If numRow = Empty Then Exit Sub
With [A4].End(xlDown)
.Resize(numRow + 1, 1).FillDown
.Offset(0, 2).Resize(numRow + 1, 2).FillDown
End With
End Sub



[ This Message was edited by: Bertie Bagshot on 2002-05-02 21:36 ]

[ This Message was edited by: Bertie Bagshot on 2002-05-02 21:38 ]
Bertie Bagshot is offline   Reply With Quote
Old May 2nd, 2002, 10:38 PM   #5
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-05-02 21:15, brettvba wrote:
thanks Jay Im not quite up with arrays yet
Hi,

Nobody is good with arrays, unless your name is Alan Beban (and a few others, of course).

I am trying to learn these myself, so I try to throw things out to the board. I help *AND* I test new ideas, so its a win-win I hope.

Originally, I had this code

With ActiveSheet
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("A" & lastrow & ":A" & lastrow + numrow).FillDown
.Range("C" & lastrow & ":C" & lastrow + numrow).FillDown
.Range("D" & lastrow & ":D" & lastrow + numrow).FillDown
End With

You'll recognize this syntax, but what if you want to add 100 more colums to the fill down? That means 100 more lines of code.

With the array, the first element (starts at 0 unless you explicitly tell it not to) is "A" so my loop places "A" wherever it sees MyArr(0). It then goes to 1 and places the second element "C" wherever it sees MyArr(1) and so on.

The UBound is allows VB to determine how many elements are in the array, rather than me. I initially had it 0 to 2, but if I need to add another column, I would have to change the loop to "0 to 3" and so on. I'm nothing if not lazy, so I want to build the automatic adjustments from the start and then I'm done.

Array formulas are very useful and powerful, but can be overused. Working with arrays in VBA are generally a different story. Most Excellers (definitely including me) would be well advised to learn as much as they can about these.

Bye,
Jay

P.S. Alan Beban, an Excel MVP, has an array function add-in on his website (don't know it off hand) that is way beyond anything I understand. Among other things, it allows you to ReDim any dimension of a multi-dimensional array (VBA only allows the last dimension) and many other things.
Jay Petrulis is offline   Reply With Quote
Old May 5th, 2002, 02:08 PM   #6
brettvba
MrExcel MVP
 
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
Default

Great thanks Jay
brettvba is offline   Reply With Quote
Old May 5th, 2002, 04:16 PM   #7
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Bertie's routine (very nice, btw) also lends itself to being written using a simple array

Code:
Sub tester()
Dim MyArr, x As Long

On Error Resume Next
numrow = WorksheetFunction.Max(0, CLng(InputBox("Enter Number Of Extra Rows Needed!", "Hello")))
If numrow = Empty Then Exit Sub
On Error GoTo 0

MyArr = Array(0, 2, 3)

With ActiveSheet.Range("A4").End(xlDown)
    For x = LBound(MyArr) To UBound(MyArr)
        .Offset(0, MyArr(x)).Resize(numrow + 1, 1).FillDown
    Next x
End With

End Sub
Jay Petrulis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 06:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes