Practice of for Loops
Sub practice1_loops15feb()
'Code to add a folder name months
MkDir "D:\Study\Excel and VBA Practice\Training Clases\months"
'Code to add 12 workbooks by month names
For x = 1 To 12
name1 = MonthName(x, True)
Workbooks.Add
ActiveWorkbook.SaveAs "D:\Study\Excel and VBA Practice\Training Clases\months\" & name1 & ".xlsx"
ActiveWorkbook.Close True
Next x
End Sub
Sub practice2_loops15feb()
'Code to make a list of month in Sheets1
For x = 1 To 12
name1 = MonthName(x, True)
Sheets("Sheet2").Cells(x, 1) = name1
Next x
End Sub
Sub practice3_loops15feb()
'Code to add Sheets
For i = 1 To 9
Sheets.Add after:=Sheets(Sheets.Count)
Next i
'1st Method to rename Sheets
For x = 1 To 12
Sheets("Sheet" & x).Name = Sheets("Sheet2").Cells(x, 1)
Next x
'2nd Method to rename Sheets
For x = 1 To 12
name1 = MonthName(x, True)
Sheets("Sheet" & x).Name = name1
Next x
End Sub
Sub practice4_loops15feb()
'Code to Select range
Dim x As Range
Set x = Sheets("Sheet1").Range("i2:cl156")
'Code to color yellow and blank each cell where value is 1
For Each cell In x
If cell.Value = 1 Then
cell.Interior.Color = vbYellow
cell.Value = ""
End If
Next
'Code to color white and fill 1 each cell where interior color is yellow
For Each cell In x
If cell.Interior.Color = vbYellow Then
cell.Interior.Color = RGB(255, 255, 255)
cell.Value = 1
End If
Next
End Sub
Sub for_dual_loop_list()
'Macro Program to print list
k = 2
For y = 2 To Application.WorksheetFunction.CountA(Sheets("Mar").Range("a:a"))
For x = 1 To Sheets("Mar").Cells(y, 2)
Sheets("Mar").Cells(k, 7) = Cells(y, 1)
Sheets("Mar").Cells(k, 8) = x
k = k + 1
Next x
Next y
End Sub
Sub Old_Program_via_for_loop()
MkDir "D:\Study\Excel and VBA Practice\Training Clases\VBAPractice"
For i = 1 To 3
name1 = MonthName(i, True)
Workbooks.Add
ActiveWorkbook.SaveAs "D:\Study\Excel and VBA Practice\Training Clases\VBAPractice\" & name1 & ".xlsx"
Sheets("Sheet1").Range("A1:A20") = name1
Sheets("Sheet1").Range("B1:B20") = Application.WorksheetFunction.RandBetween(1, 31)
ActiveWorkbook.Close True
Next i
Workbooks.Add
ActiveWorkbook.SaveAs "D:\Study\Excel and VBA Practice\Training Clases\months\working.xlsx"
For i = 1 To 3
name1 = MonthName(i, True)
Workbooks.Open "D:\Study\Excel and VBA Practice\Training Clases\VBAPractice\" & name1 & ".xlsx"
For k = 1 To Application.WorksheetFunction.CountA(Workbooks(name1 & ".xlsx").Sheets("Sheet1").Range("A:A"))
With Workbooks("working.xlsx").Sheets("Sheet" & i)
.Cells(k, 1) = Workbooks((name1 & ".xlsx")).Sheets("Sheet1").Cells(k, 1)
.Cells(k, 2) = Workbooks((name1 & ".xlsx")).Sheets("Sheet1").Cells(k, 2)
End With
Next k
Workbooks((name1 & ".xlsx")).Close True
Next i
Workbooks("working.xlsx").Activate
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Report"
For i = 1 To 3
name1 = MonthName(i, True)
Sheets("Sheet" & i).Name = name1
Next i
For i = 1 To 3
name1 = MonthName(i, True)
Workbooks("Working.xlsx").Sheets(name1).Activate
Range("A1").CurrentRegion.Copy
Sheets("Report").Activate
Range("A2000").End(xlUp).PasteSpecial xlPasteValues
'ActiveCell.EntireRow.Delete
Application.DisplayAlerts = False
Sheets(name1).Delete
Next i
End Sub
'Code to add a folder name months
MkDir "D:\Study\Excel and VBA Practice\Training Clases\months"
'Code to add 12 workbooks by month names
For x = 1 To 12
name1 = MonthName(x, True)
Workbooks.Add
ActiveWorkbook.SaveAs "D:\Study\Excel and VBA Practice\Training Clases\months\" & name1 & ".xlsx"
ActiveWorkbook.Close True
Next x
End Sub
Sub practice2_loops15feb()
'Code to make a list of month in Sheets1
For x = 1 To 12
name1 = MonthName(x, True)
Sheets("Sheet2").Cells(x, 1) = name1
Next x
End Sub
Sub practice3_loops15feb()
'Code to add Sheets
For i = 1 To 9
Sheets.Add after:=Sheets(Sheets.Count)
Next i
'1st Method to rename Sheets
For x = 1 To 12
Sheets("Sheet" & x).Name = Sheets("Sheet2").Cells(x, 1)
Next x
'2nd Method to rename Sheets
For x = 1 To 12
name1 = MonthName(x, True)
Sheets("Sheet" & x).Name = name1
Next x
End Sub
Sub practice4_loops15feb()
'Code to Select range
Dim x As Range
Set x = Sheets("Sheet1").Range("i2:cl156")
'Code to color yellow and blank each cell where value is 1
For Each cell In x
If cell.Value = 1 Then
cell.Interior.Color = vbYellow
cell.Value = ""
End If
Next
'Code to color white and fill 1 each cell where interior color is yellow
For Each cell In x
If cell.Interior.Color = vbYellow Then
cell.Interior.Color = RGB(255, 255, 255)
cell.Value = 1
End If
Next
End Sub
Sub for_dual_loop_list()
'Macro Program to print list
k = 2
For y = 2 To Application.WorksheetFunction.CountA(Sheets("Mar").Range("a:a"))
For x = 1 To Sheets("Mar").Cells(y, 2)
Sheets("Mar").Cells(k, 7) = Cells(y, 1)
Sheets("Mar").Cells(k, 8) = x
k = k + 1
Next x
Next y
End Sub
Sub Old_Program_via_for_loop()
MkDir "D:\Study\Excel and VBA Practice\Training Clases\VBAPractice"
For i = 1 To 3
name1 = MonthName(i, True)
Workbooks.Add
ActiveWorkbook.SaveAs "D:\Study\Excel and VBA Practice\Training Clases\VBAPractice\" & name1 & ".xlsx"
Sheets("Sheet1").Range("A1:A20") = name1
Sheets("Sheet1").Range("B1:B20") = Application.WorksheetFunction.RandBetween(1, 31)
ActiveWorkbook.Close True
Next i
Workbooks.Add
ActiveWorkbook.SaveAs "D:\Study\Excel and VBA Practice\Training Clases\months\working.xlsx"
For i = 1 To 3
name1 = MonthName(i, True)
Workbooks.Open "D:\Study\Excel and VBA Practice\Training Clases\VBAPractice\" & name1 & ".xlsx"
For k = 1 To Application.WorksheetFunction.CountA(Workbooks(name1 & ".xlsx").Sheets("Sheet1").Range("A:A"))
With Workbooks("working.xlsx").Sheets("Sheet" & i)
.Cells(k, 1) = Workbooks((name1 & ".xlsx")).Sheets("Sheet1").Cells(k, 1)
.Cells(k, 2) = Workbooks((name1 & ".xlsx")).Sheets("Sheet1").Cells(k, 2)
End With
Next k
Workbooks((name1 & ".xlsx")).Close True
Next i
Workbooks("working.xlsx").Activate
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Report"
For i = 1 To 3
name1 = MonthName(i, True)
Sheets("Sheet" & i).Name = name1
Next i
For i = 1 To 3
name1 = MonthName(i, True)
Workbooks("Working.xlsx").Sheets(name1).Activate
Range("A1").CurrentRegion.Copy
Sheets("Report").Activate
Range("A2000").End(xlUp).PasteSpecial xlPasteValues
'ActiveCell.EntireRow.Delete
Application.DisplayAlerts = False
Sheets(name1).Delete
Next i
End Sub
Comments
Post a Comment