Skip to main content

Conas an tábla a choinneáil fairsingithe trí shraith tábla a chur isteach i mbileog oibre faoi chosaint in Excel?

Caillfear feidhm leathnaithe uathoibríoch an tábla tar éis an bhileog oibre in Excel a chosaint. Mar shampla, tá tábla darb ainm Tábla 1 i do bhileog oibre faoi chosaint, nuair a chlóscríobhann tú aon rud faoin tsraith dheireanach, ní leathnóidh an tábla go huathoibríoch chun an tsraith nua a áireamh. An bhfuil modh ann chun an tábla a choinneáil fairsingithe trí shraith nua a chur isteach i mbileog oibre faoi chosaint? Is féidir leis an modh san alt seo cabhrú leat é a bhaint amach.

Coinnigh an tábla inathraithe trí shraith tábla a chur isteach i mbileog oibre faoi chosaint le cód VBA


Coinnigh an tábla inathraithe trí shraith tábla a chur isteach i mbileog oibre faoi chosaint le cód VBA

Mar a thaispeántar thíos an pictiúr, tábla darb ainm Tábla 1 i do bhileog oibre, agus colún foirmle is ea an colún deireanach den tábla. Anois ní mór duit an bhileog oibre a chosaint chun an colún foirmle a chosc ó athrú, ach lig don tábla a leathnú trí shraith nua a chur isteach agus sonraí nua a shannadh sna cealla nua. Déan mar a leanas le do thoil.

1. cliceáil Forbróir > Ionsáigh > Cnaipe (Rialú Foirm) a chur isteach Rialú Foirm cnaipe isteach i do bhileog oibre.

2. Sa popping suas Macra a shannadh bosca dialóige, cliceáil an Nua cnaipe.

3. Sa Microsoft Visual Basic d’Fheidhmchláir fuinneog, cóipeáil agus greamaigh an cód VBA thíos idir an Fo agus Fo Deireadh míreanna sa cód fhuinneog.

Cód VBA: Coinnigh an tábla fairsingithe trí shraith tábla a chur isteach i mbileog oibre faoi chosaint

 '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

nótaí:

1). Sa chód, is é uimhir “123” an focal faire a úsáidfidh tú chun an bhileog oibre a chosaint.
2). Athraigh ainm an tábla agus ainm an cholúin ina bhfuil an fhoirmle a chosnóidh tú.

4. Brúigh an Eile + Q eochracha chun an fhuinneog Microsoft Visual Basic for Applications a dhúnadh.

5. Roghnaigh na cealla sa tábla a gcaithfidh tú sonraí nua a shannadh dóibh seachas an colún foirmle, ansin brúigh an Ctrl + 1 eochracha a oscailt Cealla Formáid bosca dialóige. Sa Cealla Formáid bosca dialóige, díthiceáil an Faoi Ghlas bosca, agus ansin cliceáil ar an OK cnaipe. Féach an pictiúr:

6. Anois cosain do bhileog oibre le pasfhocal atá sonraithe agat sa chód VBA.

As seo amach, tar éis duit an cnaipe Rialaithe Foirme a chliceáil i do bhileog oibre faoi chosaint, beidh an tábla inathraithe trí shraith nua a chur isteach mar atá thíos an pictiúr a thaispeántar.

nótaí: is féidir leat an tábla a mhodhnú ach amháin an colún foirmle sa bhileog oibre faoi chosaint.


Earraí gaolmhara:

Uirlisí Táirgiúlachta Oifige is Fearr

🤖 Kutools AI Aide: anailís sonraí a réabhlóidiú bunaithe ar: Forghníomhú Chliste   |  Gin Cód  |  Cruthaigh Foirmlí Saincheaptha  |  Anailís a dhéanamh ar Sonraí agus Cairteacha a Ghin  |  Feidhmeanna Kutools a agairt...
Gnéithe Coitianta: Faigh, Aibhsigh nó Aithnigh Dúblaigh   |  Scrios Sraitheanna Bána   |  Comhcheangail Colúin nó Cealla gan Sonraí a Chailleadh   |   Babhta gan Foirmle ...
Cuardaigh Super: Ilchritéir VLookup    VLookup Illuachanna  |   VLookup Trasna Ilbhileoga   |   Amharc doiléir ....
Liosta anuas Casta: Go tapa Cruthaigh Liosta Anuas   |  Liosta anuas Cleithiúnach   |  Liosta Buail Isteach Ilroghnacha ....
Bainisteoir Colún: Cuir Líon Sonrach Colún leis  |  Colúin Bog  |  Scoránaigh Stádas Infheictheachta na gColún Ceilte  |  Déan comparáid idir Raonta & Colúin ...
Gnéithe Réadmhaoin: Fócas Eangaí   |  Amharc Dearaidh   |   Barra Mór na Foirmle    Leabhar Oibre & Bainisteoir Bileog   |  Leabharlann Acmhainní (Uaththéacs)   |  Piocálaí Dáta   |  Comhcheangail Bileoga Oibre   |  Criptigh/Díchriptigh Cealla    Seol Ríomhphost trí Liosta   |  Scagaire Super   |   Scagaire Speisialta (scagaire trom/iodálach/stailc tríd...) ...
Barr 15 Uirlisí12 Téacs uirlisí (Cuir Téacs, Bain Carachtair,...)   |   50 + Cairt cineálacha (Cairt Gantt,...)   |   40+ Praiticiúil Foirmlí (Ríomh aois bunaithe ar lá breithe,...)   |   19 Insertion uirlisí (Cuir isteach Cód QR, Ionsáigh Pictiúr ón gCosán,...)   |   12 Tiontú uirlisí (Uimhreacha le Focail, Comhshó Airgeadra,...)   |   7 Cumaisc & Scoilt uirlisí (Sraitheanna Comhcheangail Casta, Cealla Scoilt,...)   |   ... agus eile

Supercharge Do Scileanna Excel le Kutools le haghaidh Excel, agus Éifeachtúlacht Taithí Cosúil Ná Roimhe. Kutools le haghaidh Excel Tairiscintí Níos mó ná 300 Ardghnéithe chun Táirgiúlacht a Treisiú agus Sábháil Am.  Cliceáil anseo chun an ghné is mó a theastaíonn uait a fháil ...

Tuairisc


Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
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