Skip i'r prif gynnwys

Sut i drosi colofnau i resi a mewnosod rhesi newydd i lenwi data yn Excel?

Yn yr erthygl hon, rwy'n cyflwyno cod VBA i drawsosod colofnau i resi a mewnosod rhesi newydd i lenwi data fel islaw'r screenshot a ddangosir, os oes gennych ddiddordeb yn y llawdriniaeth hon, ewch i isod i gael mwy o fanylion.
trawsosod doc mewnosod rhesi 1

Trawsosod a mewnosod rhesi gyda VBA


swigen dde glas saeth Trawsosod a mewnosod rhesi gyda VBA

Nid oes unrhyw ffordd uniongyrchol i drawsosod ystod a mewnosod rhesi i lenwi'r un data, ond yma gallaf gyflwyno cod VBA i ddatrys y dasg hon yn gyflym.

1. Gwasgwch Alt + F11 allweddi i agor Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Cliciwch Mewnosod > Modiwlau, a'i gludo o dan god VBA i ffenestr y Modiwl. Gweler y screenshot:

VBA: Trawsosod a mewnosod rhesi.

 Sub TransposeInsertRows()
'UpdatebyExtendoffice20161125
    Dim xRg As Range
    Dim i As Long, j As Long, k As Long
    Dim x As Long, y As Long
    Set xRg = Application.InputBox _
    (Prompt:="Range Selection...", _
    Title:="Kutools For Excel", Type:=8)
    Application.ScreenUpdating = False
    x = xRg(1, 1).Column + 2
    y = xRg(1, xRg.Columns.Count).Column
    For i = xRg(xRg.Rows.Count, 1).Row To xRg(1, 1).Row Step -1
        If Cells(i, x) <> "" And Cells(i, x + 1) <> "" Then
            k = Cells(i, x - 2).End(xlToRight).Column
            If k > y Then k = y
            For j = k To x + 1 Step -1
                Cells(i + 1, 1).EntireRow.Insert
                With Cells(i + 1, x - 2)
                    .Value = .Offset(-1, 0)
                    .Offset(0, 1) = .Offset(-1, 1)
                    .Offset(0, 2) = Cells(i, j)
                End With
                Cells(i, j).ClearContents
            Next j
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

 

trawsosod doc mewnosod rhesi 2

3. Gwasgwch F5 allwedd i redeg y cod, ac yna dewiswch yr ystod ddata rydych chi am ei thrawsnewid a mewnosod rhesi yn y dialog popio. Gweler y screenshot:
trawsosod doc mewnosod rhesi 3

4. Cliciwch OK. Ac mae'r ystod a ddewiswyd wedi'i throsi fel isod sgrinlun a ddangosir.
trawsosod doc mewnosod rhesi 4

Nodyn: cyn rhedeg y cod, byddai'n well ichi arbed copi o'r data gwreiddiol.

Awgrym.Os ydych chi am drawsosod croesfwrdd yn gyflym i restru tabl neu i'r gwrthwyneb, ceisiwch ddefnyddio'r Kutools ar gyfer Excel's Trawsosod Dimensiynau Tabl fel y dangosir yn y screenshot canlynol. Mae'n swyddogaeth lawn heb gyfyngiad mewn 30 diwrnod, lawrlwythwch a chael treial am ddim nawr.

doc trawsosod croes i'r rhestr

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Compiler errorSyntax Error
Dim xRg As Range
Dim i As Long, j As Long, k As Long
Dim x As Long, y As Long

This comment was minimized by the moderator on the site
The above code works great. It keeps the first two columns. How would I change the code so that it only keeps the first column?
This comment was minimized by the moderator on the site
How can I transpose the data every two cells

The initial exercise is:


Class1 | Name | Nancy | Judy | Lily | Daniel
Class2 | Name | Bruce | Uee | Perter | Daniel
Class3 | Name | kate | Kelly | Wenddy | Daniel

and I need it to look like this:

Class1 | Name | Nancy | Judy
Class1 | Name | Lily | Daniel
Class 2 | Name | Bruce | Uee
Class 2 | Name | Perter | Daniel
Class3 | Name | kate | Kelly
Class3 | Name | Wenddy | Daniel
This comment was minimized by the moderator on the site
how can I make data 3 and 4 remain in the same column:


the initial exercise is:

Class1 | Name | Nancy | Judy | Lily | Daniel
Class2 | Name | Bruce | Uee | Perter | Daniel
Class3 | Name | kate | Kelly | Wenddy | Daniel

I need to transpose it like this

Class1 | Name | Nancy | Judy
Class1 | Name | Lily | Daniel
Class 2 | Name | Bruce | Uee
Class 2 | Name | Perter | Daniel
Class3 | Name | kate | Kelly
Class3 | Name | Wenddy | Daniel
This comment was minimized by the moderator on the site
how would i change the VBA code so that it will keep more of the columns the same? for example instead of the first two columns staying the same it will be the first 4?
This comment was minimized by the moderator on the site
I am not sure, may be you can try to change x = xRg(1, 1).Column + 2 to x = xRg(1, 1).Column + 4.
This comment was minimized by the moderator on the site
If my cross table's informations come from formulas, those formulas don't always follow when the macro is applied. How could I fix this to make sure that the formulas are kept in the cells?
This comment was minimized by the moderator on the site
Hi, thank u for ur message. For solving your problem, you can apply Kutools' Convert Formula to Text before you running the macro, then after transposing thetable, apply Kutools' Convert Text to Formula. Please Let me know if it works. Thank u.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations