Welcome, Guest
You have to register before you can post on our site.

Username/Email:
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 49
» Latest member: antwantillman
» Forum threads: 4,492
» Forum posts: 4,495

Full Statistics

Online Users
There are currently 1239 online users.
» 0 Member(s) | 1237 Guest(s)
Bing, Yandex

Latest Threads
SELECT statement with MS ...
Forum: MS Access SQL Tutorials
Last Post: Qomplainerz
07-27-2023, 03:35 PM
» Replies: 0
» Views: 1,074
SELECT statement with the...
Forum: MS Access SQL Tutorials
Last Post: Qomplainerz
07-27-2023, 03:31 PM
» Replies: 0
» Views: 528
Creating hyperlinks in HT...
Forum: HTML5 Tutorials
Last Post: Qomplainerz
07-27-2023, 01:23 PM
» Replies: 0
» Views: 818
What's new in HTML5?
Forum: HTML5 Tutorials
Last Post: Qomplainerz
07-27-2023, 12:48 PM
» Replies: 0
» Views: 543
What is HTML5?
Forum: HTML5 Tutorials
Last Post: Qomplainerz
07-27-2023, 12:43 PM
» Replies: 0
» Views: 508
Neck isometric exercises
Forum: Exercises
Last Post: Qomplainerz
07-27-2023, 11:44 AM
» Replies: 0
» Views: 807
Shoulder shrug
Forum: Exercises
Last Post: Qomplainerz
07-27-2023, 11:43 AM
» Replies: 0
» Views: 462
Neck retraction
Forum: Exercises
Last Post: Qomplainerz
07-27-2023, 11:43 AM
» Replies: 0
» Views: 430
Neck flexion and extensio...
Forum: Exercises
Last Post: Qomplainerz
07-27-2023, 11:42 AM
» Replies: 0
» Views: 498
Neck rotation
Forum: Exercises
Last Post: Qomplainerz
07-27-2023, 11:42 AM
» Replies: 0
» Views: 470

 
  Generate random numbers in VBA
Posted by: Qomplainerz - 07-27-2023, 11:03 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub GenerateRandomNumbers()
    Dim i As Long

    'Generate and place 10 random numbers in column A
    For i = 1 To 10
        Cells(i, "A").Value = Int((100 * Rnd) + 1)
    Next i
End Sub

Explanation:

The code defines a subroutine named GenerateRandomNumbers.
It declares a loop counter i.
The For loop runs from 1 to 10, generating ten random numbers.
Rnd is used to generate a random number between 0 and 1. 
(100 * Rnd) generates a random number between 0 and 100, 
and Int((100 * Rnd) + 1) generates a random integer between 1 and 100.
The generated random number is placed in column A for each iteration using Cells(i, "A").Value.

Print this item

  Concatenate data in columns
Posted by: Qomplainerz - 07-27-2023, 11:01 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub ConcatenateColumns()
    Dim lastRow As Long
    Dim i As Long

    'Find the last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    'Loop through each row and concatenate values from columns A and B
    For i = 1 To lastRow
        Cells(i, "C").Value = Cells(i, "A").Value & " " & Cells(i, "B").Value
    Next i
End Sub

Explanation:

The code defines a subroutine named ConcatenateColumns.
It declares variables for the last row with data (lastRow) and a loop counter (i).
The lastRow variable is determined using Cells(Rows.Count, "A").End(xlUp).Row, which finds the last row with data in column A.
The For loop iterates through each row from 1 to lastRow.
In each iteration, the values from columns A and B are concatenated with a space (" ") in between, and the result is placed in column C using Cells(i, "C").Value.

Print this item

  Calculate the avg and color the cells when their values are above or below the avg
Posted by: Qomplainerz - 07-27-2023, 10:41 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub CalculateAverageAndColorCode()
    Dim dataRange As Range
    Dim avgValue As Double
    Dim cell As Range

    'Set the range where you want to calculate the average and apply color coding
    Set dataRange = Range("A1:A10")

    'Calculate the average value of the range
    avgValue = Application.WorksheetFunction.Average(dataRange)

    'Color code cells based on whether their value is above or below the average
    For Each cell In dataRange
        If cell.Value > avgValue Then
            cell.Interior.Color = RGB(0, 255, 0) 'Green color for above average
        ElseIf cell.Value < avgValue Then
            cell.Interior.Color = RGB(255, 0, 0) 'Red color for below average
        End If
    Next cell
End Sub

Explanation:

