I have a form where you fill stuff in and a specific part of it should be copied to another sheet at the end of the list.
With Sheets("Sheet1")
If Application.WorksheetFunction.CountA(.Columns(2)) <> 0 Then
lastrow = .Cells(rows.Count, "B").End(xlUp).Row
Else
lastrow = 1
End If
.Cells(lastrow + 1, "B") = "my new value"
End With
I have this code to find the last row and paste/write "my new value" in it.
But i need that it pastes more than just one cell. I just need that it selects that part where it writes "my new value" in. I should be able to do the rest
I'm now using the code below. But it still copies stuff from the sheet "Tabelle3" but it should copy the stuff from the sheet "Tabelle2"
Private Sub CommandButton1_Click()
Dim lastRow As Long
With Sheets("Tabelle3")
If Application.WorksheetFunction.CountA(.Columns(1)) <> 0 Then
lastRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1 '<~~ Add 1 here and not as you are doing
Else
lastRow = 1
End If
Sheets("Tabelle2").Select
Range("B85:S85").copy
Sheets("Tabelle3").Select
'~~> Paste special
.Range("C" & lastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
End Sub
You have to find the last empty row and then simply do a paste or pastespecial as shown below.
Sub Sample()
Dim lastRow As Long
With Sheets("Sheet1")
If Application.WorksheetFunction.CountA(.Columns(2)) <> 0 Then
lastRow = .Cells(Rows.Count, "B").End(xlUp).Row + 1 '<~~ Add 1 here and not as you are doing
Else
lastRow = 1
End If
Range("Z10:Z100").Copy
'~~> Paste special
.Range("B" & lastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
End Sub
The above code will copy the range "Z10:Z100" and do a pastespecial on the next available row in Col B. If you do not want to do a pastespecial and want to do a direct paste then see this
Sub Sample()
Dim lastRow As Long
With Sheets("Sheet1")
If Application.WorksheetFunction.CountA(.Columns(2)) <> 0 Then
lastRow = .Cells(Rows.Count, "B").End(xlUp).Row + 1 '<~~ Add 1 here and not as you are doing
Else
lastRow = 1
End If
Range("Z10:Z100").Copy .Range("B" & lastRow)
End With
End Sub
Related
This code worked once and then stopped. It runs with no action or errors.
I would like if column "a" of the "export" sheet has a yes to copy the cells from B to J to the next clear line in workbook MOSTEST sheet1 (named 11.2022).
Sub DateSave()
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = Worksheets("EXPORT").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If Cells(i, 1).Value = "YES" Then
Range(Cells(i, 2), Cells(i, 10)).Select
Selection.Copy
Workbooks.Open Filename:="F:\Orders\MOSTEST.xlsx"
Worksheets("11.2022").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub
If changed the "Worksheets("11.2022").Select" to sheet1 which I would prefer as I wouldn't have to change it every month.
You should try to avoid using select, see other post
I adjusted your code where needed, I'm still trying to figure out best practice (i.e. it would be better adding the cell ranges to a range variable and then pasting them in one go but I'm not quite there yet) when it comes to minimizing code so if others can do better, feel free :)
Sub DateSave()
Dim LastRow As Long, i As Long, erow As Long
Dim wsStr As String
Dim ws As Worksheet, wsC As Worksheet
Dim wb As Workbook, wbM As Workbook
LastRow = Worksheets("EXPORT").Range("A" & Rows.Count).End(xlUp).Row
Set wb = ActiveWorkbook
Set wsC = wb.Sheets("EXPORT")
Workbooks.Open Filename:="F:\Orders\MOSTEST.xlsx" 'Don't keep opening and saving/closing your workbook per copy, that would heavily increase runtime
Set wbM = Workbooks("MOSTEST.xlsx")
wsStr = Month(Date) & "." & Year(Date)
Set ws = wbM.Worksheets(wsStr) 'If your currentmonth will always be the first sheet then you can use wbM.Sheets(1)
erow = ws.Cells(Rows.Count, 1).End(xlUp).Row
wb.Activate
For i = 1 To LastRow
If wsC.Cells(i, 1).Value = "YES" Then
erow = erow + 1
wsC.Range(wsC.Cells(i, 2), wsC.Cells(i, 10)).Copy 'avoid select
ws.Range("A" & erow).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next i
wbM.Save
wbM.Close
Application.CutCopyMode = False
End Sub
If you have questions, feel free to ask!
I am trying to copy and paste data from horizontal to vertical from sheet1 to sheet3 in a lots of 200.
Say I have a list of 600 tickers. The code will copy the first 200 from sheet1 cells ("C6 till GT7") and paste it vertically in sheet3 cell A2.
I need the next lot of 200 appended in sheet3 after row 201.
My code is pasting only the last 200 in sheet 3.
Sub getbulkprices()
Application.ScreenUpdating = False
Dim wb As Workbook, ws, ws1 As Worksheet
Dim r, iLastRow As Long, plr as long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Set ws1 = wb.Sheets("Sheet2")
iLastRow = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
ThisWorkbook.Sheets("Sheet1").Range("A2:A500").ClearContents
ThisWorkbook.Sheets("Sheet3").Range("A2:B500000").ClearContents
For r = 2 To ws1.Range("A" & Rows.Count).End(xlUp).Row Step 200
ThisWorkbook.Sheets("Sheet1").Cells(2, 1).Resize(200).Value = _
ws1.Cells(r, 1).Resize(200).Value
ws.Range("C1").FormulaR1C1 = "=#RHistory(R2C1:R200C1,"".Timestamp;.Close"",""NBROWS:""&R2C2&"" INTERVAL:1D"",,""SORT:ASC TSREPEAT:NO CH:In;"",R[5]C)"
Application.Run "EikonRefreshWorksheet"
Application.Wait (Now + TimeValue("0:00:02"))
plr = ThisWorkbook.Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
ws.Range("D6:IK7").Copy
ThisWorkbook.Sheets("Sheet3").Range("A2:B" & plr + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Application.CutCopyMode = False
Application.StatusBar = r & " / " & iLastRow - 1
Next r
End Sub
Consider qualifying the Rows.Count to the that same worksheet as qualifier to .Cells in the plr assignment:
plr = ThisWorkbook.Sheets("Sheet3").Cells( _
ThisWorkbook.Sheets("Sheet3").Rows.Count, 1 _
).End(xlUp).Row
Even better situate the copy and paste inside a With block to avoid repetition of worksheet:
For r = 2 To ... Step 200
...
With ThisWorkbook.Sheets("Sheet3")
plr = .Cells(.Rows.Count, 1).End(xlUp).Row
ws.Range("D6:IK7").Copy
.Range(.Cells(plr + 1, 1), _
.Cells(plr + 200, 2) _
).PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=True
End With
...
Next r
Consider even WorksheetFunction.Transpose and avoid copy/paste:
With ThisWorkbook.Sheets("Sheet3")
plr = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(plr + 1, 1), _
.Cells(plr + 200, 2) _
) = WorksheetFunction.Transpose(ws.Range("D6:IK7"))
End With
Change the paste to
ThisWorkbook.Sheets("sheet3").Range("A" & plr + 1 & ":B" & plr + 201).PasteSpecial...
I am not sure sure what I am doing wrong in trying to copy a range from one worksheet to another. Trying to copy from "LeadSheet" to "HistoricalDataSheet". Leadsheet Data will always start with Row 5 and copy the full rows until the last row. Then paste into the row after the last row on "HistoricalDataSHeet" and format the first column for date:
Sub CopyToHistorical()
Dim a, LR As Integer
With Worksheets("LeadSheet")
LR = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
With Worksheets("LeadSheet")
**.Range("5:LR").Copy**
'this is where I am getting there error'
End With
With Worksheets("HistoricalLeadData")
a = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
Sheets("HistoricalLeadData").Range("A" & a + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks:=False, Transpose:=False
With Sheets("HistoricalLeadData")
.Range(.Range("A" & a + 1), .Range("A" & a + 1).End(xlDown)).NumberFormat = "m/d/yyyy"
End With
End Sub
I am trying to copy the same row of information from a sheet called "Report" (numbers will change), and paste the values into a sheet "Data" that has headers in the first row.
I tried piecing together some code from various questions.
Here is my code:
Sub Insert_Data()
'
' Insert_Data Macro
Sheets("Report").Range("B9:F9").Copy
Sheets("Data").Range("A1").PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub PSData_Transfer()
Sheets("Report").Range("B9:F9").Copy
Dim lastrow As Long
lastrow = Sheets("Data").Range("A65536").End(xlUp).Row
Sheets("Data").Activate
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
You may have to modify this a little bit to work with your code, but feel free to use mine that I'm using in my current worksheet and it works perfect!
Sub Insert_Data()
For R = LR To 2 Step -1 ' Change the 2 in "To 2" to the row just below your header,
' but typically row 2 is the second cell under header anyways
Call CopyTo(Worksheets(2).Range("B" & R & ":C" & R), Worksheets(1)Range("A:B"))
Next R
End Sub
Private Function CopyTo(rngSource As Range, rngDest As Range)
LR = rngDest.cells(Rows.Count, 1).End(xlUp).row
rngDest.cells(LR + 1, 1).value = rngSource.cells(1, 1).value
rngDest.cells(LR + 1, 2).value = rngSource.cells(1, 2).value
End Function
I don't like to use the copy method as it's slow and it likes to copy all the extra jargin, where as getting the value is much faster and it's retrieving ONLY the value
I'm using the below VBA code which is copying a range from Sheet1 and paste it in the same sheet. However i need to paste the data in the next available row of sheet2.
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Sheets("Sheet2").Activate
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Please help me out..
Try this:
Private Sub CommandButton1_Click()
Dim lastrow As Long
Dim rng1 As Range
Dim rng2 As Range
lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = Sheets("Sheet1").Range("A1:A5")
Set rng2 = Sheets("Sheet2").Range("A" & lastrow + 1)
rng1.Copy
rng2.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Your code is good but one line you need to change it, that is place Sheets("Sheet2").Activate line before lastrow = Range("A65536").End(xlUp).Row
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Sheets("Sheet2").Activate
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
First activate the sheet2 and then find last row
lastrow = Range("A65536").End(xlUp).Row