• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

VBA/Macro to insert rows based on drop downlist selection|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA/Macro to insert rows based on drop downlist selection|VBA & Macros|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA/Macro to insert rows based on d…
sp_PrintTopic sp_TopicIcon
VBA/Macro to insert rows based on drop downlist selection
Avatar
Jet Fusion

New Member
Members
Level 0
Forum Posts: 2
Member Since:
November 11, 2020
sp_UserOfflineSmall Offline
1
November 11, 2020 - 4:38 pm
sp_Permalink sp_Print

Hi

I'm not sure how or get this to work.

I would like to add 3 rows based on a selection from a drop down list or would it be better to use a macro to ask which selection/name and then to insert 3 rows.

Once the rows have been inserted the firs row should be the Name that was selcted in bold and a border around the 3 rows everytime it get inserted and the the two of the rows should have info based on the name that was selected from the drop down list.

There will be a list of different selections so would it be better to make a data sheet and then the info can be autofilled to the specific rows.

Also everytime you make a selection the 3 rows that get added will have to continue from the last row that was added. So every 3rd row will have a dropdown list.

I have attached a file

Thanks in advance
Jet

Avatar
Jet Fusion

New Member
Members
Level 0
Forum Posts: 2
Member Since:
November 11, 2020
sp_UserOfflineSmall Offline
2
November 12, 2020 - 12:11 am
sp_Permalink sp_Print

My apologies

The following site have the same question added:

1. https://www.ozgrid.com/forum/i.....ost1241332

2. https://chandoo.org/forum/threads/vb...ownlist.45283/

3. https://www.excelguru.ca/forum.....-selection

4. http://www.vbaexpress.com/forum/show...list-selection

5. https://www.mrexcel.com/board/thread.../#post-5585077

 

Hope thats all the sites.

Avatar
Miguel Santos
Member
Members
Level 0
Forum Posts: 80
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
3
November 13, 2020 - 9:56 pm
sp_Permalink sp_Print
Good morning everyone,

I apologize for the delay on this topic, without much time available
I didn't understand everything you wanted, but here is an example with some parts that I understood
I attached a file

 

Option Explicit

Dim nextStepProgram As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)

Dim intRowCount As Integer

If nextStepProgram = False Then
     nextStepProgram = True
     intRowCount = CheckNumb
     Call Update_DataValidation(intRowCount)
     nextStepProgram = False
End If

Call getAllCellsWithValidation

End Sub

Private Function CheckNumb() As Integer

Dim i As Integer
Dim nextStep As Boolean

i = 1
nextStep = True

While nextStep = True
     If Cells(i, 1) <> "" Then
         i = i + 1
     Else
         nextStep = False
     End If
Wend

CheckNumb = i - 1

End Function

Private Sub Update_DataValidation(ByVal intRow As Integer)

Dim LastRow As Long
Dim ws As Worksheet
Dim range1 As Range, Rng As Range

Application.ScreenUpdating = False

Set ws = Application.ThisWorkbook.Worksheets("Folha2")

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Debug.Print LastRow

Set Rng = ws.Range("A1:A" & LastRow)
Rng.RemoveDuplicates Columns:=1, Header:=xlNo

Set range1 = ws.Range("A1:A" & LastRow)

With Sheet1.Range("J2").Validation
     .Delete
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
     Operator:=xlBetween, Formula1:="='" & ws.Name & "'!" & range1.Address
     .IgnoreBlank = True
     .InCellDropdown = True
     .InputTitle = ""
     .ErrorTitle = ""
     .InputMessage = ""
     .ErrorMessage = ""
     .ShowInput = True
     .ShowError = True
End With

Application.ScreenUpdating = True

End Sub

Private Sub getAllCellsWithValidation()

Dim cell As Range, v As Long
Dim LastRow As Long
Dim ws As Worksheet
Dim xcell As String
Dim ArrInput() As Integer, i As Integer
Dim x As Integer
Dim xrange As Range
Dim a As Integer

Application.ScreenUpdating = False

Set ws = Application.ThisWorkbook.Worksheets("Folha1")

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Debug.Print LastRow

Set xrange = ws.Range("J1:J" & LastRow)

On Error Resume Next
ReDim ArrInput(0)
On Error GoTo 0
i = 0

