EXCEL VBA - Copy and Paste values within each excel workbook [duplicate] - excel

The following code loops within a sheet.
I want to loop through all sheets in my workbook except for one sheet labeled "Summary".
For i = 2 To LR
If Abs(Range("J" & i)) > 0 Then
Range("A" & i & ":J" & i).Copy
Sheets("Sheet1").Range("A" & k).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
k = k + 1
Else
End If
Next i

Please try something like this
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
'Your code here
End If
Next

Related

Pastespecial pasting only the first cell value across entire range

I'm trying to paste the formula results from one column to the next. I need to perform this through a macro without using a loop as it slows down the tool due to large number of records. The following methods are not working -
a) range("G5:G10").value = range("H5:H10").value
b) Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
The issue is that excel copies the formula result of the first cell ("G5") to all the cells at the destination ("H5:H10"). The formula in the source is an array formula. Could anyone please share how to resolve the issue.
Edit :
Sub paste()
'
' paste Macro
'
'
Dim ColName As String, sheetname1 As String, lookup_col As String, lastrow_range As Long
ColName = "A"
sheetname1 = "Sheet1"
lookup_col = "C"
lastrow_range = 8
Worksheets("Sheet1").Range("F5").Select
ActiveCell.FormulaArray = _
"=IF(ISERROR(MATCH(TRUE,EXACT($" & ColName & ActiveCell.Row & "," & sheetname1 & "!$" & lookup_col & "$5:$" & lookup_col & "$" & lastrow_range & "),0)) , ""-" & ColName & """&"" is Invalid. This should match with any one of the values in column "" &""" & lookup_col & """&"" of "" & """ & sheetname1 & """&"". Please refer ""&""" & sheet_name & """ ,""It Matches"")"
Worksheets("Sheet1").Range("F5").Select
Selection.AutoFill Destination:=Range("F5:F8")
Range("F5:F8").Select
Selection.Copy
Range("G5").Select
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Copy and paste special values of range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
Copy and paste range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Or copy and paste the entire column
Range("H:H").Copy
Range("G:G").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Thanks for the input.
I found that the calculation method had to be changed to 'Automatic' from 'Manual' :
Application.Calculation = xlAutomatic

cut copy paste macro across 2 sheets with dynamic range

I have a sheet with a range of A12:N112, Column A is my trigger column (1 or ) based on changing criteria). The first bit of my macro which works sorts this range to all the rows with a 1 are at the top of the range. It then opens the destination sheet as well.
The next bit of code below, needs to copy cells B:L for each row with a 1 in column A and paste that into the first empty row in the destination sheet starting at column D. This then generates a number which the then copied and pasted back into the first sheet in column M of that specific row. This then needs to loop until all of the rows with a 1 in column A have been processed.
Can anyone help, here is my code, which runs but nothing is copied or pasted.
Dim lr As Long lr = Sheets("Data Entry").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step 1
If Range("AB" & r).Value = "1" Then
Rows(r).Copy.Range ("A" & lr2 + 1)
Windows("Serialisation Log.xlsx").Activate
Sheets("SNo Log").Select
Range("D" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("Serialisation Log.xlsx").Activate
Sheets("SNo Log").Select
Range("A" & Rows.Count).End(xlUp).Offset(-1).Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Range("A" & Rows.Count).End(xlUp).Offset(0).Select
Selection.Copy
Windows("Serialisation Generator rev 1.xlsm").Activate
Worksheets("Data Entry").Select
Range("N").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
If Range("AB" & r).Value = "0" Then
Range("I4").Select
ActiveCell.FormulaR1C1 = "Serial No. Issue complete for this OA"
End If
Range("F5").Select
Next r
Any help will be greatly appreciated.

Compiler error 'Next without For' - can't understand why

This is a simple For ... Next so why am I getting the error, is it related to the function somehow?
The macro I supposed to find a specific worksheet within a large workbook, get some data and copy it to a separate workbook named after the worksheet. Most of this came from mw recording a macro with changes were necessary.
Dim wbThisWB As Workbook
Dim LastRow As Long
Dim WSName As String
Dim lRow As Long
Workbooks.Open Filename:= _
"\\Shenetapp01\itt viability and intervention\Assurance Work AY 17-18\AGR\Test\16-17 EY Trainees test.xls"
LastRow = wbThisWB.Worksheets("Sheet1").Cells(Row.Count, 1).End(xlUp).Row
For I = 1 To LastRow
WSName = wbThisWB.Worksheets("Sheets1").Cells(I, 1)
If sheetExists(WSName, wbThisWB) Then
MsgBox "Sheet found:" & WSName
lRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("C2", "M" & lRow).Copy
Workbooks.Open Filename:="\\Shenetapp01\itt viability and intervention\Assurance Work AY 17-18\AGR\Test\" & WSName & " 17-18 AGR.xlsx"
Sheets("EY 17-18 Starters").Select
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close
Next I
End Sub
Function sheetExists(sheetToFinad As String, wbThisWB As Workbook) As Boolean
sheetExists = False
For Each Sheet In wbThisWB.Worksheets
If sheetToFind = Sheet.Name Then
sheetExists = True
Exit Function
End If
Next Sheet
End Function

How to loop through sheets in a workbook with an exception?

The following code loops within a sheet.
I want to loop through all sheets in my workbook except for one sheet labeled "Summary".
For i = 2 To LR
If Abs(Range("J" & i)) > 0 Then
Range("A" & i & ":J" & i).Copy
Sheets("Sheet1").Range("A" & k).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
k = k + 1
Else
End If
Next i
Please try something like this
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then
'Your code here
End If
Next

How do I copy a worksheet from one workbook into another new workbook with just that worksheet in (with pastevalues)?

I am trying to create a macro that takes a worksheet from a workbook, and then saves that worksheet as a single workbook but with all cells as values rather than formulas.
The bit I am struggling with is the paste values section.
This is what I have currently:
ThisWorkbook.Sheets("Tickets (1-48)").Copy
With ActiveWorkbook
For ColNr = 8 To 120
If Cells(RowNr, ColNr - 1) = "0" Then
TempValue = Cells(RowNr, ColNr - 1)
If TempValue = "0" Then
For i = 0 To 9
Cells(RowNr, ColNr - 1).Select
Cells(RowNr, ColNr - 1).EntireColumn.Delete
Next i
ColNr = ColNr - 1
Else
End If
Else
End If
Next ColNr
.SaveAs strpath & "\" & "Retail " & strFilename & "(1-48)" & ".xls"
.Close 0
End With
Ignore the section in the middle.
Thanks.
I'm not sure what your code is doing but you can adapt it to this. Here is the general code that a recorded macro will show you.
'Selects everything on the current sheet and copies it
Cells.Select
Selection.Copy
'Add a new workbook.
'Adding a new workbook makes it the active workbook so you can paste to it.
Workbooks.Add
'Paste the date using Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C8").Select

Resources