VBA code to copy a one cell and a range of cells in one column to another sheet

bdt

New Member
Joined
Oct 3, 2024
Messages
42
Office Version
  1. 2019
Platform
  1. Windows
Hi, absolute novice with VBA and first post so please be gentle.
I have a work sheet (ABACUS) that has a cell (C2) with a week ending date and a range of cells (AX22:AX32) that have a number in them. I would like to run some VBA code to copy this data into another sheet (OVERTIME) with the cell C2 being added to column A and (AX22:AX32) being added to columns B to L in the same row.
The following week ending the same data would be added to OVERTIME but the row below.
Is this even possible? Any guidance much appreciated. Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Alt-F11 to open the VBA Project Model. Double click ThisWorkbook of your open workbook. Paste the following code in it and hit F5.
VBA Code:
Option Explicit

Sub pasteRange()

Dim wb As Workbook, aSht As Worksheet, oSht As Worksheet, rng As Range, kWrd, cell As Range
Dim i

Set wb = ThisWorkbook: Set aSht = wb.Sheets("ABACUS")
Set rng = aSht.Range("AX22", "AX32")
kWrd = aSht.Cells(2, 3)
i = 2

Set oSht = Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))


With oSht
    .Cells(1, 1) = kWrd
    For Each cell In rng
       .Cells(1, i) = cell.Value
       i = i + 1
    Next cell
    .Name = "OVERTIME"
End With



End Sub
 
Upvote 0
another option, assuming your OVERTIME sheet already exists
paste this into a standard module
VBA Code:
Sub bdt()
    Dim WKend As Date
    Dim arr As Variant
    Dim writerow As Long
    
With Sheets("ABACUS")
    WKend = .Range("C2").Value
    arr = .Range("AX22:AX32").Value
End With

With Sheets("OVERTIME")
    ' last used row in column A plus 1
    writerow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    .Range("A" & writerow) = WKend
    .Range("B" & writerow).Resize(, UBound(arr)).Value = Application.Transpose(arr)
End With

End Sub
 
Upvote 0
another option, assuming your OVERTIME sheet already exists
paste this into a standard module
VBA Code:
Sub bdt()
    Dim WKend As Date
    Dim arr As Variant
    Dim writerow As Long
   
With Sheets("ABACUS")
    WKend = .Range("C2").Value
    arr = .Range("AX22:AX32").Value
End With

With Sheets("OVERTIME")
    ' last used row in column A plus 1
    writerow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    .Range("A" & writerow) = WKend
    .Range("B" & writerow).Resize(, UBound(arr)).Value = Application.Transpose(arr)
End With

End Sub
No sparks, this code works lovely except when pasting the date, formatted to **.**.** it pastes it into column A in time format. If I format the entire column to date **.**.** it returns a date of 0.1.00 every time.
Is there a way to correct this
Thanks
 
Upvote 0
Click in the cell C2 and show us a picture that includes the Cell AND the formula bar.
Also change the date format in C2 and tell us if the display changes to the new format or stays the same.
 
Upvote 0
Cell C2 is formatted to date **.*.**
19.10.24
and the destination column is formatted the same. but returns in the formula bar

00/01/1900 17:10:24
And shows below in the A column
0.1.00

any thoughts gratefully received, thanks
 
Upvote 0
Because WKend is declared as Date
C2 must be a real date or you would be getting Run Time error 13: Type mismatch
so I'm thinking it must be the formatting of column A

try changing the formatting of the next cells in column A to General
and try this, it should be 45584 decimal something you see in the cell
which you can then format to display the date as whatever you want.

VBA Code:
Sub bdt()
    Dim WKend
    Dim arr As Variant
    Dim writerow As Long
    
With Sheets("ABACUS")
    WKend = .Range("C2").Value2
    arr = .Range("AX22:AX32").Value
End With

With Sheets("OVERTIME")
    ' last used row in column A plus 1
    writerow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    .Range("A" & writerow) = WKend
    .Range("B" & writerow).Resize(, UBound(arr)).Value = Application.Transpose(arr)
End With

End Sub

let me know how that goes
 
Upvote 0
Solution
Because WKend is declared as Date
C2 must be a real date or you would be getting Run Time error 13: Type mismatch
so I'm thinking it must be the formatting of column A

try changing the formatting of the next cells in column A to General
and try this, it should be 45584 decimal something you see in the cell
which you can then format to display the date as whatever you want.

VBA Code:
Sub bdt()
    Dim WKend
    Dim arr As Variant
    Dim writerow As Long
   
With Sheets("ABACUS")
    WKend = .Range("C2").Value2
    arr = .Range("AX22:AX32").Value
End With

With Sheets("OVERTIME")
    ' last used row in column A plus 1
    writerow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    .Range("A" & writerow) = WKend
    .Range("B" & writerow).Resize(, UBound(arr)).Value = Application.Transpose(arr)
End With

End Sub

let me know how that goes
NoSparks you are a hero. Formatted column A in OVERTIME to general and it now returns the date format I was after.
Many thanks
 
Upvote 0

Forum statistics

Threads
1,222,562
Messages
6,166,804
Members
452,073
Latest member
akinch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top