Ga naar hoofdinhoud

Hoe de tabel uitbreidbaar te houden door een tabelrij in een beschermd werkblad in Excel in te voegen?

De automatisch uitbreidende functie van de tabel gaat verloren nadat het werkblad in Excel is beveiligd. Er is bijvoorbeeld een tabel met de naam Tabel1 in uw beveiligde werkblad. Wanneer u iets typt onder de laatste rij, wordt de tabel niet automatisch uitgevouwen om de nieuwe rij op te nemen. Is er een methode om de tabel uitbreidbaar te houden door een nieuwe rij in een beschermd werkblad in te voegen? De methode in dit artikel kan u daarbij helpen.

Houd de tabel uitbreidbaar door een tabelrij in een beveiligd werkblad met VBA-code in te voegen


Houd de tabel uitbreidbaar door een tabelrij in een beveiligd werkblad met VBA-code in te voegen

Zoals onderstaand screenshot laat zien, een tabel met de naam Table1 in uw werkblad en de laatste kolom van de tabel is een formulekolom. Nu moet u het werkblad beschermen om te voorkomen dat de formulekolom verandert, maar u kunt de tabel uitbreiden door een nieuwe rij in te voegen en nieuwe gegevens toe te wijzen aan de nieuwe cellen. Ga als volgt te werk.

1. klikken Ontwikkelaar > Invoegen > Knop (formulierbeheer) om een Formulierbeheer knop in uw werkblad.

2. In het opduiken Wijs macro toe dialoogvenster, klik op de New knop.

3. In de Microsoft Visual Basic voor toepassingen -venster, kopieer en plak de onderstaande VBA-code tussen de Sub en End Sub alinea's in de Code venster.

VBA-code: houd de tabel uitbreidbaar door een tabelrij in een beveiligd werkblad in te voegen

 'Update by ExtendOffice 20220826
    Dim xRg, tableRg As Range
    Dim xRowCount As Integer
    Dim pswStr As String
    pswStr = "123"
    On Error Resume Next
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:=pswStr

    Set tableRg = ActiveSheet.ListObjects("Table4").Range
    xRowCount = tableRg.Rows.Count
    
    Set xRg = Range("Table4[[#Headers],[Total]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault

    ActiveSheet.Protect Password:=pswStr, DrawingObjects:=False, _
                    Contents:=True, Scenarios:=False, _
                    AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                    AllowFormattingRows:=True, AllowInsertingColumns:=True, _
                    AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
                    AllowDeletingColumns:=True, AllowDeletingRows:=True, _
                    AllowSorting:=True, AllowFiltering:=True, _
                    AllowUsingPivotTables:=True
    Application.ScreenUpdating = True

Opmerkingen:

1). In de code is nummer "123" het wachtwoord dat u gebruikt om het werkblad te beschermen.
2). Wijzig de tabelnaam en de naam van de kolom met de formule die u wilt beschermen.

4. druk de anders + Q -toetsen om het venster Microsoft Visual Basic for Applications te sluiten.

5. Selecteer de cellen in de tabel waaraan u nieuwe gegevens moet toewijzen, behalve de formulekolom, en druk vervolgens op Ctrl + 1 toetsen om de te openen Cellen opmaken dialoog venster. In de Cellen opmaken dialoogvenster, schakel het vinkje uit Vergrendeld vak en klik vervolgens op het OK knop. Zie screenshot:

6. Bescherm nu uw werkblad met het wachtwoord dat u in de VBA-code hebt opgegeven.

Vanaf nu, nadat u op de knop Formulierbeheer in uw beschermde werkblad hebt geklikt, kan de tabel worden uitgevouwen door een nieuwe rij in te voegen zoals onderstaand screenshot.

Note: u kunt de tabel wijzigen, behalve de formulekolom in het beveiligde werkblad.


Gerelateerde artikelen:

Beste Office-productiviteitstools

馃 Kutools AI-assistent: Een revolutie teweegbrengen in de data-analyse op basis van: Intelligente uitvoering   |  Genereer code  |  Aangepaste formules maken  |  Analyseer gegevens en genereer grafieken  |  Roep Kutools-functies aan...
Populaire functies: Zoek, markeer of identificeer duplicaten   |  Verwijder lege rijen   |  Combineer kolommen of cellen zonder gegevens te verliezen   |   Ronde zonder formule ...
Super opzoeken: Meerdere criteria VLookup    VLookup met meerdere waarden  |   VOpzoeken over meerdere bladen   |   Fuzzy opzoeken ....
Geavanceerde vervolgkeuzelijst: Maak snel een vervolgkeuzelijst   |  Afhankelijke vervolgkeuzelijst   |  Multi-select vervolgkeuzelijst ....
Kolom Beheerder: Voeg een specifiek aantal kolommen toe  |  Kolommen verplaatsen  |  Schakel de zichtbaarheidsstatus van verborgen kolommen in  |  Vergelijk bereiken en kolommen ...
Uitgelichte functies: Raster focus   |  Ontwerpweergave   |   Grote formulebalk    Werkmap- en bladbeheer   |  resource Library (Auto-tekst)   |  Datumkiezer   |  Combineer werkbladen   |  Cellen coderen/decoderen    Stuur e-mails per lijst   |  Super filter   |   Speciaal filter (filter vet/cursief/doorhalen...) ...
Top 15 gereedschapsets12 Tekst Tools (toe te voegen tekst, Tekens verwijderen, ...)   |   50+ tabel Types (Gantt Chart, ...)   |   40+ Praktisch Formules (Bereken leeftijd op basis van verjaardag, ...)   |   19 Invoeging Tools (QR-code invoegen, Afbeelding invoegen vanaf pad, ...)   |   12 Camper ombouw Tools (Getallen naar woorden, Currency Conversion, ...)   |   7 Samenvoegen en splitsen Tools (Geavanceerd Combineer rijen, Gespleten cellen, ...)   |   ... en meer

Geef uw Excel-vaardigheden een boost met Kutools voor Excel en ervaar effici毛ntie als nooit tevoren. Kutools voor Excel biedt meer dan 300 geavanceerde functies om de productiviteit te verhogen en tijd te besparen.  Klik hier om de functie te krijgen die u het meest nodig heeft...

Omschrijving


Office-tabblad Brengt een interface met tabbladen naar Office en maakt uw werk veel gemakkelijker

  • Schakel bewerken en lezen met tabbladen in Word, Excel, PowerPoint in, Publisher, Access, Visio en Project.
  • Open en maak meerdere documenten in nieuwe tabbladen van hetzelfde venster in plaats van in nieuwe vensters.
  • Verhoogt uw productiviteit met 50% en vermindert honderden muisklikken voor u elke dag!
Comments (19)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
hello, I've copied and pasted the code into VBA, amended the table name and selected the columns I want to protect. I click the button however all it does is protects the sheet but not add any new table rows. Any advice?
This comment was minimized by the moderator on the site
Sub ButtonOut_Click()

Dim PswS As String
PswStr = "54321"

On Error Resume Next

Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=PswStr

ActiveSheet.ListObjects("Table1").ListRows.Add

ActiveSheet.Protect Password:=PswStr
Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
The code is not working.
Several errors.

Dim xRg, tableRg As Range

xRg
is a variant not a range

yRg
not declared at all

Set xRg = Range("Table4[[#Headers],[Total]]").Offset(1, 0)

runtime error 1004
When I take away the TOTAL, it works.
It is not working with the total row displayed and neither when I hide the total row in the ribbon.

Normally your website is really great, but this article need improvment.
This comment was minimized by the moderator on the site
Hi prem,
You need to make sure that the table name and column header specified in the code match the table name and column header in the worksheet. To avoid the 1004 error, you may need to enable the trust access to the VBA project object model in your Excel: click File > Options > Trust Center > Trust Center Settings > Macro Settings > and then check the Trust access to the VBA project object model box.
This comment was minimized by the moderator on the site
Hi.

