+ Reply to Thread
Results 1 to 17 of 17

Convert Table1 To Range and Edit Vba With Change

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Convert Table1 To Range and Edit Vba With Change

    Hi,

    Can the table on the AFRsInput sheet be converted to a range and edit the codes to allow for the change?

    The password is "pass" for this sheet.

    The range requested is O2:Q23 please.

    Thank you,
    FF

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,518

    Re: Convert Table1 To Range and Edit Vba With Change

    See if this helps:

    Option Explicit
    
    Sub ConvertRangeToTable()
    Dim rRange As Range
    Set rRange = Range("O2", Range("O" & Rows.Count).End(xlUp)).Resize(, 3)
    On Error Resume Next
    Worksheets("AFRsInput").ListObjects.Add( _
        xlSrcRange, _
        rRange, , xlYes).Name = "Table1"
    On Error GoTo 0
    End Sub
    
    Sub ConvertTableToRange()
    Dim rList As Range
    On Error Resume Next
    With Worksheets("AFRsInput").ListObjects("Table1")
        Set rList = .Range
        .Unlist                           ' convert the table back to a range
    End With
    
    With rList
        .Interior.ColorIndex = xlColorIndexNone
        .Font.ColorIndex = xlColorIndexAutomatic
        .Borders.LineStyle = xlLineStyleNone
    End With
    On Error GoTo 0
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    No sorry not what I meant. There are other references to the table when I convert to the range the buttons don't work properly updating the data. Also when putting a number that is in cell D4 which is the ID it should pull the data from the AFRsDB and AFRsParts sheets.

  4. #4
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    Thanks for the reply. There seem to be other references to the table. Can you please check the codes for AFRsInput sheet and module 1.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,518

    Re: Convert Table1 To Range and Edit Vba With Change

    You said you wanted to convert the Table to a Range. So I gave you code to go both ways. I didn't realise you wanted to delete the data range (leaving the headers).

    This will do that:

    Sub DeleteRange()
    Dim rRange As Range
    On Error Resume Next
    Set rRange = Range("O3", Range("O" & Rows.Count).End(xlUp)).Resize(, 3)
    rRange.ClearContents
    On Error GoTo 0
    End Sub

  6. #6
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    Hi can you please bump to front?

  7. #7
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    Here is the attachment workbook:

  8. #8
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Convert Table1 To Range and Edit Vba With Change

    I don't see a table on AFRsInout.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  9. #9
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    Thank you for the help sir. The reason I don't want the table is because I cannot add new parts when the sheet is protected so I thought have the range instead and keeping that range unlocked. I will try to implement your changes.

  10. #10
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    It was a table sorry. Columns O:Q was Table 1

  11. #11
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Convert Table1 To Range and Edit Vba With Change

    If you truly want to change the table to range the only modification I would make to TMS' code is instead of:
    With Worksheets("AFRsInput").ListObjects("Table1")
    use
    With Worksheets("AFRsInput").ListObjects(1)
    This code assumes only one table on the sheet but doesn't care what it is named.

    If you want to keep the table but delete the table data try this:
    Dim tbl As ListObject
    
    Public Sub ClearFilter(sh As Worksheet, TableName As String)
    
    Set tbl = sh.ListObjects(TableName)
    
    If FilterIsOn(tbl) = True Then
        Range(TableName).AutoFilter
        Range(TableName).AutoFilter
    Else
        Range(TableName).AutoFilter
    End If
    
    
    End Sub
    
    Function FilterIsOn(lo As ListObject) As Boolean
     
    Dim bOn As Boolean
     
    bOn = False
    On Error Resume Next
     
    If lo.AutoFilter.Filters.Count > 0 Then
        If Err.Number = 0 Then bOn = True
    End If
    
    On Error GoTo 0
    
    FilterIsOn = bOn
    
    End Function
    
    Sub ClearTable(sh As Worksheet, TableName As String)
    ClearFilter sh, TableName
    If tbl.ListRows.Count >= 1 Then
        tbl.DataBodyRange.Delete
    End If
    
    tbl.ListRows.Add
    
    With tbl.DataBodyRange.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    End Sub
    
    Function TableStart(sh As Worksheet, TableName As String) As Range
    Dim tbl As ListObject   ' Table to analyze
    Dim RowNum As Long      ' Return Row Number
    Dim AddRow As Long      ' Add 1 for blank 0 for cleared table
    Dim ColNum As Long      ' Retrun Column Numer
    Dim HeadRow As Long     ' Header Row
    Dim cl As Range         ' Pointer to row below header
    Dim VCount As Long      ' Count of non-blank cells
    Dim FCount As Long      ' Count of cells with formulas
    
    ' Initalize variables
    Set tbl = sh.ListObjects(TableName)
    HeadRow = tbl.HeaderRowRange.Row
    ColNum = tbl.Range.Column
    VCount = 0
    FCount = 0
    
    ' Count up blank cells and formulas
    For Each cl In sh.Range(sh.Cells(HeadRow + 1, ColNum), sh.Cells(HeadRow + 1, ColNum + tbl.ListColumns.Count - 1))
        If cl.Value <> "" Then
            VCount = VCount + 1
        End If
        
        If Left(cl.Formula, 1) = "=" Then
            FCount = FCount + 1
        End If
    
    Next cl
    
    ' Determine if table is cleared or blank
    If VCount + FCount = 0 Then
        AddRow = 1
    Else
        AddRow = 0
    End If
    
    
    If VCount - FCount = 0 Then
        ' Clear or blank
        RowNum = tbl.ListRows.Count + tbl.Range.Row + AddRow
    Else
        ' Has data
        RowNum = sh.Cells(Rows.Count, ColNum).End(xlUp).Row + 1
    End If
    
    Set TableStart = sh.Cells(RowNum, ColNum)
    End Function
    There are actually three macros in this module:

    Sub ClearTable (sh as worksheet, TableName as string) - clears the data from a table.

    Sub ClearFilter (sh as worksheet, TableName as string) - clears the filter from the table

    Function TableStart (sh as worksheet, TableName as string) - returns the cell for the next data entry. This is the topmost, leftmost cell in an empty table or the cell immediately below the first column of a table with data in it.

  12. #12
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    I converted to range but there is a problem. When I put a number in cell D4 which is the AFR# or ID it goes to debug mode. Can you try it?

  13. #13
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    dflak,

    Maybe all that has to be done is to change whatever referenced Table 1 in the codes?

  14. #14
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Convert Table1 To Range and Edit Vba With Change

    A piece of code that I discovered recently
    ws.portect.userinterfaceonly = True
    This lets VB code change the sheet without unprotecting it.

  15. #15
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Convert Table1 To Range and Edit Vba With Change

    Attach the worksheet with the malfunctioning code.

    Let's also backup on the requirement. You want to protect the sheet and lock down the cells except those that are at the very end of the table so you can do data entry on them. Is this correct?

    If so, do you want the user to be able to change former data? That is, I can make it so that the ONLY active cells on the sheet are those for the next data entry.

    Alternatively, we could put a button on the sheet to open the next row or maybe even launch a form that will write to the data.

    There are several different ways of meeting the requirement.

  16. #16
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    I would like the cells D4:D18, H4:H18, L4:L23 (has merged cells) open for entry.

    Also in the parts area these cells open: O2:Q23 (preferably not a table)

    There is an 'Update Data' button to accept new entries or changes to the record.

    This will update on the AFRsDB and the AFRsParts sheets.

    The yellow areas show that the parts are not matching what is on the AFRsParts sheet but should.
    Last edited by fredfarmer; 03-25-2019 at 02:12 PM.

  17. #17
    Forum Contributor
    Join Date
    03-12-2019
    Location
    USA
    MS-Off Ver
    2010
    Posts
    421

    Re: Convert Table1 To Range and Edit Vba With Change

    Hello again dflak,

    I got it working thanks for all the help.

+ 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. Populate and rezise table2 as table1 changes
    By Dresas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-05-2017, 01:03 PM
  2. [SOLVED] How to convert uploaded table1 to table2?
    By tta.akmal in forum Excel General
    Replies: 5
    Last Post: 04-16-2017, 08:04 AM
  3. IFERROR(INDEX(Table1, ROW([@[no.]])...
    By gustavo_uk in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-07-2016, 05:18 AM
  4. [SOLVED] VBA to change range that users are allowed to edit
    By chaddug in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-03-2014, 06:04 PM
  5. UserFrom Match, Find and Edit data : ComboBox1 Change range
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-08-2014, 09:06 AM
  6. [SOLVED] Help needed to edit macros due to change in data range
    By geniechua in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-04-2012, 10:57 PM
  7. Replies: 1
    Last Post: 08-29-2012, 04:01 AM

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