Option Explicit
Private Sub cmdAdd_Click()
Dim strLastRow As Integer
strLastRow = xlLastRow("Sheet1")
With UserForm1
If (.TextBox1.Value <> vbNullString And .TextBox2.Value <> vbNullString And _
.TextBox3.Value <> vbNullString) Then
Cells(strLastRow + 1, 1).Value = UserForm1.TextBox1.Value
Cells(strLastRow + 1, 2).Value = UserForm1.TextBox2.Value
Cells(strLastRow + 1, 3).Value = UserForm1.TextBox3.Value
strLastRow = strLastRow + 1
UserForm1.ListBox1.RowSource = "Sheet1!A2:C" & strLastRow
.TextBox1.Value = vbNullString
.TextBox2.Value = vbNullString
.TextBox3.Value = vbNullString
Else
MsgBox "Please Enter Data"
End If
End With
End Sub
Private Sub cmdDel_Click()
With UserForm1.ListBox1
If (.Value <> vbNullString) Then
If (.ListIndex >= 0 And xlLastRow("Sheet1") > 2) Then
Range(.RowSource)(.ListIndex + 1, 1).EntireRow.Delete
.RowSource = "Sheet1!A2:C" & xlLastRow("Sheet1")
ElseIf (.ListIndex = 0 And xlLastRow("Sheet1") = 2) Then
Range(.RowSource)(.ListIndex + 1, 1).EntireRow.Delete
.RowSource = "Sheet1!A2:C2"
End If
Else
MsgBox "Please Select Data"
End If
End With
End Sub
Private Sub ListBox1_Change()
Dim SourceRange As Excel.Range
Dim Val1 As String, Val2 As String, Val3 As String
If (ListBox1.RowSource <> vbNullString) Then
Set SourceRange = Range(ListBox1.RowSource)
Else
Set SourceRange = Range("Sheet1!A2:C2")
Exit Sub
End If
Val1 = ListBox1.Value
Val2 = SourceRange.Offset(ListBox1.ListIndex, 1).Resize(1, 1).Value
Val3 = SourceRange.Offset(ListBox1.ListIndex, 2).Resize(1, 1).Value
Label1.Caption = "Selected Data: " & vbNewLine & Val1 & " " & Val2 & " " & Val3
Set SourceRange = Nothing
End Sub
Private Sub UserForm_Initialize()
DeleteBlankRows
DeleteBlankColumns
With Me.ListBox1
.BoundColumn = 1
.ColumnCount = 3
.ColumnHeads = True
.TextColumn = True
.RowSource = "Sheet1!A2:C" & xlLastRow("Sheet1")
.ListStyle = fmListStyleOption
.ListIndex = 0
End With
End Sub
Option Explicit
Sub DeleteBlankRows()
Dim Rw As Long, RwCnt As Long, Rng As Excel.Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error GoTo Exits:
If Selection.Rows.Count > 1 Then
Set Rng = Selection
Else
Set Rng = Range(Rows(1), _
Rows(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row()))
End If
RwCnt = 0
For Rw = Rng.Rows.Count To 1 Step -1
If Application.WorksheetFunction.CountA(Rng.Rows(Rw).EntireRow) = 0 Then
Rng.Rows(Rw).EntireRow.Delete
RwCnt = RwCnt + 1
End If
Next Rw
Exits:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Set Rng = Nothing
End Sub
Sub DeleteBlankColumns()
Dim Col As Long, ColCnt As Long, Rng As Excel.Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error GoTo Exits:
If Selection.Columns.Count > 1 Then
Set Rng = Selection
Else
Set Rng = Range(Columns(1), Columns(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column()))
End If
ColCnt = 0
For Col = Rng.Columns.Count To 1 Step -1
If Application.WorksheetFunction.CountA(Rng.Columns(Col).EntireColumn) = 0 Then
Rng.Columns(Col).EntireColumn.Delete
ColCnt = ColCnt + 1
End If
Next Col
Exits:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Set Rng = Nothing
End Sub
Option Explicit
Dim MyRange As Excel.Range
Sub MoveIt()
Set MyRange = Range((Cells(xlFirstRow, xlFirstCol)), (Cells(xlLastRow, xlLastCol)))
MyRange.Copy Destination:=Sheets("Sheet2").Range("B4")
Set MyRange = Nothing
End Sub
Function xlFirstCol(Optional WorksheetName As String) As Long
If WorksheetName = vbNullString Then
WorksheetName = ActiveSheet.Name
End If
With Worksheets(WorksheetName)
xlFirstCol = .Cells.Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByColumns, xlNext).Column
End With
End Function
Function xlFirstRow(Optional WorksheetName As String) As Long
If WorksheetName = vbNullString Then
WorksheetName = ActiveSheet.Name
End If
With Worksheets(WorksheetName)
xlFirstRow = .Cells.Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByRows, xlNext).Row
End With
End Function
Function xlLastRow(Optional WorksheetName As String) As Long
If WorksheetName = vbNullString Then
WorksheetName = ActiveSheet.Name
End If
With Worksheets(WorksheetName)
xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByRows, xlPrevious).Row
End With
End Function
Function xlLastCol(Optional WorksheetName As String) As Long
If WorksheetName = vbNullString Then
WorksheetName = ActiveSheet.Name
End If
With Worksheets(WorksheetName)
xlLastCol = .Cells.Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByColumns, xlPrevious).Column
End With
End Function
Sub Button1_Click()
UserForm1.Show
End Sub
|