How to unroll a row in Excel, to have a better view for editing?
Solution 1:
Other answers have used Excel's built-in Data Form. This provides a powerful mechanism for adding/editing/deleting rows of data in a data list without any need for VBA. A data form has the following features
- It defaults to the first data row when activated from the ribbon/quick access toolbar, though allows intuitive navigation to subsequent rows
- All fields (columns) in the data list are editable and a separate one line textbox is provided for changing each field
In some circumstances, these features may be less than ideal - for example when data items comprise very lengthy text strings.
As an alternative to a Data Form, the solution below uses a UserForm. A userform provides greater flexibility than a data form but has the downside that VBA coding and some design skills are required. The userform shown is based on the following assumptions about the OP's requirements
- The major requirement of the userform is to view and change the content in columns Note1, Note2, Note2 and Note4 of the data list
- The Name and UserId columns are simply used to identify the data row. The values within these two columns are correct and will not need changing
- There is no requirement for the userform to provide the capability for adding or deleting rows of the data list
- There should be the flexibility to move between rows of the data list
- The userform should be capable of being switched on and off
The userform is shown in the screenshot below.
Behaviour of the userform
- When the worksheet is in "form mode" (see 4., below), selecting a single cell within any of the columns headed Note1, Note2, Note3 or Note4 displays the content of the corresponding Note item, along with the Name and UserId values from the start of the row. The Note involved (Note1, Note2, Note3 or Note4) is indicated by the label to the left of the large textbox in the userform. The userform is modal, so the user must interact with the form to progress.
- The text in the large Note textbox may be changed by the user. The button labelled "Save Changes" writes the current content of the textbox to the corresponding Note cell of the worksheet and "closes" the userform (removes it from the display). The button labelled "Cancel" also "closes" the userform, though without updating the corresponding Note cell.
- No navigation functionality is provided in the userform. Instead, the user "closes" the form (using either of the two buttons) and can navigate using the standard Excel interface to other cells. The form reappears whenever the current selected cell(s) on the worksheet meet the condition specified in 1., above.
- The worksheet remains in "form mode" until the userform is closed with the "Close" button (the standard "X" at the top-right of the form). The worksheet can be put back into "form mode" by running the macro
FormMode
.
Structure and Organisation
- The data, including the header row and the Name and UserId columns are contained in a named range called
MyData
- The userform is called
EditForm
. The form contains 5 labels, 1 textbox, 2 buttons and the standard "Close button" on the title bar.EditForm
has a boolean propertyEditOn
which determines whether "form mode" is on or off. - There is a VBA module which declares a couple of
Public
variablesintDataRow
andintDataColumn
which are the row and column containing the cell withinMyData
that provides the contents of the Note textbox displayed inEditForm
. The module also contains the macroFormMode
which simply switches "form mode" on by the assignmentEditForm.EditOn = True
. - The worksheet containing
MyData
contains aWorksheet_SelectionChange
procedure. This procedure tests- whether "form mode" is on
- whether a single cell has been selected on the worksheet
- whether this cell is contained within a Note column in
MyData
(after the header row) If any test fails, the procedure does nothing. Otherwise it calculates theintDataRow
andintDataColumn
values of the selected cell and causesEditForm
to be displayed through itsShow
method.
- The module for EditForm contains the following procedures:
-
Property Let
andProperty Get
for theEditOn
property of the userform -
UserForm_Activate
event procedure. The event is triggered byEditForm.Show
(in 4., above) and populates the userform from the worksheet based on the current values ofintDataRow
andintDataColumn
. - button
_Click
procedures for the "Save Changes" and "Cancel" buttons. The former updatesRange("MyData").Cell(intDataRow,intDataColumn)
with the contents of the userform's textbox. Both procedures remove the userform from the display ("close" it) through the form'sHide
method.
-
A screenshot of the VBA project is shown below.
The form is basic but functional. It can obviously be adapted and improved to meet requirements. It provides a different model to that provided by Excel's Data Form.
The VBA code utilised is listed below.
CODE FOR WORKSHEET CONTAINING RANGE MyData
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not EditForm.EditOn Then
Exit Sub
End If
If Target.Rows.Count > 1 Or _
Target.Columns.Count > 1 Then
Exit Sub
End If
If Target.Row < Range("MyData").Row + 1 Or _
Target.Row > Range("MyData").Row + Range("MyData").Rows.Count - 1 Or _
Target.Column < Range("MyData").Column + 2 Or _
Target.Column > Range("MyData").Column + Range("MyData").Columns.Count - 1 Then
Exit Sub
End If
intDataRow = 1 + Target.Row - Range("MyData").Row
intDataColumn = 1 + Target.Column - Range("MyData").Column
EditForm.Show
End Sub
CODE FOR FORM EditForm
Option Explicit
Private pEditMode As Boolean
Public Property Let EditOn(bValue As Boolean)
pEditMode = bValue
End Property
Public Property Get EditOn() As Boolean
EditOn = pEditMode
End Property
Private Sub UserForm_Activate()
Name_Field.Caption = Range("MyData").Cells(intDataRow, 1)
UserID_Field.Caption = Range("MyData").Cells(intDataRow, 2)
NoteLabel.Caption = "Note" & (intDataColumn - 2) & ":"
Note_Field.Value = Range("MyData").Cells(intDataRow, intDataColumn)
End Sub
Private Sub CancelButton_Click()
Me.Hide
End Sub
Private Sub SaveButton_Click()
Range("MyData").Cells(intDataRow, intDataColumn) = Note_Field.Value
Me.Hide
End Sub
CODE FOR MODULE
Option Explicit
Public intDataRow As Integer
Public intDataColumn As Integer
Sub FormMode()
EditForm.EditOn = True
End Sub
Solution 2:
Here is how to add a VBA macro that will show a Data Form for the current row.
-
In Excel Show the Developer tab
-
Save the workbook as a macro-enabled
.xlsm
file (not.xlsx
) -
Go to the Developer tab
-
Click Visual Basic
-
Select the Menu entry of Insert > Module
-
In the text-editor that opens, copy the following text:
Option Explicit Sub CurrRowForm() SendKeys "{DOWN " & ActiveCell.Row - 2 & "}{TAB 3}" Application.DisplayAlerts = False ActiveSheet.ShowDataForm Application.DisplayAlerts = True End Sub
-
Save by pressing Ctrl+S and leave the VBA editor by Alt+Q
-
To test the new macro:
-
Select your row
-
In the Developer tab click Macros
-
Ensure the
CurrRowForm
macro is selected -
Click the Run button
-
The result should look like this when the second row is the current:
-
You may assign the macro a hotkey by :
- Enter Developer > Macros
- Ensure the macro
CurrRowForm
is selected - Click Options
- Select your Shortcut key
- Click OK
You may also for CurrRowForm
Add a macro button to the ribbon.
Solution 3:
I haven't read any comments mentioning the built in Excel data form on the Data tab, Data Entry
group. Alt A Y 2 keys in Windows.
Formulas are read only, cell protection is maintained; however, there's no ability to change heights or widths of fields.
Fields will widen automatically but only to a point. You could set up a hotkey to avoid the Ribbon key sequence.