Thanks for sharing. Though I have a question... by using the code above, I can add one row at a time. How to add multiple rows in one click?

Thanks in advance.

'Update by ExtendOffice 20220826
Dim xRg, tableRg As Range
Dim xRowCount As Integer
Dim pswStr As String
pswStr = "123"
On Error Resume Next
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=pswStr

Set tableRg = ActiveSheet.ListObjects("Table4").Range
xRowCount = tableRg.Rows.Count

Set xRg = Range("Table4[[#Headers],[Total]]").Offset(1, 0)
Set yRg = xRg.Resize(xRowCount, 1)
xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault

ActiveSheet.Protect Password:=pswStr, DrawingObjects:=False, _
Contents:=True, Scenarios:=False, _
AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, _
AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
AllowDeletingColumns:=True, AllowDeletingRows:=True, _
AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Application.ScreenUpdating = True
This comment was minimized by the moderator on the site
Hola!!!
Tengo una tabla donde m谩s de una columna est谩 protegida.
La tabla tiene 17 columnas de las cuales 7 deben quedar bloqueadas porque poseen f贸rmulas.
Mi tabla arranca en celda A4

Estaba tratando de usar este c贸digo para probarlo, cambiando lo que ver谩n abajo como "CLAVE", "MITABLA" y "AVISO 1" por mis nombres particulares:
Donde "AVISO 1" corresponde a una de las columnas que est谩 protegida.

Dim pswStr As String
'Update by ExtendOffice 20181106
pswStr = "CLAVE"
On Error Resume Next
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=pswStr
ActiveSheet.Range("A4").Select
Range("MITABLA[[#Headers],[AVISO 1]]").Select
Selection.End(xlDown).Select
Selection.Offset(1, -16).Select
ActiveCell.FormulaR1C1 = "new"
ActiveSheet.Protect Password:=pswStr, DrawingObjects:=False, _
Contents:=True, Scenarios:=False, _
AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, _
AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
AllowDeletingColumns:=True, AllowDeletingRows:=True, _
AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
Selection.ClearContents
Application.ScreenUpdating = True

Lo que est谩 haciendo el c贸digo tal cual como lo escribo es que en lugar de agregar una nueva l铆nea a mi tabla, est谩 colocando la palabra "new" en la 煤ltima celda con contenido de la columna "AVISO 1".

Surgen entonces 2 dudas:
1. 驴c贸mo podr铆a hacer para determinar m谩s de una columna protegida?
2. 驴por qu茅 est谩 haciendo esto el c贸digo definido?

