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
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 recording a macro and need some help. I'd like copy and paste the values from the column G of the "SalesData" worksheet into cells A2, A12, A22 etc of the "Results" worksheet until there's no more values in the column G.
VBA is pretty new to me, I've tried using Do/Until, but everything crashed. Could you please help me? Please see the code I've recorded below. Thank you!
Sub(x)
Sheets("SalesData").Select
Range("G2").Select
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A12").Select
Sheets("SalesData").Select
Range("G3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A22").Select
Sheets("SalesData").Select
Range("G4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A32").Select
Sheets("SalesData").Select
Range("G5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I prefer to find the last cell in the column first then use a For loop.
Since you are only doing the values we can avoid the clipboard and assign the values directly.
Since you paste is every 10 cells we can use a separate counter to move down 10 each loop.
Sub x()
Dim ws As Worksheet
Dim lst As Long
Dim i As Long, j As Long
'use variable to limit the number of times we type the same thing
Set ws = Worksheets("Results")
'First row of the output
j = 2
'using with and the "." in front of those items that belong to it also limits the typing.
With Worksheets("SalesData")
'Find the last row with values in Column G
lst = .Cells(.Rows.Count, 7).End(xlUp).Row
'Loop from the second row to the last row.
For i = 2 To lst
'Assign the value
ws.Cells(j, 1).Value = .Cells(i, 7).Value
'Move down 10 rows on the output
j = j + 10
Next i
End With
End Sub
here is the same thing but using range variables
Sub x()
Dim src As Range
Dim dst As Range
Set dst = Worksheets("Results").Range("a2") ' point to top cell of destination
With Worksheets("SalesData")
For Each src In Range(.Cells(2, "g"), .Cells(.Rows.Count, "g").End(xlUp)) ' loop through used cell range in column G
dst.Value = src.Value
Set dst = dst.Offset(10) ' move destination pointer down 10 rows
Next src
End With
End Sub
This is just for fun/practice for another way to do it:
Sub copyFromG()
Dim copyRng As Range, cel As Range
Dim salesWS As Worksheet, resultsWS As Worksheet
Set salesWS = Sheets("SalesData")
Set resultsWS = Sheets("Results")
Set copyRng = salesWS.Range("G2:G" & salesWS.Range("G2").End(xlDown).Row) ' assuming you have a header in G1
For Each cel In copyRng
resultsWS.Range("A" & 2 + 10 * copyRng.Rows(cel.Row).Row - 30).Value = cel.Value
Next cel
End Sub
Hi I'm busy with a VBA macro that copies data from one sheet to another, problem is whenever i paste the data to the other sheet, the conditional formatting falls off.It messes up with what i want to achieve. Isn't there a code I could use to keep conditional formatting. here is my code:
'In this example I am Copying the Data from Sheet1 (Source) to Sheet2
(Destination)
Sub sbCopyRangeToAnotherSheet()
'Method 1
Application.ScreenUpdating = False
'Set active sheet as current sheet
temp = ActiveSheet.Index
'Clear contents in sheet 1
Sheets("Sheet1").Select
Range("B22").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'Clear Specials in Sheet 1
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'Return to current sheet and copy required contents
Sheets(temp).Select
Range("D51").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Paste data in sheet 1
Worksheets("Sheet1").Activate
k = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Range("B22").Select ' kindly change the code to suit your paste location
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy specials over to sheet1
Sheets(temp).Select
Range("i36").Select
p = Range(Selection, Selection.End(xlDown)).Count
j = 0
For k = 1 To p
Sheets(temp).Select
t = Range("i36").Offset(k - 1, 0).Value
s = Range("j36").Offset(k - 1, 0).Value
If t = True Then
Sheets("Sheet1").Select
j = j + 1
Range("b13").Offset(j - 1, 0).Value = s
Else: End If
Next k
'Delete Empty Rows In UPL
Dim iRow As Long, lastRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1") 'qualify your sheet
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row 'find last used row
For iRow = lastRow To 1 Step -1 'run from last used row backwards to row 1
If ws.Cells(iRow, 3).Text = "#N/A" Or _
ws.Cells(iRow, 4).Text = "#N/A" Then
ws.Rows(iRow).Delete
End If
Next iRow
' Paste Unit Into UPL
Sheets(temp).Select
temp = Sheets(temp).Range("d35").Value
model = Range("D26").Value
Sheets("Sheet1").Select
Range("B11").Value = temp & " " & model
End Sub
Please Assist
so I recommend to replace this:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
with this:
Selection.PasteSpecial Paste:=xlPasteAllMergingConditionalFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False 'so that Excel will not be in the copy mode
Sub FIXPAY()
Dim LastCol As Long, LastRow As Long, s2 As Worksheet
Set s2 = Sheets("Analysis")
s2.Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Copy
s2.Range("A" & Rows.Count).End(xlUp).Offset(2, 0).PasteSpecial xlPasteAll
'*Copy the table header from 1st table and paste it to the second empty row.*
Range("A3:C3").Select
Range(Selection, Selection.End(xlDown)).Copy
s2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'*Copy from A3:C3 to the last filled row and paste it to the row right below
the heading.*
s2.Range("D" & Rows.Count).End(xlUp).Offset(1, 0).FormulaR1C1 = _
"=INDIRECT(""'""&RC1&""'!""&ADDRESS(ROW(R73C[-2]),COLUMN(R73C[-2])))"
'*Put the above formula in the D Column right below the heading*
The problem lies here.How can I replace the below line with dynamic range such that it will auto-fill the data in the D Column
s2.Range("D" & Rows.Count).End(xlUp).Offset(1, 0).FormulaR1C1 = _
"=INDIRECT(""'""&RC1&""'!""&ADDRESS(ROW(R73C[-2]),COLUMN(R73C[-2])))"
s2.Range("D15").AutoFill destination:=Range("D15:D" & Range("A15").End(xlDown).Row)
LastCol = Cells(14, Columns.Count).End(xlToLeft).Column
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("D15", Cells(LastRow, LastCol)).FormulaR1C1 = Range("D15").FormulaR1C1
End Sub
Can someone please help me making the code fully dynamic? Please note that I have referenced as row 15 here because my heading of the second table is in row 14 and hence the formula from D15 is to be copied to the last filled row and last filled column.
I'm really new to programming in VBA and having a problem with this code I'm trying to write. I am wanting the code to figure out the first row in column A that is unused then copy and paste data from a different part of the sheet into that row.
Sub CopyandPaste()
Dim RowLast As Long
RowLast = ThisWorkbook.Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
Set NewRange = ThisWorkbook.Worksheets("Sheet2").Cells(RowLast, 1)
ThisWorkbook.Worksheets("Sheet1").Cells(8, "B").Select
Selection.Copy
Range("NewRange").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Any help would be extremely helpful.
Try this code :
Sub CopyandPaste()
Dim RowLast As Long
ThisWorkbook.Activate
With Worksheets("Sheet2")
RowLast = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
Sheets("Sheet1").Cells(8, "B").Copy Sheets("Sheet2").Cells(RowLast, 1)
End With
End Sub
I have added comments into the code explaining changes I made.
Sub CopyandPaste()
Dim RowLast As Long
Dim newRange As Range
'this works easier if I understand your intent right
'I generally use some large row number with Excel 2010
'You may ahve to make this smaller if you are in 03
RowLast = Sheets("Sheet2").Range("B99999").End(xlUp) + 1
'if you KNOW you have continuous data in this column (no spaces)
RowLast = Sheets("Sheet2").Range("B1").End(xldown) + 1
'this is slightly better way to do this
Set newRange = ThisWorkbook.Worksheets("Sheet2").Range("A" & RowLast)
'don't do this
'ThisWorkbook.Worksheets("Sheet1").Cells(8, "B").Select
'Selection.Copy
'do this instead
Sheets("Sheet1").Range("B8").Copy
newRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'you were attempting to use a variable name (newrange) as a
'name of a named range in the Excel sheet
'use the variable range itself (as above)
End Sub