Can you please provide a VBA event to do the following: 'COPY XTRA WORK IN "B" TO "F"…
- User
Can you please provide a VBA event to do the following:
'COPY XTRA WORK IN "B" TO "F" Dim wbkx As Workbook Dim wsx As Worksheet Dim wsxr As Range
Set wbkx = ThisWorkbook Set wsx = ThisWorkbook.Worksheets("Today") Set wsxr = wsf.Range("B18:B21") Application.EnableEvents = False Application.ScreenUpdating = False wsf.Activate
codeFor Each x In Range("B18:B21") If x.Offset(0, 1).Value <> "x" Then copy x to the next available row after B25 End If Next xApplication.ScreenUpdating = True Application.EnableEvents = True
- Assistant
Here is the VBA event that copies the cells in range "B18:B21" to the next available row after B25 in the worksheet "F":
VBA - User
I want to run the code from a button and not as a change event. I also want to copy the values to the same sheet 'Today' and it must paste results after B24
- Assistant
If you want to run the code from a button instead of a change event, you can create a button in your worksheet and assign the following macro to it:
VBASub CopyXtraWork() Dim wbkx As Workbook Dim wsx As Worksheet Dim wsToday As Worksheet Dim wsxr As Range Dim x As Range Dim lastRow As Long Set wbkx = ThisWorkbook Set wsx = wbkx.Worksheets("Today") Set wsToday = wbkx.Worksheets("Today") Set wsxr = wsx.Range("B18:B21") Application.EnableEvents = False Application.ScreenUpdating = False For Each x In wsxr If x.Offset(0, 1).Value <> "x" Then lastRow = wsToday.Cells(wsToday.Rows.Count, "B").End(xlUp).Row + 1 x.Copy Destination:=wsToday.Cells(lastRow, "B") End If Next x Application.ScreenUpdating = True Application.EnableEvents = True End Sub - User
In the code, there is no indication that range “B18:B21” in the “Today” worksheet will be pasted after B24 in the same sheet.
- Assistant
Apologies for the confusion. If you want to paste the values from range "B18:B21" in the "Today" worksheet after cell B24 in the same sheet, you can modify the code as follows:
VBASub CopyXtraWork() Dim wbkx As Workbook Dim wsx As Worksheet Dim wsToday As Worksheet Dim wsxr As Range Dim x As Range Dim lastRow As Long Set wbkx = ThisWorkbook Set wsx = wbkx.Worksheets("Today") Set wsToday = wbkx.Worksheets("Today") Set wsxr = wsx.Range("B18:B21") Application.EnableEvents = False Application.ScreenUpdating = False lastRow = wsToday.Cells(wsToday.Rows.Count, "B").End(xlUp).Row For Each x In wsxr If x.Offset(0, 1).Value <> "x" Then lastRow = lastRow + 1 wsToday.Cells(lastRow, "B").Value = x.Value End If Next x Application.ScreenUpdating = True Application.EnableEvents = True End Sub