Agradezco de antemano que me puedan ayudar! Estar茅 atenta.
This comment was minimized by the moderator on the site
Hi Daina,
1. If the 7 formula columns that you want to protect are consecutive in the table.
For example, the headers of the columns are gg, hh, ii, jj, kk, ll, mm as shown in the screenshot below. You can apply the following VBA code to get it done.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/table.png
In this line Set xRg = Range("Table3[[#Headers],[gg]:[mm]]").Offset(1, 0) in the following code, you just need to enter the headers of the first column and the last column.
Sub Button1_Click()
 'Update by ExtendOffice 20220826
    Dim xRg, tableRg As Range
    Dim xRowCount As Integer
    Dim pswStr As String
    pswStr = "123"
    On Error Resume Next
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:=pswStr

    'Change the table name and the column headers
    Set tableRg = ActiveSheet.ListObjects("Table3").Range
    xRowCount = tableRg.Rows.Count
    
     Set xRg = Range("Table3[[#Headers],[gg]:[mm]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, xRg.Columns.Count)

    xRg.Resize(xRowCount - 1, xRg.Columns.Count).AutoFill Destination:=yRg, Type:=xlFillDefault
    

    ActiveSheet.Protect Password:=pswStr, DrawingObjects:=False, _
                    Contents:=True, Scenarios:=False, _
                    AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                    AllowFormattingRows:=True, AllowInsertingColumns:=True, _
                    AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
                    AllowDeletingColumns:=True, AllowDeletingRows:=True, _
                    AllowSorting:=True, AllowFiltering:=True, _
                    AllowUsingPivotTables:=True
    Application.ScreenUpdating = True
End Sub

2. If the 7 formula columns that you want to protect are discontinuous in the table. Apply the following code. In the code, you need to manually input the headers of the columns one by one.
Sub Button1_Click()
 'Update by ExtendOffice 20220826
    Dim xRg, tableRg As Range
    Dim xRowCount As Integer
    Dim pswStr As String
    pswStr = "123"
    On Error Resume Next
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:=pswStr

    'Change the table name and the column headers
    Set tableRg = ActiveSheet.ListObjects("Table3").Range
    xRowCount = tableRg.Rows.Count
    
    Set xRg = Range("Table3[[#Headers],[gg]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault
    Set xRg = Range("Table3[[#Headers],[hh]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault
    Set xRg = Range("Table3[[#Headers],[ii]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault
    Set xRg = Range("Table3[[#Headers],[jj]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault
    Set xRg = Range("Table3[[#Headers],[kk]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault
     Set xRg = Range("Table3[[#Headers],[ll]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault
    Set xRg = Range("Table3[[#Headers],[mm]]").Offset(1, 0)
    Set yRg = xRg.Resize(xRowCount, 1)
    xRg.Resize(xRowCount - 1, 1).AutoFill Destination:=yRg, Type:=xlFillDefault

    ActiveSheet.Protect Password:=pswStr, DrawingObjects:=False, _
                    Contents:=True, Scenarios:=False, _
                    AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                    AllowFormattingRows:=True, AllowInsertingColumns:=True, _
                    AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
                    AllowDeletingColumns:=True, AllowDeletingRows:=True, _
                    AllowSorting:=True, AllowFiltering:=True, _
                    AllowUsingPivotTables:=True
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
How do I create a button to erase lines?
This comment was minimized by the moderator on the site
Merhaba Tablo ismini ve sat谋r ba艧lang谋c yerlerini de臒i艧tirdi臒im zaman kod 莽al谋艧m谋yor yard谋mc谋 olurmusunuz
This comment was minimized by the moderator on the site
Hi,
Make sure you have changed to the exact same table name and column header in the code.
I have changed the table name and the column header to test the code, and it works well.
Did you get any error prompt? I need to know more specific about your issue, such as your Excel version. The more detailed you describe the error, the faster we can understand and solve it.
This comment was minimized by the moderator on the site
Hello,

the code worked initially, but after I duplicated the worksheet, it stayed for after 24 hours then all the code disappeared. And now I can鈥檛 access the worksheet.

it keeps telling me incorrect password. And the code have disappeared. .
This comment was minimized by the moderator on the site
Hello, I used the above code and got the following error message:
"Code execution has been interrupted". When I click on Debug, Line 20 "Selection.ClearContents" is highlighted.

When I initially entered the code, it worked correctly.

I changed "Table" to the name of the table and change the column to the name of the column I am using. I also changed the "Selection.Offset (x,-x).Select" to match my needs.


Any suggestions as to why this is occurring?
This comment was minimized by the moderator on the site
Try this Vba code for add new line in you table

Sub Tab_Line_Add()
Dim pswStr As String
pswStr = "123"
On Error Resume Next
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=pswStr
ActiveSheet.Range("D8").Select
'D8 is tabel header
Range("Table1[[#Headers],[Total]]").Select
Selection.End(xlDown).Select
Selection.ListObject.ListRows.Add AlwaysInsert:=False
ActiveSheet.Protect Password:=pswStr

End Sub
.
This comment was minimized by the moderator on the site
using the suggested (Selection.ListObject.ListRows.Add AlwaysInsert:=False) fixed a similar problem for me with the original code, where a new full row (extending down cell contained formulas) would not be added to the table on a much wider table 51 columns. So thanks for sharing and fixing Mac.
This comment was minimized by the moderator on the site
Hi Mac,
Thanks for sharing.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations