I have a simple problem I cannot figure out.
Right now I have a macro set up that moves 1 selected row, to the bottom of another worksheet and changes it's color based on good/bad outcome.
I need the cell values and color formatting to be copy/pasted to the other worksheet.
Here's the code I have now
Sub CloseCasePaid()
'
' CloseCasePaid Macro
'
With Selection.Font
.Color = -1003520
.TintAndShade = 0
End With
Selection.EntireRow.Copy Sheets("Closed Files").Cells(Rows.Count, 1).End(xlUp).Offset(1)
Selection.EntireRow.Delete xlShiftUp
End Sub
Thank you for any assistance!
You need to use paste special - values. Here's the syntax:
Range("A1").Copy
Range("B2").PasteSpecial (xlPasteValues)
I would do this before deleting the row, since deleting the row will knock it out of the clipboard.
Related
So I have an Excel workbook and I have a button to add new row at the top. The below starts at Row 4 (First data row is row 5). My actual sheet goes out to column L, but the below is just a few of the columns. The header row is highlighted grey. I had trouble with other examples of doing what I needed it to do, so I recorded a macro. Basically it copies the top data row, inserts it, removes highlight, and clears data. This way the lists I have are preserved as well as the cell borders. Is there a more elegant way to do this?
Also, is there a way I can have the Add New Row button generate the UID for me? Note I may sort or filter the data, so I can't just take what's in A5 and add 1.
UID
Requirement
Source
Category
0002
...
...
[list]
0001
...
...
[list]
Private Sub CommandButton1_Click()
Rows("5:5").Select
Selection.Copy
Selection.Insert Shift:=xlDown
Rows("5:5").Select
Application.CutCopyMode = False
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("B5:L5").Select
Selection.ClearContents
Range("A1").Select
End Sub
Update 1:
New code based on everyone's recommendations. Still would be ideal if I could insert UID when I manually insert a row too.
Private Sub CommandButton1_Click()
Rows(5).Insert xlShiftDown, xlFormatFromRightOrBelow
With Worksheets("Requirements")
NextUID = WorksheetFunction.Max(.Range("A:A")) + 1
End With
Range("A5").Value = NextUID
End Sub
Welcome Scott, get the maximum value of the first column and increment it:
With Worksheets("Sheet1")
NextUID= WorksheetFunction.Max(.Range("A:A"))+1
End With
I have a macro that is supposed to copy the format of a row and insert a new row with the same format.
Here is the macro code:
Sub Insertion_ligne_verrouillée()
'
' Insertion_ligne_verrouillée Macro
ActiveSheet.Unprotect
ActiveCell.Offset(-1, 0).EntireRow.Copy
Rows(ActiveCell.Row).Insert Shift:=xlDown
On Error Resume Next
Rows(ActiveCell.Row).SpecialCells(xlCellTypeConstants).ClearContents
ActiveSheet.Unprotect
'Application.CutCopyMode=False
End Sub
Now i am not the one that wrote the macro and honestly my VBA is quite rusty (also not that good in VBA either). The problem i am having is the user is using the macro by selecting a row and using ctrl+L.
It does copy and insert a row with the right format, however some rows afterward seem empty (all blank and no row number) so you have to select the row > right click > display, for it to display properly
Not sure what to look for
The following code makes a new row below the row you want to copy then copies the format of the row and paste into the new row.
Sub Insertion_ligne_verrouillée()
'Make a new row below active cell
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown,
CopyOrigin:=xlFormatFromRightOrAbove
'Copy the active row
ActiveCell.EntireRow.Copy
'paste format into new row
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub
My task is to create a searchable database within Excel with an entry form. I need a macro to take the data from the entry form move to the database sheet offset the active cell down 1 row and copy the values only(not the formatting)
Every time I try to run the macro I get a run-time error in the code. I have no experience with VB or VBA; please tell me what is wrong with this.
Sheets("Database").Select 'Navigates to Database worksheet
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If 'Clears filters
Sheets("Entry Form").Select 'Navigates back to Entry Form worksheet
Range("E10:L10").Select ' select date, period, and data
Selection.Copy
Sheets("datatable").Select ' navigate to datatable tab
Range("A1").Select
Selection.End(xlDown).Select ' ctrl-down to last occupied row,
ActiveCell.Offset(1, 0).Select ' then one more to first blank row
Selection.PasteSpecial Paste:=xlPasteValues
'Pastes data as values only into the Database worksheet
Sheets("Entry Form").Select 'Navigates to Entry Form worksheet
Application.CutCopyMode = False 'clears copy data from clipboard
Range("E10, L10").Select
Selection.ClearContents 'Clears data from drop down selections
Range("E10").Select 'Returns selection back to Date entry box
It goes the very bottom of the next page and gives a 1004 error.
You need more than just a column label in A1 if you are going to use xlDown. There has to be at least one more value in column A or you will traverse to the bottom of the worksheet. It is usually better to look from the bottom of the worksheet upwards and then offset one row down.
With Sheets("Database") 'Primarily use Database worksheet
If .FilterMode Then .ShowAllData
With .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) 'look from bottom up then down 1 row
'direct value transfer is faster than Copy, Paste Special, Values
.Cells.Resize(1, 8) = Sheets("Entry Form").Range("E10:L10").Value
End With
End With
With Sheets("Entry Form") 'Primarily use Entry Form worksheet
.Range("E10:L10").ClearContents 'Clears data from drop down selections
.Range("E10").Select 'Returns selection back to Date entry box
End With
This makes use of the With ... End With statement to control which worksheet is receiving attention. See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.
I want to build a macro that inserts a row below the selected cell with the same format. This is the code I have so far:
Public Sub insertRowBelow()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
End Sub
The problem is: This code only partially transfers the format. It does use the same background color for the new row, but it does not use the borders/frames for the cells. How can I achieve that?
The easiest option is to make use of the Excel copy/paste.
Public Sub insertRowBelow()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub
Private Sub cmdInsertRow_Click()
Dim lRow As Long
Dim lRsp As Long
On Error Resume Next
lRow = Selection.Row()
lRsp = MsgBox("Insert New row above " & lRow & "?", _
vbQuestion + vbYesNo)
If lRsp <> vbYes Then Exit Sub
Rows(lRow).Select
Selection.Copy
Rows(lRow + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
'Paste formulas and conditional formatting in new row created
Rows(lRow).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone
End Sub
This is what I use. Tested and working,
Thanks,
When inserting a row, regardless of the CopyOrigin, Excel will only put vertical borders on the inserted cells if the borders above and below the insert position are the same.
I'm running into a similar (but rotated) situation with inserting columns, but Copy/Paste is too slow for my workbook (tens of thousands of rows, many columns, and complex formatting).
I've found three workarounds that don't require copying the formatting from the source row:
Ensure the vertical borders are the same weight, color, and pattern above and below the insert position so Excel will replicate them in your new row. (This is the "It hurts when I do this," "Stop doing that!" answer.)
Use conditional formatting to establish the border (with a Formula of "=TRUE"). The conditional formatting will be copied to the new row, so you still end up with a border.Caveats:
Conditional formatting borders are limited to the thin-weight lines.
Works best for sheets where borders are relatively consistent so you don't have to create a bunch of conditional formatting rules.
Set the border on the inserted row in VBA after inserting the row. Setting a border on a range is much faster than copying and pasting all of the formatting just to get a border (assuming you know ahead of time what the border should be or can sample it from the row above without losing performance).
well, using the Macro record, and doing it manually, I ended up with this code .. which seems to work .. (although it's not a one liner like yours ;)
lrow = Selection.Row()
Rows(lrow).Select
Selection.Copy
Rows(lrow + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Selection.ClearContents
(I put the ClearContents in there because you indicated you wanted format, and I'm assuming you didn't want the data ;) )
First thing I did was create a button that would copy certain cells using this code:
Worksheets("Sheet1").Range("A:A,B:B,D:D").Copy _
and it worked fine.
Second, I found the code that would copy all details in a row based on the criteria of one, in this case if there was an "A" in the "Location" column.
Private Sub ENTIREROW_Click()
'Sub copyrows()
Dim i As Range, Cell As Object
Set i = Range("D:D") 'Substitute with the range which includes your True/False values
For Each Cell In i
If IsEmpty(Cell) Then
Exit Sub
End If
If Cell.Value = "A" Then
Cell.ENTIREROW.Copy
Sheet2.Select 'Substitute with your sheet
ActiveSheet.Range("A65536").End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.Paste
End If
Next
End Sub
My question is, how do I copy all information in the specified columns (A,B,D) where there is an "A" in "Location" in one button.
Furthermore, this is my example data, the sheet I will actually use this on has 34 columns to copy. Is there a more efficient way of setting a range when you don't want an entire sequence, everything but the data in column C?
Thanks in advance and apologies for my explanation skills.
One way maybe to:
filter your source
hide column C
copy the result using .PasteSpecial xlPasteValues into the destination
Unhide column C on the source sheet
remove the autofilter
Using xlPasteValues only pastes the visible cells from the source - so no column C
The code then looks like this: .
Sub CopyRows()
With Sheets(1).Range([A2], [A2].SpecialCells(xlLastCell))
[A1].AutoFilter
.AutoFilter Field:=4, Criteria1:="A"
[C:C].EntireColumn.Hidden = True
.Copy
[C:C].EntireColumn.Hidden = False
End With
With Sheets(2)
If .Cells(Sheets(2).Rows.Count, 1).End(xlUp) = "" Then 'it's a clean sheet
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).PasteSpecial Paste:=xlPasteValues
Else
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End If
End With
Application.CutCopyMode = False
Sheet1.[A1].AutoFilter
End Sub