Excel Scrolling By Cutting Rows With Spin Buttons

Cut And Insert Row To Down/Up With VBA


          The selected row is cut and it is scrolled with spin buttons to down/up.
UserForm is opened as automatically when workbook is opened.
Spin buttons can be used on all sheets of the workbook.



The codes of userform that contains spin buttons:
Private Sub SpinButton1_SpinDown()
Rows(ActiveCell.Row).Cut
On Error Resume Next
Rows(ActiveCell.Row + 2).Insert Shift:=xlUp
Rows(ActiveCell.Row + 1).Select
End Sub

Private Sub SpinButton1_SpinUp()
Rows(ActiveCell.Row).Cut
On Error Resume Next
Rows(ActiveCell.Row - 1).Insert Shift:=xlDown
Rows(ActiveCell.Row - 1).Select
End Sub

Private Sub UserForm_Activate()
UserForm1.Top = 15
End Sub



Read more ...

Macro To Highlight Duplicate Records

Excel VBA Highlight Duplicate Values           


         The duplicate records are searched in the used ranges of all worksheets by Excel WorksheetFunction.CountIf  method  as following :
Sub duplicates_coloring()
Dim mycell As Range, syf As Byte

For syf = 1 To Sheets.Count
Sheets(syf).Select
ActiveSheet.UsedRange.Select

For Each mycell In Selection
    If WorksheetFunction.CountIf(Selection, mycell.Value) > 1 Then
        mycell.Interior.ColorIndex = 8
        mycell.Borders.Weight = xlThin
        mycell.Borders.ColorIndex = 44
    End If
Next
Next
Sheets(1).Select
End Sub

Background of found records are painted and new borders are drew  :
For Each mycell In Selection
If WorksheetFunction.CountIf(Selection, mycell.Value) > 1 Then
mycell.Interior.ColorIndex = 8
mycell.Borders.Weight = xlThin
mycell.Borders.ColorIndex = 44
End If
Next




If desired , the processes can be recovered with "Undo Button".



Read more ...

Listing The Filtered Data On Separate WorkBooks

Excel VBA : Creating New Workbooks From Unique Values In Worksheet And Hyperlinks For Workbooks


         A useful Excel macro.       
UserForm opens automatically when the workbook is opened.When pressed the button on the userform, unique values are listed with the filtering method in a new sheet. Named folder according to date is created.

         According to the unique values in the column A , new workbooks are created in this folder , the data are copied into this workbooks.The hyperlinks are created for this new Excel workbooks.







Excel Macro codes that to create new workbooks from unique values in the sheet and  to build hyperlinks for this workbooks :
Private Sub CommandButton1_Click()
Dim My_Range As Range, FieldNum As Long, FileExtStr As String
Dim FileFormatNum, CalcMode, ViewMode As Long
Dim ws2 As Worksheet, MyPath, foldername As String
Dim Lrow, CCount, ErrNum As Long, cell As Range, WSNew As Worksheet
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
Sheets("Data").Activate
Set My_Range = Range("A1:F" & LastRow(ActiveSheet))
My_Range.Parent.Select

If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new workbook"
Exit Sub
End If

'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False

'Set the file extension/format
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
If ActiveWorkbook.FileFormat = 56 Then
FileExtStr = ".xls": FileFormatNum = 56
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
End If

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False

' Add worksheet to copy/Paste the unique list
Set ws2 = Worksheets.Add(After:=Sheets(Sheets.Count))
ws2.Name = "LogSheet" & Sheets.Count
'Fill in the path\folder where you want the new folder with the files
MyPath = ThisWorkbook.Path & "\"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'Create folder for the new files
foldername = MyPath & Format(Now, "dd-mm-yyyy hh-mm-ss") & "\"
MkDir foldername

With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("A3"), Unique:=True
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A4:A" & Lrow)