For Each cell In xrange.Cells
     v = 0
     On Error Resume Next
     v = cell.SpecialCells(xlCellTypeSameValidation).Count
     On Error GoTo 0

     If v = 0 Then
         ' MsgBox "No validation!"
     Else
         ' MsgBox "Has validation!"
         ' x = x & vbCrLf & cell.Address
         ' x = x & vbCrLf & cell.Row
         xcell = cell.Row
         ArrInput(i) = xcell
         i = i + 1
         ReDim Preserve ArrInput(i)

     End If
Next cell

If i = 0 Then Exit Sub

With Application.ThisWorkbook.Worksheets("Folha1")

For x = LBound(ArrInput) To UBound(ArrInput)
     a = ArrInput(x)
     If a <> 0 Then
         If ActiveCell.Column = 10 And ActiveCell.Row = a Then
             If ActiveCell.Value <> "" Then
                     Call addNewRowWithDropDownList(a)
             Else
                 'Cancel is true
             End If
         Else
             'Cancel is true
         End If
     End If
Next x

End With

Application.ScreenUpdating = True

End Sub

Private Sub addNewRowWithDropDownList(rNum As Integer)

Dim RowNum As Integer
Dim LastRow As Long, LastRow2 As Integer
Dim ws As Worksheet
Dim range1 As Range, Rng As Range
Dim x As Integer

Application.ScreenUpdating = False

Set ws = Application.ThisWorkbook.Worksheets("Folha2")

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Debug.Print LastRow

Set Rng = ws.Range("A1:A" & LastRow)
Rng.RemoveDuplicates Columns:=1, Header:=xlNo

Set range1 = ws.Range("A1:A" & LastRow)

RowNum = Application.ThisWorkbook.Worksheets("Folha1").Range("J" & rNum).Cells.Row
If RowNum = 0 Then Exit Sub

Range("J1").Offset(RowNum, 0).Select
x = Range("J1").Offset(RowNum, 0).Cells.Row

LastRow2 = Application.ThisWorkbook.Worksheets("Folha1").Cells(Application.ThisWorkbook.Worksheets("Folha1").Rows.Count, "A").End(xlUp).Row
Debug.Print LastRow2

'Application.ThisWorkbook.Worksheets("Folha1").Range("A" & x & ":" & "A" & x + 2).EntireRow.Insert
'Application.ThisWorkbook.Worksheets("Folha1").Range("J" & x).Select

With Application.ThisWorkbook.Worksheets("Folha1")
     .Range("A" & LastRow2 + 1 & ":" & "A" & LastRow2 + 3).EntireRow.Insert
     .Range("A" & LastRow2 + 1).Value = "info line 1"
     .Range("A" & LastRow2 + 2).Value = "info line 2"
     .Range("A" & LastRow2 + 3).Value = "info line 3"
     .Range("A" & LastRow2 + 1 & ":" & "M" & LastRow2 + 1).Borders(xlEdgeTop).LineStyle = xlContinuous
     .Range("A" & LastRow2 + 3 & ":" & "M" & LastRow2 + 3).Borders(xlEdgeBottom).LineStyle = xlContinuous
     .Range("A" & LastRow2 + 1 & ":" & "A" & LastRow2 + 3).Borders(xlEdgeLeft).LineStyle = xlContinuous
     .Range("M" & LastRow2 + 1 & ":" & "M" & LastRow2 + 3).Borders(xlEdgeRight).LineStyle = xlContinuous
     .Columns.AutoFit
     .Rows.WrapText = False
End With

Application.ThisWorkbook.Worksheets("Folha1").Range("J" & LastRow2).Select

With Selection.Validation
     .Delete
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
     Operator:=xlBetween, Formula1:="='" & ws.Name & "'!" & range1.Address
     .IgnoreBlank = True
     .InCellDropdown = True
     .InputTitle = ""
     .ErrorTitle = ""
     .InputMessage = ""
     .ErrorMessage = ""
     .ShowInput = True
     .ShowError = True
End With

Application.ScreenUpdating = True

If Not ws Is Nothing Then Set ws = Nothing

With Application.ThisWorkbook.Worksheets("Folha1")
     .Select
     .Range("J" & LastRow2 + 1).Validation.Delete
     .Range("J" & LastRow2 + 2).Validation.Delete
End With

End Sub

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Magnolia Stellata
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 216
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Melanie Ford
Isaac Felbah
Adele Glover
Hitesh Asrani
Rohan Abraham
Anthony van Riessen
Erlinda Eloriaga
Abisola Ogundele
MARTYN STERRY
Rahim Lakhani
Forum Stats:
Groups: 3
Forums: 24
Topics: 6356
Posts: 27793

 

Member Stats:
Guest Posters: 49
Members: 32324
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.