Ugrás a tartalomra

Hogyan lehet a táblázatot kibővíteni úgy, hogy táblázat sorokat illeszt be egy védett munkalapba az Excelben?

A táblázat automatikus bővítési funkciója elvész, miután megvédte a munkalapot az Excel-ben. Például van egy táblázat1 nevű tábla a védett munkalapon, amikor bármit beír az utolsó sor alá, a táblázat nem bővül automatikusan az új sorra. Van-e módszer a táblázat kibővíthetőségének fenntartására új sor beszúrásával egy védett munkalapba? A cikkben szereplő módszer segíthet elérni.

Tartsa a táblázatot kibővíthetővé úgy, hogy a VBA kóddal ellátott védett munkalapba táblázatsort helyez be


Tartsa a táblázatot kibővíthetővé úgy, hogy a VBA kóddal ellátott védett munkalapba táblázatsort helyez be

Amint az alábbi képernyőképen látható, a munkalapon egy Table1 nevű tábla, a táblázat utolsó oszlopa pedig egy formula oszlop. Most meg kell védenie a munkalapot, hogy megakadályozza a képletoszlop megváltozását, de lehetővé kell tennie a táblázat kibővítését új sor beszúrásával és új adatok hozzárendelésével az új cellákba. Kérjük, tegye a következőket.

1. kettyenés Fejlesztő > betétlap > Gomb (űrlapvezérlés) beilleszteni a Űrlapvezérlés gombra a munkalapon.

2. A felbukkanóban Hozzárendelje a Makrót párbeszédpanelen kattintson a Új gombot.

3. Ban,-ben Microsoft Visual Basic for Applications ablakba, kérjük, másolja és illessze be az alábbi VBA kódot a alatt és a End Sub bekezdések a Kód ablak.

VBA-kód: Tartsa a táblázatot kibővíthetővé, ha egy táblázatsort beilleszt egy védett munkalapba

 '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

Megjegyzések:

1). A kódban a „123” szám a jelszó, amelyet a munkalap védelmére használ.
2). Kérjük, változtassa meg a táblázat nevét és annak az oszlopnak a nevét, amely a megvédendő képletet tartalmazza.

4. megnyomni a más + Q billentyűkkel zárja be a Microsoft Visual Basic for Applications ablakot.

5. Jelölje ki a táblázat azon celláit, amelyekbe új adatokat kell rendelnie, kivéve a képlet oszlopot, majd nyomja meg a gombot Ctrl + 1 billentyűk megnyitásához Cellák formázása párbeszédablak. Ban,-ben Cellák formázása párbeszédpanelen törölje a jelet a Zárt jelölőnégyzetet, majd kattintson a gombra OK gomb. Lásd a képernyőképet:

6. Most védje meg munkalapját a VBA kódban megadott jelszóval.

Mostantól a védett munkalapon az Űrlapvezérlés gombra kattintás után a táblázat kibővíthető lesz új sor beszúrásával az alábbi képernyőkép szerint.

Megjegyzések: módosíthatja a táblázatot, kivéve a védett munkalap képlet oszlopát.


Kapcsolódó cikkek:

A legjobb irodai hatékonyságnövelő eszközök

🤖 Kutools AI Aide: Forradalmasítsa az adatelemzést a következők alapján: Intelligens végrehajtás   |  Kód létrehozása  |  Hozzon létre egyéni képleteket  |  Adatok elemzése és diagramok létrehozása  |  A Kutools funkciók meghívása...
Népszerű szolgáltatások: Ismétlődések keresése, kiemelése vagy azonosítása   |  Üres sorok törlése   |  Oszlopok vagy cellák kombinálása adatvesztés nélkül   |   Kerek Formula nélkül ...
Szuper keresés: Több kritérium VLookup    Többértékű VLookup  |   VLookup több munkalapon   |   Fuzzy Lookup ....
Speciális legördülő lista: Gyors legördülő lista létrehozása   |  Függő legördülő lista   |  Többszörösen válassza ki a legördülő listát ....
Oszlopkezelő: Adjon meg egy adott számú oszlopot  |  Oszlopok mozgatása  |  Kapcsolja be a Rejtett oszlopok láthatósági állapotát  |  Tartományok és oszlopok összehasonlítása ...
Kiemelt funkciók: Rács fókusz   |  Design nézet   |   Nagy Formula bár    Munkafüzet és lapkezelő   |  Erőforrás-könyvtár (Auto szöveg)   |  Dátumválasztó   |  Kombinálja a munkalapokat   |  Cellák titkosítása/dekódolása    E-mailek küldése listánként   |  Szuper szűrő   |   Speciális szűrő (félkövér/dőlt/áthúzott szűrés...) ...
A 15 legjobb eszközkészlet12 szöveg Eszközök (Szöveg hozzáadása, Karakterek eltávolítása,...)   |   50 + Táblázatos Típusai (Gantt-diagram,...)   |   40+ Praktikus képletek (Számolja ki az életkort a születésnap alapján,...)   |   19 beszúrás Eszközök (Helyezze be a QR-kódot, Kép beszúrása az útvonalból,...)   |   12 Átalakítás Eszközök (Számok szavakig, Valuta átváltás,...)   |   7 Egyesítés és felosztás Eszközök (Haladó kombinált sorok, Hasított sejtek,...)   |   ... és több

Töltsd fel Excel-készségeidet a Kutools for Excel segítségével, és tapasztald meg a még soha nem látott hatékonyságot. A Kutools for Excel több mint 300 speciális funkciót kínál a termelékenység fokozásához és az időmegtakarításhoz.  Kattintson ide, hogy megszerezze a leginkább szükséges funkciót...

Leírás


Az Office lap füles felületet hoz az Office-ba, és sokkal könnyebbé teszi a munkáját

  • Füles szerkesztés és olvasás engedélyezése Wordben, Excelben és PowerPointban, Publisher, Access, Visio és Project.
  • Több dokumentum megnyitása és létrehozása ugyanazon ablak új lapjain, mint új ablakokban.
  • 50% -kal növeli a termelékenységet, és naponta több száz kattintással csökkenti az egér kattintását!
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’t 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