My VBA Macro code is supposed to insert Column C, add the heading and then type a formula into cell C2 =text(B2," mm/dd/yyyy ") I then want to copy and paste that formula to the bottom of the data. The problem is that the range changes with each spreadsheet. I do I code it to look at column B and go to the end of the data and copy the formula up to Row 2?
Just learning to do Macros and need to modify the script.
Here is my code:
Sub TEST1()
'
' TEST1 Macro
'
'
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Range("C1").Select
ActiveCell.FormulaR1C1 = "Revised Billed Date"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],"" mm/dd/yyyy "")"
Range("C2").Select
Selection.Copy
Range("B2").Select
Selection.End(xlDown).Select
Range("C8").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Range("C7").Select
Selection.End(xlUp).Select
Range("C2").Select
End Sub
I believe that this will solve it for you. I will recommend you to learn to use With and End with to better organize and have a cleaner code. This will allow you to use .Range and .Paste .
All I did was pivot around the selected cells using .Offset(), place my self at the last row with content of column B, and create a range in column C in which I will lastly paste the content of the copied cell.
Sub TEST1()
With ActiveSheet
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
.Range("C1").Select
Selection.FormulaR1C1 = "Revised Billed Date"
.Range("C2").Select
Selection.FormulaR1C1 = "=TEXT(RC[-1],"" mm/dd/yyyy "")"
Selection.Copy
''''''Find last cell with content in column B and go back to column C
Selection.Offset(0, -1).End(xlDown).Offset(0, 1).Select
'''''Create a range from the end of column C up until below C2 cell
.Range(Selection, Selection.End(xlUp).Offset(1, 0)).Select
.Paste
End With
End Sub
Related
Please help me with a macro for copying and pasting text from Input sheet (worksheet1) to the Reports sheet (worksheet 2) within the same excel file.
Input Sheet Format:
Name- cell J5
Date- cell J6
Start Time- cell J7
End Time- cell J8
Downtime- cell J9
I need a macro for copying J5:J9 and paste it in A2:E2, using transpose function.
Also, the sheet is shared with multiple users so it needs to be pasted in the last available row in the Reports sheet (worksheet 2).
Presently, I am using the following macro code:
Sub Report()
Sheets("Input").Select
Range("J5:J9").Select
Selection.Copy
Sheets("Reports").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Sheets("Input").Select
ActiveWindow.SmallScroll Down:=-5
Sheets("Input").Select
Range("J5:J9").Select
Selection.ClearContents
End Sub
I am getting error at "ActiveCell.Offset(1, 0).Select" while running the macro.
Please advise any changes to the code which can help me fixing the issue.
Thanks
You can rewrite everything you're doing in a few lines:
Sub Report()
' Copy the range from J5:J9 on INPUT worksheet
ThisWorkbook.Worksheets("Input").Range("J5:J9").Copy
With ThisWorkbook.Worksheets("Reports")
' Find the LAST ROW in COLUMN A
Dim lROW As Long: lROW = .Cells(.Rows.Count, 1).End(xlUp).Row
' Paste in cell A-lROW with transpose
.Range("A" & lROW).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, Transpose:=True
End With
' Clear entry in original INPUT sheet
ThisWorkbook.Worksheets("Input").Range("J5:J9").ClearContents
End Sub
I'm not sure why you're getting an error for offsetting by one row, it works for me - but it's probably to do with your method of "selecting" the last row
I'm trying to copy data into new workbook and validate the data by removing duplicates keeping one cell value as the source.
All I wanted is the count of XD in the worksheet provided there are no duplicate Record locator.
Also I wanted the count to be in a msgbox. Can someone help ?
Sub openworkbook()
Workbooks.Open ("C:\Users\kjayachandiran\Desktop\ACUITY CF 1204-1210.xls")
Worksheets(2).Activate
Cells.Select
Selection.Copy
Workbooks.Open ("C:\Users\kjayachandiran\Desktop\New-Manjunath.xlsx")
Worksheets(1).Activate
Range("A1").Select
ActiveSheet.Paste
Workbooks(3).Save
Workbooks(2).Close
ActiveWorkbook.Activate
Worksheets(1).Activate
Cells(1, 1).Select
Range("A366655").Value = Application.WorksheetFunction.CountIf(Columns(9), "=" & "XD")
End Sub
I want to copy data from sheet "summary" row A44 (fixed row with dynamic data with formula) to sheet18 (row A3), A1 and A2 are header; i have below vba code and manage to do so. I would like to copy and paste the data as value (like Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False), so that the data will convert to absolute number, anyone how to edit the code?
Sub COPY_SUMMARY2COPYDATA()
Set des = Sheet18.Range("a1")
With Worksheets("SUMMARY")
.Rows(Range("A44").Row).Copy
des.Range("A3").Insert Shift:=xlUp
End With
Application.CutCopyMode = False
End Sub
Please try this:
Sub COPY_SUMMARY2COPYDATA()
Dim LastRow As Long
LastRow = Sheet18.Cells(Rows.Count,1).End(XlUp).Row + 1
Sheets("SUMMARY").Rows("44").Copy
Sheet18.Rows(LastRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Hope this help
To find the first blank cell in column A look from the bottom up and offset down one row.
Use a direct value transfer with .Value2 to pass over 'the data will convert to absolute number'. This will discard regional currency and date conventions as well as formulas in favor of the raw underlying value.
with Worksheets("SUMMARY")
with intersect(.usedrange, .rows(44).cells)
Sheet18.cells(.rows.count, "A").end(xlup).offset(1, 0).resize(.rows.count, .columns.count) = .value2
end with
end with
I am compiling data from multiple worksheets to place on one consolidated sheet. The first sheet is easy because I can paste the entire sheet however after that it becomes tricky as I only need from A5-H### as I no longer need the header. I then need to paste this information at the bottom of the previous paste. I am getting a Range of Object "_Global" failed for the 'Range(lastRow).Select'. The issue is when I look at it in the debugger it is coming up with the correct row number. What am I doing wrong?
Sheets(1).Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Consolidation Sheet").Select
Range("A1").Select
ActiveSheet.Paste
Sheets(2).Select
Range("A5:A925").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Consolidation Sheet").Select
Range("A5").Select
lastRow = ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Row + 1
Range(lastRow).Select
ActiveSheet.Paste
You need your Range statement in the following form (for example):
Range("A1").Select
So since you've got the row number (lastRow), you just need to add the column reference too:
Range("E" & lastRow).Select
Extra info
Avoid using Select by referring directly to the worksheets/ ranges you want to deal with
Sheets(1).Cells.Copy Destination:=Sheets("Consolidation Sheet").Range("A1")
With Sheets(2)
.Range(.Range("A5:A925"),.Range("A5:A925").End(xlToRight)).Copy
End With
With Sheets("Consolidation Sheet")
.Cells(.Rows.Count, "E").End(xlUp).Offset(1,0).PasteSpecial
End With
I have googled and tried various different things to get this to work but still can't find a solution.
I am still trying to learn about macros and VB so any help would be appreciated.
Basically, what I am after is a cell value on one worksheet to be the amount of rows on a second worksheet.
Picture showing what I am after
As described in the picture, the value of the source cell (number of payments) varies depending on the term/frequency/value of agreement.
I would then like that number of rows be allocated in the next worksheet, with sequential numbering.
This is what I have managed to fumble about with so far....
Sub ExtendByValue()
'
' ExtendByValue Macro
' Extends the rows by the number of repayments
'
'
Sheets("Agreement Tems").Select
Range("C8").Select
Selection.Copy
Sheets("Payments").Select
Range("M1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("8:8").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Any help with this is appreciated.
Rows("8:" & (sheets("agreementterms").range("c8").Value + 8)).select
Selection.insert shift:=xldown
incase you have value issues as specified in comments use the below.
Rows("8:" & (sheets("agreementterms").range("c8").text + 8)).select
Selection.insert shift:=xldown
try this
Option Explicit
Sub ExtendByValue()
'
' ExtendByValue Macro
' Extends the rows by the number of repayments
'
Dim nRows As Long
nRows = CLng(Sheets("Agreement Tems").Range("C8")) 'get the integer part of cell "C8" value in "Agreement Tems" sheet
With Sheets("Payments")
.Range("B8").Resize(nRows - 1).EntireRow.Insert 'insert nRows-1 below cell "B8" of "Payments" sheet, so as to have nRows with this latter included
With .Range("B8").Resize(nRows) 'of these nRows ...
.FormulaR1C1 = "=row()- row(R7)" ' ... fill column "B" with a formula returning integer form 1 to nRows ...
.Value = .Value ' ... and finally get rid of formulas and leave only values
End With
End With
End Sub