I want to assign a macro that copy several values from one sheet to another, and so far I've come to this:
Sub botaoconfirmar_click()
Range("C6").Select
Selection.Copy
Worksheets("Historico").Select
lMaxRows = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("C59").Select
Selection.Copy
Worksheets("Historico").Select
lMaxRows = Cells(Rows.Count, "C").End(xlUp).Row
Range("C" & lMaxRows + 1).Select
Selection.PasteSpecial , Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
End Sub
The problem is that when I click the button with this macro it only performs the first action. If I change the order, it still performs only the first action (former #2).
What am I doing wrong?
Specifically to your problem, you're not qualifying the Sheet associated with the Copy statement. So, when you copy Range("C59"), your code is still on Sheets("Historico"). Change both of your initial selection statements to, for example, Sheets("Data").Range("C6").Select.
On a side note, the code is not running optimally. There is no need to select each cell and sheet that you intend to work with. The best way to write the code would be (again, assuming the data is in a sheet named "Data"):
Sub botaoconfirmar_click()
Dim wsData As Worksheet
Dim wsHistorico As Worksheet
Dim lMaxRows As Long
Set wsData = Worksheets("Data")
Set wsHistorico = Worksheets("Historico")
lMaxRows = wsHistorico.Cells(Rows.Count, "B").End(xlUp).Row
wsHistorico.Range("B" & lMaxRows + 1).Value = wsData.Range("C6")
lMaxRows = wsHistorico.Cells(Rows.Count, "C").End(xlUp).Row
wsHistorico.Range("C" & lMaxRows + 1).Value = wsData.Range("C59")
End Sub
Related
So, I'm very new to VBA and I am having a difficult time finding answers to what I believe should be a fairly straightforward question.
I have a workbook that has 2 sheets, which we will call Sheet1 and Sheet2.
I want to copy data from columns B, D and E on Sheet1 to the first available row in columns A, B and C on Sheet 2, with the following changes:
Sheet1 Sheet2
Col E Col A
Col D Col B
Col B Col C
But I only want the data to be copied if the cell value of each row in Sheet1 Column I is "Y".
I don't have any code for this yet.
UPDATE:
After taking advice from a response, I ran a Macro record and got this:
Sub VBlk()
'
' VBlk Macro
' V Block Copy
'
'
Range("B2").Select
Selection.Copy
Sheets("Sheet2").Select
Range("C3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("B3").Select
Sheets("Sheet1").Select
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Range("E2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Try the code below (Hope it helps) :
Sub test()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("sheet1")
Set ws2 = Sheets("sheet2")
'get the Last non empty row in sheet1 based on Column B
lastrow1 = ws1.Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastrow1
'get the Last non empty row in sheet2 based on Column A
lastrow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
If ws1.Range("I" & i).Value = "Y" Then
ws2.Range("A" & lastrow2 + 1).Value = ws1.Range("E" & i)
ws2.Range("B" & lastrow2 + 1).Value = ws1.Range("D" & i)
ws2.Range("C" & lastrow2 + 1).Value = ws1.Range("B" & i)
End If
Next i
End Sub
I'm trying to copy multiple cells from one worksheet to another. I'm getting error message wrong number of arguments or invalid property assignment.
Range("D10:D12,D15,D22,D25,D32:D33,D38:D42,D47:D50,D53,D55,D57,D63").Select
Range("G3").Select
Selection.Copy
Sheets("Sheet3").Select
'Range("I4").End(xlUp).Select
lMaxRows = Cells(Rows.Count, "I", "AD").End(xlUp).Row
Range("I", "AD" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("Sheet1").Select
Range("I4", "AD").Select
Hoping for your help.
i tried using union but cannot come up with a solution. Here is the codes I have now
Dim r1 As Range, r2 As Range, multiRange As Range
Set r1 = Sheets("Sheet1").Range("D10:D12,D15,D22,D25,D32:D33,D38:D42,D47:D50,D53,D55,D57,D63")
Set r2 = Sheets("Sheet1").Range("G3")
Set multiRange = Union(r1, r2)
Application.Union(r1, r2).Select
Selection.Copy
Sheets("Sheet3").Select
'Range("I4").End(xlUp).Select
lMaxRows = Cells(Rows.Count, "I").End(xlUp).Row
Range("I" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("Sheet1").Select
Range("I4").Select
THE error message I'm know getting is That command cannot be used on multiple selections.
The Highlighted code is SELECTION.COPY
The code below assumes that Sheets("Sheet1") has sheet CodeName "Sheet1", and similar for Sheet3. (In general you should use sheet CodeName in your code).
Dim SourceArea As Range
Dim TargetArea As Range
Dim CopyRange As Range
Set CopyRange = Sheet1.Range("D10:D12,D15,D22,D25,D32:D33,D38:D42,D47:D50,D53,D55,D57,D63")
For Each SourceArea In CopyRange.Areas
Set TargetArea = Sheet3.Range(SourceArea.Address)
TargetArea.Value = SourceArea.Value
Next
Edit: The above will paste in Sheet3 in exactly the same locations as the ranges in Sheet1. If you want to paste to a different location, use Offset. For example if you want the top left cell in the target to be I20, then:
Set TargetArea = Sheet3.Range(SourceArea.Address).Offset(10,5)
I have seen some examples but they have been using .Select and .Activate. I am trying to learn how to not use those anymore because everyone says you should try to stay away from them.
I want to take a row, then copy it to the first blank row on the other sheet. I was close but it just isn't working.
UsdRws = Range("A" & Rows.Count).End(xlUp).Row
With Sheets("Totals by Department")
.Range("A1:Z" & UsdRws).autofilter Field:=1, Criteria1:="1450"
.Range("A2:Z" & UsdRws).SpecialCells(xlCellTypeVisible).EntireRow.COPY
End With
Set NextRow = Range("A" & Sheets(2).UsedRange.Rows.Count + 1)
Sheets(2).NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
The first part copies perfectly, I really just need help pasting it over on the other sheet. I will also take other recommendations for cleaning the code up. Like I said I am trying to learn to write better. The second part is messy because I have been adding and editing it but now I am lost.
Your "NextRow" object is a Range object, but you are calling it as if it were a method or property of Sheets(2).
Try removing the Sheets(2). and just start with Next Row.
Set NextRow = Sheets(2).Range("A" & Sheets(2).UsedRange.Rows.Count + 1)
NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' UsdRws is equal the last used row on whichever sheet is active at the moment that this code runs
UsdRws = Range("A" & Rows.Count).End(xlUp).Row
' this code properly references ranges on a specific worksheet, regardless of which worksheet is active
With Sheets("Totals by Department")
.Range("A1:Z" & UsdRws).autofilter Field:=1, Criteria1:="1450"
.Range("A2:Z" & UsdRws).SpecialCells(xlCellTypeVisible).EntireRow.COPY
End With
' NextRow is reference to a cell on whichever sheet is active at the moment that this code runs
' but the row referenced is same as the first emply cell on Sheets(2)
Set NextRow = Range("A" & Sheets(2).UsedRange.Rows.Count + 1)
' NextRow is already a range .... so it should be NextRow.PasteSpecial ......
Sheets(2).NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
this may be what you want
With Sheets("Totals by Department")
UsdRws = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:Z" & UsdRws).autofilter Field:=1, Criteria1:="1450"
.Range("A2:Z" & UsdRws).SpecialCells(xlCellTypeVisible).EntireRow.COPY
End With
Set NextRow = Sheets(2).Range("A" & Sheets(2).UsedRange.Rows.Count + 1)
NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
I'm so close, but this isn't working quite yet.
What's wrong here?
Sub DUMMY_ITEMS()
'
' DUMMY_ITEMS Macro
Sheets("Operations").Select
Range("H2:V73").Select
Selection.Copy
Sheets("Raw Data").Select
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub '
I guess you mean you got an error trying to use the PasteSpecial line.
As a recommendation, try to avoid using Select, Selection, and ActiveSheet, instead use fully qualified Worksheets and Ranges.
"Reduced" Code
Sub DUMMY_ITEMS()
'
' DUMMY_ITEMS Macro
Dim LastRow As Long
Sheets("Operations").Range("H2:V73").Copy
With Sheets("Raw Data")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A" & LastRow + 1).PasteSpecial xlPasteValues
End With
End Sub
I am working on an excel sheet and need to move the same range over and over again to the column "P" + 2
So the next range would be "C15:G15" to "P14". I'm looking for a slimmer solution than to repeat this code and change the ranges for hundreds of times..
ActiveWindow.SmallScroll Down:=-3
Range("C13:G13").Copy
Application.CutCopyMode = False
Selection.Copy
Range("P12").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
This quick snippet should walk down every second row in column C starting at row 13 till the last populated cell in column C.
Sub move_CG_to_PT()
Dim rw As Long
With Worksheets("Sheet4") '<~~set this worksheet reference properly!
For rw = 13 To .Cells(.Rows.Count, "C").End(xlUp).Row Step 2
.Cells(rw - 1, "P").Resize(1, 5) = _
.Cells(rw, "C").Resize(1, 5).Value
Next rw
End With
End Sub
This only transfers the values. If the formatting and/or theme is critical then those could be adjusted for with the following.
Sub move_CG_to_PT_w_Formatting()
Dim rw As Long
With Worksheets("Sheet4") '<~~set this worksheet reference properly!
For rw = 13 To .Cells(.Rows.Count, "C").End(xlUp).Row Step 2
.Cells(rw, "C").Resize(1, 5).Copy _
Destination:=.Cells(rw - 1, "P")
Next rw
End With
End Sub