+ Reply to Thread
Results 1 to 15 of 15

Using VBA to populate cells based on a criteria in another tab

Hybrid View

  1. #1
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Using VBA to populate cells based on a criteria in another tab

    Hi, I really want to learn VBA and its many uses in excel, but I'm more of a hands on learner. I have a simple project I'm trying to do for work that I know is possible through VBA, but I don't have the expertise to do it.

    Essentially we have a master list of property managers (PM) addresses and owners etc... in a tab called "Property List" and tabs for each of the property managers in the same file. I would like to populate the individual PM's tabs with their properties based on a search/copy/paste of the "Property List" tab

    Row 1 contains the headers ... there are 10 columns of information
    Row 2 begins the data
    Column 1 contiains the property manager Initial (the search criteria)

    I've tried this code but get a runtime error object required
    Sub copyrows()
         
        Dim tfCol As Range, Cell As Object
         
        Set tfCol = Range("A:A") 'Substitute with the range which includes your True/False values
         
        For Each Cell In tfCol
             
            If IsEmpty(Cell) Then
                Exit Sub
            End If
             
            If Cell.Value = "BC" Then
                Cell.EntireRow.Copy
                Brad.Select 'Substitute with your sheet
                ActiveSheet.Range("A65536").End(xlUp).Select
                Selection.Offset(1, 0).Select
                ActiveSheet.Paste
            End If
             
        Next
         
    End Sub
    Any helpful hints or suggestions is greatly appreciated. I would really like to learn this stuff so if you have suggestions on tutorials etc... I'm open!
    Last edited by mongoose36; 07-25-2013 at 09:07 AM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: New to VBA and want to learn

    Sub copyrows()
        Dim Cell As Range
        LR = Sheets("Brad").Range("A65536").End(xlUp).Row + 1
        For Each Cell In Range("A:A")
            If IsEmpty(Cell) Then Exit Sub
            If Cell.Value = "BC" Then
                Cell.EntireRow.Copy Sheets("Brad").Cells(LR, 1)
                LR = LR + 1
            End If
        Next
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: New to VBA and want to learn

    Wow! Awesome that worked perfectly thank you!!...would you mind walking me through the code so I can understand the arguments. I am familiar with some of the arguments having programed applescripts, but not all.

    Additionally, I want to make this dynamic...We currently have 12 property managers...in the future some may be added and some removed.

    I would like to expand this macro to say search a tab called "Property Manager List" which would contain initials and First names. The macro would then create a tab for each first name (if one doesn't exist already) then perform the operation of the code above for each Porperty managers initial. I need the code to be dynamic enough that everytime we remove and/or add a property manager I don't have to go in and change the code.

    Is this possible?

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: New to VBA and want to learn

    Quote Originally Posted by cplettner View Post
    I need the code to be dynamic enough that everytime we remove and/or add a property manager I don't have to go in and change the code.
    you are not asking for suggestions, but for a turnkey package.

  5. #5
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: New to VBA and want to learn

    Ok?? I think I understand what you are saying. I'm willing to do this on my own if you could point me to resources I could study. I really do want to learn this stuff, but don't know where to start?
    Last edited by mongoose36; 07-26-2013 at 12:38 PM.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Using VBA to populate cells based on a criteria in another tab

    Hi, cplettner,

    work through the range of all managers, apply an Autofilter to the sheet List with each single name, copy all visible cells to a new sheet, name that sheet. Maybe have a look at this:
    Sub EF941913()
    Dim rngCell As Range
    Dim rngArea As Range
    Dim wsList As Worksheet
    Dim wsManag As Worksheet
    Dim wsNew As Worksheet
    Dim lngLast As Long
    
    Set wsList = Sheets("Property List")
    Set wsManag = Sheets("Property Managers")
    
    With wsManag
      Set rngArea = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
    End With
    
    For Each rngCell In rngArea
      With wsList
        If .AutoFilterMode Then .Range("A1").AutoFilter
        .Range("A1").AutoFilter
        .Range("A1").AutoFilter , field:=1, Criteria1:=rngCell
        lngLast = .Range("A" & Rows.Count).End(xlUp).Row
        If lngLast > 1 Then
          .Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
          Set wsNew = Worksheets.Add(after:=Worksheets(Worksheets.Count))
          wsNew.Range("A1").PasteSpecial xlValues
          wsNew.Name = rngCell.Offset(0, 1).Value
          Set wsNew = Nothing
        End If
        .Range("A1").AutoFilter
      End With
    Next rngCell
    
    Set rngArea = Nothing
    Set wsManag = Nothing
    Set wsList = Nothing
    Application.CutCopyMode = False
    
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  7. #7
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Using VBA to populate cells based on a criteria in another tab

    I have no idea about your workbook

  8. #8
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Using VBA to populate cells based on a criteria in another tab

    I'm not quite sure how to take your answer?? If you mean you need additional information I have attached the workbook for you to look at. If on the other hand you mean you do not want to prusue this further I understand and thank you for your help thus far.
    Attached Files Attached Files

  9. #9
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Using VBA to populate cells based on a criteria in another tab

    Does anybody else have any suggestions?

  10. #10
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Using VBA to populate cells based on a criteria in another tab

    GENIOUS!! I REALLY appreciate your help. I'm going through the code trying to understand all the arguments. I admit it’s a bit beyond me, but hopefully with time I will understand it all. A couple of questions.

    If I want it to change the formats as well can I simply change
    wsNew.Range("A1").PasteSpecial xlValues
    to
    wsNew.Range("A1").Paste
    Also how do we update?? If I delete some of the properties and try to run the script again it creates another sheet and gives me an error. I had to delete the newly created tabs and run the script to see the change. Perhaps the way around this is to write a script at the beginning that would delete all the tabs except for "Property Managers" and "Property List" then run the rest of the script. Your thoughts

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Using VBA to populate cells based on a criteria in another tab

    Hi, cplettner,

    there are different ways to solve this: keep the sheets and clear the contents if teh code finds a suiting name. Or as you said delete all sheets apart from the two which are part of the sample workbook.

    Itīs up to you to decide what you think is suiting your taste/situation.

    Ciao,
    Holger

  12. #12
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Using VBA to populate cells based on a criteria in another tab

    As Far as deleting the sheets I was able to get this to work:

    Sub EF941913()
    Dim rngCell As Range
    Dim rngArea As Range
    Dim wsList As Worksheet
    Dim wsManag As Worksheet
    Dim wsNew As Worksheet
    Dim lngLast As Long
    Dim ws As Worksheet
    
    Application.DisplayAlerts = False
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name = "Property List" Or ws.Name = "Property Managers" Then
        Else
            ws.Visible = xlSheetVisible
            ws.Delete
        End If
    Next ws
    Application.DisplayAlerts = True
    
    Set wsList = Sheets("Property List")
    Set wsManag = Sheets("Property Managers")
    
    With wsManag
      Set rngArea = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
    End With
    
    For Each rngCell In rngArea
      With wsList
        If .AutoFilterMode Then .Range("A1").AutoFilter
        .Range("A1").AutoFilter
        .Range("A1").AutoFilter , field:=1, Criteria1:=rngCell
        lngLast = .Range("A" & Rows.Count).End(xlUp).Row
        If lngLast > 1 Then
          .Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
          Set wsNew = Worksheets.Add(after:=Worksheets(Worksheets.Count))
          wsNew.Range("A1").PasteSpecial xlValues
          wsNew.Name = rngCell.Offset(0, 1).Value
          Set wsNew = Nothing
        End If
        .Range("A1").AutoFilter
      End With
    Next rngCell
    
    Set rngArea = Nothing
    Set wsManag = Nothing
    Set wsList = Nothing
    Application.CutCopyMode = False
    
    End Sub
    How would do the first thing you mentioned??

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Using VBA to populate cells based on a criteria in another tab

    Hi, cplettner,

    code for keeping the sheets and only adding new ones if new names are introduced may look like this:
    Sub EF941913_2()
    Dim rngCell As Range
    Dim rngArea As Range
    Dim wsList As Worksheet
    Dim wsManag As Worksheet
    Dim wsNew As Worksheet
    Dim lngLast As Long
    
    Set wsList = Sheets("Property List")
    Set wsManag = Sheets("Property Managers")
    
    With wsManag
      Set rngArea = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
    End With
    
    For Each rngCell In rngArea
      With wsList
        If .AutoFilterMode Then .Range("A1").AutoFilter
        .Range("A1").AutoFilter
        .Range("A1").AutoFilter , field:=1, Criteria1:=rngCell
        lngLast = .Range("A" & Rows.Count).End(xlUp).Row
        If lngLast > 1 Then
          If WorksheetExists(rngCell.Offset(0, 1).Value) = False Then
            Set wsNew = Worksheets.Add(after:=Worksheets(Worksheets.Count))
          Else
            Set wsNew = Worksheets(rngCell.Offset(0, 1).Value)
            wsNew.UsedRange.ClearContents
          End If
          .Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
          wsNew.Range("A1").PasteSpecial xlValues
          wsNew.Name = rngCell.Offset(0, 1).Value
          Set wsNew = Nothing
        End If
        .Range("A1").AutoFilter
      End With
    Next rngCell
    
    Set rngArea = Nothing
    Set wsManag = Nothing
    Set wsList = Nothing
    Application.CutCopyMode = False
    
    End Sub
    
    Function WorksheetExists(ByVal WorksheetName As String) As Boolean
    
        On Error Resume Next
        WorksheetExists = (Sheets(WorksheetName).Name <> "")
        On Error GoTo 0
    
    End Function
    Ciao,
    Holger

  14. #14
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: Using VBA to populate cells based on a criteria in another tab

    This works wonders!! But how do I get it to delete a tab if I remove a property manager from the Property Mangers tab

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Using VBA to populate cells based on a criteria in another tab

    Hi, cplettner,

    two options from my point: either stay with what yoiu have right now an dthen loop thorugh all sheets and see if the name is in the list, or get your first idea out again to delete all sheets except those with data to distribute and then build the managers sheets up again.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need to learn VBA
    By MBeedle in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-26-2013, 03:12 PM
  2. Here to learn!
    By bigac in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 08-07-2012, 12:39 PM
  3. How to Learn VBA?
    By melleniam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2008, 09:55 AM
  4. To learn about VBA
    By Morrigan in forum Excel General
    Replies: 3
    Last Post: 08-09-2005, 09:33 AM
  5. How to learn VBA
    By Thrava in forum Excel General
    Replies: 3
    Last Post: 01-07-2005, 09:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1