Friday, April 17, 2020

Getting column letter from column number,selecting the column and change the column format using vba

'MsgBox (Split(Cells(1, ActiveCell.Column).address, "$")(1))
Dim rangecol As Range
Set rangecol = Range(Split(Cells(1, ActiveCell.Column).address, "$")(1) & ":" & Split(Cells(1, ActiveCell.Column).address, "$")(1))
'MsgBox (rangecol.address)

rangecol.NumberFormat = "General"

Monday, April 13, 2020

Subtracting one range from another in VBA

Option Explicit
Private mrBuild As Range
Sub selection_experiment()
Sheets("RCformula").Select

Dim workingrange As Range


Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Set workingrange = Selection
MsgBox (workingrange.Address)
Dim removefromrange As Range
Range("A1").Select
Selection.End(xlDown).Select
Set removefromrange = Selection
MsgBox (removefromrange.Address)
MsgBox (SubtractRanges(workingrange, removefromrange).Address)


End Sub



Public Function SubtractRanges(rFirst As Range, rSecond As Range) As Range

    Dim rInter As Range
    Dim rReturn As Range
    Dim rArea As Range

    Set rInter = Intersect(rFirst, rSecond)
    Set mrBuild = Nothing

    If rInter Is Nothing Then 'No overlap
        Set rReturn = rFirst
    ElseIf rInter.Address = rFirst.Address Then 'total overlap
        Set rReturn = Nothing
    Else 'partial overlap
        For Each rArea In rFirst.Areas
            BuildRange rArea, rInter
        Next rArea
        Set rReturn = mrBuild
    End If

    Set SubtractRanges = rReturn

End Function

Sub BuildRange(rArea As Range, rInter As Range)

    Dim rLeft As Range, rRight As Range
    Dim rTop As Range, rBottom As Range

    If Intersect(rArea, rInter) Is Nothing Then 'no overlap
        If mrBuild Is Nothing Then
            Set mrBuild = rArea
        Else
            Set mrBuild = Union(mrBuild, rArea)
        End If
    Else 'some overlap
        If rArea.Columns.count = 1 Then 'we've exhausted columns, so split on rows
            If rArea.Rows.count > 1 Then 'if one cell left, don't do anything
                Set rTop = rArea.Resize(rArea.Rows.count \ 2) 'split the range top to bottom
                Set rBottom = rArea.Resize(rArea.Rows.count - rTop.Rows.count).Offset(rTop.Rows.count)
                BuildRange rTop, rInter 'rerun it
                BuildRange rBottom, rInter
            End If
        Else
            Set rLeft = rArea.Resize(, rArea.Columns.count \ 2) 'split the range left to right
            Set rRight = rArea.Resize(, rArea.Columns.count - rLeft.Columns.count).Offset(, rLeft.Columns.count)
            BuildRange rLeft, rInter 'rerun it
            BuildRange rRight, rInter
        End If
    End If

End Sub


Source:https://stackoverflow.com/questions/21580795/subtracting-ranges-in-vba-excel

Sunday, April 12, 2020

Download youtube videos from a playlist between start and end index using youtube-dl

youtube-dl --no-check-certificate -cio "E:/powerpoint_vba/access_vba/%(title)s.%(ext)s" --playlist-start 1 --playlist-end 6 https://www.youtube.com/playlist?list=PLYMOUCVo86jEeMMdaaq03jQ_t9nFV737s

Using Excel formulas with and without RC notation in VBA,VBA Teacher Sourav,Kolkata 08910141720

 Without RC notation

Sub usingexcelfunctioninvba()

Sheets("prg").Select
Dim workingrange As Range


Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Set workingrange = Selection
Dim sumresult As Integer
sumresult = Application.sum(workingrange)
Dim average As Double
average = Application.average(workingrange)
Dim count As Integer
count = Application.count(workingrange)
Dim max, min As Double
max = Application.max(workingrange)
min = Application.min(workingrange)
Dim stdevsample As Double
stdevsample = Application.WorksheetFunction.StDev_S(workingrange)
Dim stdevpopulation As Double
stdevpopulation = Application.WorksheetFunction.StDev_P(workingrange)
Dim median, mean, mode As Double
median = Application.WorksheetFunction.median(workingrange)
'mean = Application.WorksheetFunction.mean(workingrange)
mode = Application.mode(workingrange)
Range("A5").Value = sumresult
Range("B5").Value = average
Range("C5").Value = count
Range("D5").Value = max
Range("E5").Value = min
Range("F5").Value = stdevsample
Range("G5").Value = stdevpopulation
Range("H5").Value = average
Range("I5").Value = median
Range("J5").Value = mode

End Sub



With RC notation

Option Explicit

Sub usingrcformulainvba()
Sheets("prg").Select
Range("A1").Select
Dim firstrangerow As Integer
firstrangerow = (ActiveCell.Row)
Dim firstrangecolumn As Integer
firstrangecolumn = (ActiveCell.Column)
Selection.End(xlToRight).Select
Dim lastrangerow As Integer
lastrangerow = (ActiveCell.Row)
Dim lastrangecolumn As Integer
lastrangecolumn = (ActiveCell.Column)

Range("A7").Select

Dim currentrangerow As Integer
currentrangerow = (ActiveCell.Row)
Dim currentrangecolumn As Integer
currentrangecolumn = (ActiveCell.Column)

'MsgBox (firstrangerow - currentrangerow)


ActiveCell.FormulaR1C1 = "=SUM(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"
ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=Average(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=count(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=max(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=min(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=stdev.s(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=stdev.p(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=average(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"

ActiveCell.Offset(0, 1).Select

currentrangerow = (ActiveCell.Row)

currentrangecolumn = (ActiveCell.Column)
ActiveCell.FormulaR1C1 = "=median(R[" & (firstrangerow - currentrangerow) & "]C[" & (firstrangecolumn - currentrangecolumn) & "]:R[" & (lastrangerow - currentrangerow) & "]C[" & (lastrangecolumn - currentrangecolumn) & "])"
End Sub

Friday, April 10, 2020

youtube-dl error when downloading videos from youtube 'urlopen error' solved

Instead of 

C:\Windows\system32> youtube-dl  -cio "E:/powerpoint_vba/%(title)s.%(ext)s" http
s://www.youtube.com/playlist?list=PLyKV-uDcp5XgJ8FqczLBf2XxKjmQUvcz6


we should use

C:\Windows\system32> youtube-dl --no-check-certificate  -cio "E:/powerpoint_vba/
%(title)s.%(ext)s" https://www.youtube.com/playlist?list=PLyKV-uDcp5XgJ8FqczLBf2
XxKjmQUvcz6


Source:https://github.com/ytdl-org/youtube-dl/issues/883