Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 447

Excel VBA: Adding Rows and Columns Based on User Input

$
0
0

I'm trying to build a table based upon user inputs: number of Areas, number of Floors, and number of Buildings. It should automatically create the table based on user input.

example of an output would be:

enter image description here

I figured out how to use vba to copy/paste X Amount of columns (buildings) based on user input and I figured out how to use the filldown in vba for the areas, but I can't get past adding floors automatically, and I can't tie it all together in the table. I would normally not even both with something like this, but it's an ask from the leadership team. My VBA skills (and imagination) have hit a wall.

example of code for the columns being added:

Private Sub cmd_Button_Columns_Click()'  Add BuildingsDim N As IntegerN = Range("A2").ValueSelect Case NCase 1' Copy_Sheet = "Employing VBA Macros"Copy_Cell = "f1:i7"' Paste_Sheet = "Employing VBA Macros"Paste_Cell = "k1:n7"' Worksheets(Copy_Sheet).Range(Copy_Cell).CopyMe.Range(Copy_Cell).Copy' Worksheets(Paste_Sheet).Range(Paste_Cell).PasteSpecialMe.Range(Paste_Cell).PasteSpecial Paste:=xlPasteAllCase 2Copy_Cell = "f1:i7"Paste_Cell = "k1:n7"Me.Range(Copy_Cell).CopyMe.Range(Paste_Cell).PasteSpecial Paste:=xlPasteAllPaste_Cell = "p1:s7"Me.Range(Paste_Cell).PasteSpecial Paste:=xlPasteAllCase 3Copy_Cell = "f1:i7"Paste_Cell = "k1:n7"Me.Range(Copy_Cell).CopyMe.Range(Paste_Cell).PasteSpecial Paste:=xlPasteAllPaste_Cell = "p1:s7"Me.Range(Paste_Cell).PasteSpecial Paste:=xlPasteAllPaste_Cell = "u1:x7"Me.Range(Paste_Cell).PasteSpecial Paste:=xlPasteAllCase 0Range("k1:z7").ClearContentsEnd SelectEnd Sub

And for the rows I tried the following:

Sub Add_Areas()' Add AreasDim Resizer As IntegerDim a As Varianta = InputBox("Please enter the MAX number of Areas for your project", "NUMBER OF AREAS") 'First we ask for user inputOn Error GoTo notvalid  'We add an error handler, so if the user would enter text like "seven", the sub will exit with a messageResizer = CInt(a)       'we store the input in a variable which has to be an integer, if the user enters text it will couse an error so we jump to the endIf Resizer < 2 Then GoTo notvalid 'We also check if the number is higher than 1, othervise it couses error, or copies the 19th row to the 20thOn Error GoTo 0 'We reset the error handling so we can see if something else goes wrong.ThisWorkbook.Sheets("Sheet1").Visible = TrueThisWorkbook.Sheets("Sheet1").SelectThisWorkbook.Sheets("Sheet1").Rows(20 + 1).EntireRow.Insert shift:=xlDown 'add a new row under the 20th row/above the 21st rowThisWorkbook.Sheets("Sheet1").Rows(20).Resize(Resizer).FillDown  Exit Sub    'We exit the sub before the error message.notvalid: 'in case of error we jump here    MsgBox "Please enter a number which is 2 or higher"End Sub

Viewing all articles
Browse latest Browse all 447

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>