0% found this document useful (0 votes)
7 views3 pages

VBA

The document contains VBA macros for Excel that automate the process of copying data from specified ranges at set intervals. It includes functionalities to start and stop timers, copy data from one range to another, and optimize performance by disabling screen updates during execution. The macros are designed to work with specific sheets and ranges, allowing for efficient data management within an Excel workbook.

Uploaded by

Rakesh Saharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

VBA

The document contains VBA macros for Excel that automate the process of copying data from specified ranges at set intervals. It includes functionalities to start and stop timers, copy data from one range to another, and optimize performance by disabling screen updates during execution. The macros are designed to work with specific sheets and ranges, allowing for efficient data management within an Excel workbook.

Uploaded by

Rakesh Saharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Dim timerActive As Boolean

Dim currentColumn As Long

Sub StartTimer()
If Not timerActive Then
timerActive = True
currentColumn = 13 ' Column M ka number (13)
Application.OnTime Now + TimeValue("00:01:00"), "CopyAndPasteData"
End If
End Sub

Sub StopTimer()
timerActive = False
On Error Resume Next ' Agar timer nahi chalu hai toh error ko ignore kare
Application.OnTime Now + TimeValue("00:01:00"), "CopyAndPasteData", , False
On Error GoTo 0
End Sub

Sub CopyAndPasteData()
Dim sourceRange As Range
Dim targetRange As Range

Set sourceRange = ThisWorkbook.Sheets("Sheet1").Range("L1:L20") ' Change


"Sheet1" to your actual sheet name
Set targetRange = ThisWorkbook.Sheets("Sheet1").Cells(1, currentColumn) '
Starting at row 1, next column

' Loop through each cell in the source range and copy to the target column
Dim i As Long
For i = 1 To sourceRange.Rows.Count
targetRange.Cells(i, 1).Value = sourceRange.Cells(i, 1).Value
Next i

currentColumn = currentColumn + 1 ' Move to the next column for the next paste
Application.OnTime Now + TimeValue("00:01:00"), "CopyAndPasteData" ' Restart
timer
End Sub

Dim NextCopy As Double

Sub StartCopying()
' 30 second ka interval set karna
NextCopy = Now + TimeValue("00:00:30")
Application.OnTime NextCopy, "CopyData"
End Sub

Sub StopCopying()
On Error Resume Next
Application.OnTime NextCopy, "CopyData", , False
End Sub

Sub CopyData()
Dim LastCol As Long
Dim CopyRange As Range

' L1:L20 ka data copy karna


Set CopyRange = Range("L1:L20")

' Last filled column find karna


LastCol = Cells(1, Columns.Count).End(xlToLeft).Column

' Next column mein data ko copy karna


CopyRange.Copy Destination:=Cells(1, LastCol + 1).Resize(CopyRange.Rows.Count,
CopyRange.Columns.Count)

' Next interval set karna


NextCopy = Now + TimeValue("00:00:30")
Application.OnTime NextCopy, "CopyData"
End Sub

Sub Macro1()
' Macro1 Macro
' Keyboard Shortcut: Ctrl + a

Dim sourceRange As Range


Dim targetCell As Range
Dim data() As Variant
Dim i As Integer, j As Integer

' Disable screen updating and calculations for better performance


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' Set the source range and target cell


Set sourceRange = Range("B2:K20") ' Adjusted to cover all relevant columns in
one go
Set targetCell = Range("C2")

' Read data from the source range into an array


data = sourceRange.Value

' Write the data directly to the target range


For j = LBound(data, 2) To UBound(data, 2)
For i = LBound(data, 1) To UBound(data, 1)
targetCell.Offset(i - 1, j - 1).Value = data(i, j)
Next i
Next j

' Clear the clipboard (unnecessary as we are not using copy/paste anymore)
Application.CutCopyMode = False

' Restore screen updating and calculations


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

Sub Macro1()
' Macro to copy values from B2:K20 to C2:K20 on Sheet2
Dim sourceRange As Range
Dim targetRange As Range
Dim data As Variant
Dim ws As Worksheet

' Set the worksheet to Sheet2


Set ws = ThisWorkbook.Sheets("Sheet2")

' Disable screen updating and calculations for better performance


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' Set the source range and target range


Set sourceRange = ws.Range("B2:K20") ' Source range on Sheet2
Set targetRange = ws.Range("C2") ' Starting point for pasting on Sheet2

' Store the data in an array


data = sourceRange.Value

' Directly assign values to target range


targetRange.Resize(UBound(data, 1), UBound(data, 2)).Value = data

' Restore screen updating and calculations


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

You might also like