The code defines a subroutine named CalculateAverageAndColorCode.
It declares variables for the data range (dataRange), the average value (avgValue), and a loop variable (cell) to iterate through each cell in the range.
Set dataRange = Range("A1:A10") specifies the range A1:A10 where you want to calculate the average and apply color coding.
The Application.WorksheetFunction.Average function is used to calculate the average value of the dataRange.
The For Each loop is used to iterate through each cell in dataRange.
If a cell's value is above the average, it is colored green using cell.Interior.Color = RGB(0, 255, 0).
If a cell's value is below the average, it is colored red using cell.Interior.Color = RGB(255, 0, 0).

Print this item

  Protect specific cells with VBA
Posted by: Qomplainerz - 07-27-2023, 10:39 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub ProtectSpecificCells()
    Dim ws As Worksheet
    Dim protectedRange As Range

    'Set the worksheet where the cells are located
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Set the range of cells to be protected (adjust the range as per your data)
    Set protectedRange = ws.Range("B2Big Grin10")

    'Protect the specified cells with a password
    ws.Protect Password:="mypassword", UserInterfaceOnly:=True
    protectedRange.Locked = True
End Sub

Explanation:

The code defines a subroutine named ProtectSpecificCells.
It declares variables for the worksheet (ws) and the range of cells to be protected (protectedRange).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the cells are located.
Set protectedRange = ws.Range("B2Big Grin10") sets the range of cells B2 to D10 to be protected.
The ws.Protect method is used to protect the entire worksheet with a password ("mypassword"). 
The UserInterfaceOnly:=True argument allows VBA code to modify protected cells without unprotecting the worksheet.
protectedRange.Locked = True locks the specified cells within the protected worksheet.

Print this item

  Find a specific value in a range and replace it with a new value
Posted by: Qomplainerz - 07-27-2023, 10:38 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub FindAndReplace()
    Dim searchRange As Range
    Dim findValue As Variant
    Dim replaceValue As Variant

    'Set the range where you want to find and replace values
    Set searchRange = Range("A1:A10")

    'Specify the value to find and the value to replace it with
    findValue = "OldValue"
    replaceValue = "NewValue"

    'Find and replace the value in the range
    searchRange.Replace What:=findValue, Replacement:=replaceValue, LookAt:=xlWhole
End Sub

Explanation:

The code defines a subroutine named FindAndReplace.
It declares variables for the search range (searchRange), the value to find (findValue), and the value to replace it with (replaceValue).
Set searchRange = Range("A1:A10") specifies the range A1:A10 where you want to find and replace values.
findValue = "OldValue" and replaceValue = "NewValue" set the values to be found and replaced, respectively.
The searchRange.Replace method is used to find and replace the value. 
What:=findValue specifies the value to find, Replacement:=replaceValue specifies the value to replace it with, and LookAt:=xlWhole indicates that it should find whole matches.

Print this item

  Create a pivot table based on data in a worksheet with VBA
Posted by: Qomplainerz - 07-27-2023, 09:48 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub CreatePivotTable()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim ptCache As PivotCache
    Dim ptRange As Range

    'Set the worksheet where the data is located
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Set the range of data for the pivot table (adjust the range as per your data)
    Set ptRange = ws.Range("A1:B10")

    'Create a new pivot cache based on the data range
    Set ptCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ptRange)

    'Create a new pivot table on a new worksheet
    Set pt = ptCache.CreatePivotTable(TableDestination:=ws.Cells(1, 4), TableName:="MyPivotTable")
    With pt
        .PivotFields("Category").Orientation = xlRowField
        .AddDataField .PivotFields("Value"), "Sum of Value", xlSum
    End With
End Sub

Explanation:

The code defines a subroutine named CreatePivotTable.
It declares variables for the worksheet (ws), pivot table (pt), pivot cache (ptCache), and the range of data for the pivot table (ptRange).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the data is located.
The ptRange variable is set to the range of data for the pivot table (A1:B10 in this example).
A new pivot cache is created based on the data range using ThisWorkbook.PivotCaches.Create.
The pivot table is created on a new worksheet at cell D1 (TableDestination:=ws.Cells(1, 4)) with the name "MyPivotTable" using ptCache.CreatePivotTable.
The With block is used to customize the pivot table. 
In this example, the "Category" field is set as a row field, and the "Value" field is added as a data field with the aggregation function xlSum.

Print this item

  Delete rows based on a condition