'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add new workbook with one sheet
Set WSNew = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
'Save the file in the new folder and close it
On Error Resume Next
WSNew.Parent.SaveAs foldername & _
cell.Value & FileExtStr, FileFormatNum
If Err.Number > 0 Then
Err.Clear
ErrNum = ErrNum + 1
WSNew.Parent.SaveAs foldername & _
"Error_" & Format(ErrNum, "0000") & FileExtStr, FileFormatNum
.Cells(cell.Row, "B").Formula = "=Hyperlink(""" & foldername & _
"Error_" & Format(ErrNum, "0000") & FileExtStr & """)"
.Cells(cell.Row, "A").Interior.Color = vbRed
Else
.Cells(cell.Row, "B").Formula = _
"=Hyperlink(""" & foldername & cell.Value & FileExtStr & """)"
End If

WSNew.Parent.Close False
On Error GoTo 0
End If
'Show all the data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
.Cells(1, "A").Value = ""
.Cells(1, "B").Value = "THE CREATED WORKBOOKS"
.Cells(1, "B").Interior.ColorIndex = 3
.Cells(3, "A").Value = "Unique Values"
.Cells(3, "B").Value = "Workbooks' Path and Workbooks' Name"
.Cells(3, "B").Interior.ColorIndex = 42
.Cells(3, "A").Font.Bold = True
.Cells(3, "B").Font.Bold = True
.Columns("A:B").AutoFit
End With

'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
My_Range.Parent.Select
ActiveWindow.View = ViewMode
ws2.Select

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub

Function LastRow(sh As Worksheet)
  On Error Resume Next
  LastRow = sh.Cells.Find(What:="*", _
  After:=sh.Range("A1"), _
  Lookat:=xlPart, _
  LookIn:=xlValues, _
  SearchOrder:=xlByRows, _
  SearchDirection:=xlPrevious, _
  MatchCase:=False).Row
  On Error GoTo 0
End Function




Read more ...

Transferring Data From Excel To The Text File

Copy From Excel To Text


       In this example, the data are transferred into text file as separated by spaces. In the same folder with the Excel book, "records" named text file is created.

The data are transferred to this text file starting from the "column B".


excel transfer to text


Read more ...

Excel Vba Get Data From Closed Workbook

Excel Vba Get Data From Closed Workbook
Import Data From Closed Book Based On The Value In The Cell (Without Using ADO)

         The wanted values are being fetched from other closed books according to Product No. in the in main file's column B . The fetched values are listed in sequential rows.


          There are 3 books in the same folder -according to need may be more than files - : main_file.xls , 13,xls , 14.xls . In our example, product numbers are starting 13 or 14 . For this reason ,the files are opens according to product no. (13.xls or 14.xls) and values are entered into main_file.xls.


Read more ...

Copying The Chosen Numeric Values To The Other Sheet

Useful Macros - 12
Copying The Chosen Numeric Values To The Other Sheet

           In the previous examples, the columns that contain non-numeric values were copied .



We will copy the numerical values that comply with criteria in this template . 

There are numeric values in Column I. Now let's copy the great values from 20.000 in Column I to other sheet.
We can view number of the copied data on the opened msgbox.


VBA codes that we used to copy numeric values :
Sub run_transfer()
Dim i, k, a, filledr As Long, sh2 As Worksheet
Application.ScreenUpdating = False

Set sh2 = Sheets("Copied_Values")
a = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
last = sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1
filledr = WorksheetFunction.CountA(sh2.Range("A:A"))
For i = 1 To a
If VBA.Val(Cells(i, 9)) > 20000 Then
Sheets("Data").Rows(i).Copy sh2.Rows(last)
last = last + 1
End If
Next i
MsgBox "The copied record's number :" & " " & WorksheetFunction.CountA(sh2.Range("A:A")) - filledr
If WorksheetFunction.CountA(sh2.Range("A:A")) - filledr = 0 Then ' The number of data transferred are calculated
Exit Sub
Else

For k = 2 To ActiveSheet.Range("I" & Rows.Count).End(xlUp).Row
    sh2.Cells(k, 9).Font.ColorIndex = 3
Next

sh2.Range("A1:I1").Value = Sheets("Data").Range("A1:I1").Value  ' Column headings are set.
sh2.Columns("A:I").AutoFit                                     ' Column widths are set.

End If
Application.ScreenUpdating = True
i = Empty: k = Empty: a = Empty: filledr = Empty: Set sh2 = Nothing ' The variables are emptied to speed up the macro .
End Sub



Read more ...

Copying The Values To The Other Page

Useful Macros – 11
Copying The Chosen Values To The Other Sheet

            This template is similar to the example in previous tutorial (Useful Macros-10).
Difference between two example; chosen data  are just copied (data aren't cut) in this template.

Also in this template , font color of the data in the Column C was selected red :
For k = 2 To ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
 sh2.Cells(k, 3).Font.ColorIndex = 3
Next




Read more ...