Posted by: Qomplainerz - 07-27-2023, 09:47 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub DeleteRowsBasedOnCondition()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long

    'Set the worksheet where the data is located
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Find the last row with data in column A
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    'Loop through the rows in reverse order to avoid skipping rows after deletion
    For i = lastRow To 2 Step -1
        If ws.Cells(i, "A").Value < 10 Then
            ws.Rows(i).Delete
        End If
    Next i
End Sub

Explanation:

The code defines a subroutine named DeleteRowsBasedOnCondition.
It declares variables for the worksheet (ws), the last row with data in column A (lastRow), and a loop counter (i).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the data is located.
The lastRow variable is determined using ws.Cells(ws.Rows.Count, "A").End(xlUp).Row, which finds the last row with data in column A.
The For loop iterates through the rows in reverse order (from the last row to the second row) to avoid skipping rows after deletion.
The If statement checks the value in column A for each row. If the value is less than 10, the entire row is deleted using ws.Rows(i).Delete.

Print this item

  Filter data in a worksheet
Posted by: Qomplainerz - 07-27-2023, 09:46 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub FilterData()
    Dim filterRange As Range

    'Set the range to be filtered (assuming data starts from A1)
    Set filterRange = Range("A1").CurrentRegion

    'Apply filter to column A for values greater than 5
    filterRange.AutoFilter Field:=1, Criteria1:=">5"
End Sub

Explanation:

The code defines a subroutine named FilterData.
It declares a variable filterRange, which represents the range to be filtered. 
In this example, the range is set to the data starting from cell A1 using the CurrentRegion property.
The filterRange.AutoFilter method is used to apply a filter to the range.
Field:=1 specifies that the filter should be applied to the first column of filterRange (column A).
Criteria1:=">5" sets the filtering criteria to show only values greater than 5.

Print this item

  Create a chart from data with VBA
Posted by: Qomplainerz - 07-27-2023, 09:44 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub CreateChart()
    Dim chartSheet As Worksheet
    Dim dataSheet As Worksheet
    Dim chartObj As ChartObject
    Dim chartRange As Range

    'Set the data sheet where the chart data is located
    Set dataSheet = ThisWorkbook.Sheets("Sheet1")

    'Set the range of data for the chart (adjust the range as per your data)
    Set chartRange = dataSheet.Range("A1:B5")

    'Create a new worksheet to place the chart
    Set chartSheet = ThisWorkbook.Sheets.Add

    'Create the chart as a column chart on the new worksheet
    Set chartObj = chartSheet.ChartObjects.Add(Left:=50, Width:=375, Top:=75, Height:=225)
    With chartObj.Chart
        .SetSourceData Source:=chartRange
        .ChartType = xlColumnClustered
    End With
End Sub

Explanation:

The code defines a subroutine named CreateChart.
It declares variables for the chart sheet (chartSheet), data sheet (dataSheet), chart object (chartObj), and chart data range (chartRange).
Set dataSheet = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the chart data is located.
Set chartRange = dataSheet.Range("A1:B5") sets the range of data for the chart (adjust the range as per your data).
A new worksheet (chartSheet) is created to place the chart using ThisWorkbook.Sheets.Add.
The chart is created as a column chart on the new worksheet using chartSheet.ChartObjects.Add. 
The chart dimensions and position are adjusted using the Left, Width, Top, and Height parameters.
The chart source data is set to chartRange using .SetSourceData Source:=chartRange.
The chart type is set to a clustered column chart using .ChartType = xlColumnClustered.

Print this item

  Sort data in a range with VBA
Posted by: Qomplainerz - 07-27-2023, 09:43 AM - Forum: Excel VBA Tutorials - No Replies

Example:

Sub SortData()
    Dim sortRange As Range

    'Set the range to be sorted (assuming data starts from A1)
    Set sortRange = Range("A1").CurrentRegion

    'Sort the range in ascending order based on values in column A
    sortRange.Sort Key1:=sortRange.Columns(1), Order1:=xlAscending, Header:=xlYes
End Sub

Explanation:

The code defines a subroutine named SortData.
It declares a variable sortRange, which represents the range to be sorted. In this example, the range is set to the data starting from cell A1 using the CurrentRegion property.
The sortRange.Sort method is used to sort the range in ascending order.
Key1:=sortRange.Columns(1) specifies that the sorting should be based on values in column A (the first column of sortRange).
Order1:=xlAscending sets the sorting order to ascending.
Header:=xlYes indicates that the range has a header row, and sorting should include the header.

Print this item