Copy All values VBA script failing - vba

I am trying to create a VBA script that copies all data in a whole workbook as pastes as values, then saves as a new workbook, thus removing all formulas.
Here is my code:
Sub MakeAllVals()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
With wSheet
.UsedRange.Copy
.PasteSpecial xlValues
End With
Next wSheet
Application.Dialogs(xlDialogSaveAs).Show
End Sub
I'm getting a runtime error 1004 on the .PasteSpecial xlValues command but I can't work out why.
How can I accomplish the goal of pasting all data as values and saving as a new workbook?

You just need to paste to a range in the new sheet. Currently you are not pasting within the new book and you are not pasting within a range.
Sub MakeAllVals()
Dim oldBook As Workbook, oldSheet As Worksheet
Dim newBook As Workbook, newSheet As Worksheet
Set oldBook = ThisWorkbook ' Set to the formula workbook you want to copy from
Set newBook = Workbooks.Add ' Make the new workbook you want only values in
For Each oldSheet In oldBook.Sheets ' Loop through all of the sheets in the formula book
Set newSheet = newBook.Sheets.Add ' Make a new sheet in the newbook to add the values to
newSheet.Name = oldSheet.Name & " Values"
oldSheet.UsedRange.Copy
newSheet.Range("A1").PasteSpecial xlValues ' Paste in a range on the new sheet as Values
Next oldSheet
Application.Dialogs(xlDialogSaveAs).Show ' Show Save As Dialog window
End Sub

You were close just have to move UsedRange up next to wSheet
Sub MakeAllVals()
Dim wSheet As Worksheet
For Each wSheet In ActiveWorkbook.Worksheets
With wSheet.UsedRange
.Copy
.PasteSpecial xlValues
End With
Next wSheet
Application.Dialogs(xlDialogSaveAs).Show
End Sub

Related

Copy/Paste Macro Pasting Randomly

I am having an issue copying and pasting a drop-down data validation menu from one sheet into all selected sheets. The drop-down menu seems to paste randomly instead of pasting into sheet "B22" of the selected sheets.
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
Selection.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = False
End Sub
Any suggestions on how to tackle this? I am having some difficulty finding the error in my code.
In case you want to work with the workbook and avoid using copy/paste..
Option Explicit
Sub Test()
Dim excel_sheet As Worksheet
Dim sht As Worksheet
Dim drop_down_value As Range
Set sht = ThisWorkbook.Sheets("Sheet2")
Set drop_down_value = sht.Range("B22")
'Sheets selection should be done before running macro
For Each excel_sheet In ThisWorkbook.Windows(1).SelectedSheets
excel_sheet.Range("B22").Value = drop_down_value.Value
Next
End Sub
Try the following, you need to loop through all selected Sheets instead of using Selection.Range:
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
For Each sht In ActiveWindow.SelectedSheets
sht.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = True
Next
End Sub

Export values not formulas with VBA

I am using the script below to export a worksheet as a CSV:
Sub Button14_Click()
'
' export Macro
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:M" & LR).SpecialCells(xlCellTypeConstants, 23).Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:= _
"C:\upload\19meat-kl.csv" _
, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
I am a novice at VBA scripting and I actually got this code from another post.
For the most part it does what I want but if I try to use a formula it exports the formula rather than the result. What do I need to change so that the cell contents are what is exported?
Further to the comment by #Mat'sMug you need to use PasteSpecial xlValues and not just Paste.
Try this example code below - it does what your original macro does but with some best practices:
Use Option Explicit to prevent issues with badly defined variables
Set references to source data and target data e.g. source worksheet (wsSource), source range (rngToCopy), target workbook (wbTarget) and
target worksheet (wsTarget) - this is better than using ActiveSheet or Something.Select and so forth, which is not best practice
Do the paste immediately after the copy to prevent issues with clipboard persistence
Code:
Option Explicit
Sub SaveRangeDataAsValuesOnNewWorkbook()
' set-up your variables
Dim strFileToSave As String
Dim wsSource As Worksheet
Dim lngLastRow As Long
Dim wbTarget As Workbook
Dim wsTarget As Worksheet
Dim rngToCopy As Range
' where you want to save
strFileToSave = "C:\upload\19meat-kl.csv"
' get a worksheet reference
Set wsSource = ThisWorkbook.Worksheets("Sheet1") '<~~ set to your worksheet
' get last row in column A - you need to reference a worksheet to do this properly
lngLastRow = wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row
' now - add a workbook and get its reference
Set wbTarget = Application.Workbooks.Add
' get the first worksheet in the new workbook
Set wsTarget = wbTarget.Worksheets(1)
' get a reference to your source range
Set rngToCopy = wsSource.Range("A2:M" & lngLastRow).SpecialCells(xlCellTypeConstants, 23)
' copy the source range
rngToCopy.Copy
' paste it to the target worksheet in the new workbook - you need to PasteSpecial to a Range
wsTarget.Range("A1").PasteSpecial xlValues
' save the new workbook
wbTarget.SaveAs Filename:=strFileToSave, FileFormat:=xlCSV, CreateBackup:=False
' close the new workbook
Application.DisplayAlerts = False
wbTarget.Close
Application.DisplayAlerts = True
End Sub

Copy/paste values from multiple sheets, but not all sheets, into one sheet

I am needing to copy cells B3:W400 from multiple sheets (will have varying names each time it is run) and paste values into "CombinedPlans", appending each new selection under the last. I need 3 sheets excluded from the code: IBExport, MonthlyIBs, and Combined Plans.
A lot of googling with trial and error has given me the following code, which I got to work in my "practice" workbook. Now that I have put it into my production workbook, it is no longer copying any sheets. It just skips straight to the message box. What am I doing wrong?
Sub consolidatetest()
Sheets("CombinedPlans").Select
Range("B3:W1048576").Select
Selection.ClearContents
Dim J As Integer
Dim sh As Worksheet
Const excludeSheets As String = "QBExport,MonthlyIBs,CombinedPlans"
On Error Resume Next
For Each sh In ActiveWorkbook.Worksheets
If IsError(Application.Match(sh.Name, Split(excludeSheets, ","))) Then
Application.GoTo Sheets(sh.Name).[b3]
Range("B3:W400").Select
Selection.Copy
Worksheets("CombinedPlans").Activate
Range("B1048576").End(xlUp).Offset(rowOffset:=1, columnOffset:=0).PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
MsgBox "Complete!"
End Sub
This should work. If you have still problems, make sure that the Sheet CombinedPlans is indeed so named.
Sub consolidatetest()
Dim wb As Workbook
Dim sh_CombPlans As Worksheet
Set wb = ThisWorkbook
Set sh_CombPlans = wb.Sheets("CombinedPlans")
sh_CombPlans.Range("B3:W1048576").ClearContents
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case "QBExport", "MonthlyIBs", "CombinedPlans":
'Do Nothing
Case Else
sh.Range("B3:W400").Copy
sh_CombPlans.Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End Select
Next
Application.CutCopyMode = False
MsgBox "Complete!"
End Sub

Excel vba to copy and paste a cells from different worksheets to new worksheet

i need a macro to copy and paste a certain cells(b5:d10) from multiple worksheets and paste it into a new single worksheet. just i want to collide the contents .thanks in advance
sub copyrange()
range("b5:d10).copy
range("e1").select
activesheet.paste
application.cutcopymode=false
endsub
my code doesnot copy all the worksheets data. kindly help me to copy and paste it
Sub copyrange()
Dim rngTarget As Range, wksTemp As Worksheet
Set rngTarget = ThisWorkbook.Worksheets(1).Range("A2:C7")
For Each wksTemp In ActiveWorkbook.Worksheets
rngTarget.Value = wksTemp.Range("B5:D10").Value
Set rngTarget = rngTarget.Offset(6)
Next wksTemp
End Sub

Copying range to new workbook

I have come across some code to copy a range to a new workbook, but I'm not sure why it works.
Worksheets("Short Form").Copy
Set wb = ActiveWorkbook
How does this copy the worksheet 'Short Form' to a new workbook when all that the code says is assign the active workbook to the reference 'wb'? It doesn't even employ the .add method. Right now I want to paste values only to this new workbook, but not quite sure how to do so because I don't understand this block of code.
Try this - as the following manual steps are the same as your code snippet:
1.Open a blank workbook
2.Press record macro
3.Right click the Sheet1 workbook tab
4.Select "Move or Copy"
5.In the "To book" combo select (new book)
6.Check the "Create a copy" box so that the window now looks like this:
7.Stop the recorder
8.Go and find your recorded code ...and voila....mine looks like this
Option Explicit
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets("Sheet1").Select
Sheets("Sheet1").Copy
End Sub
Your code is the same as what these manual steps describe.
You must have a line Dim wb as workbook somewhere or it would not run.
This line Set wb = ActiveWorkbook will then make the object wb equal to the new workbook that you have copied into, as it is active, so you can do further operations on it. You can easily switch the workbook that wb is pointed at:
Sub Macro1()
Dim wb As Workbook
ThisWorkbook.Sheets("Sheet1").Copy
Set wb = ActiveWorkbook
MsgBox wb.Name
ThisWorkbook.Activate
Set wb = ActiveWorkbook
MsgBox wb.Name
End Sub
BUT
In my production code I generally never use Set x To ActiveWorkbook I always name the workbook and then use Set x To Workbooks("DefiniteName")
WITHOUT USING CLIPBOARD
If you want to avoid using the clip board then the following example shows how to move values-only data without using paste:
Sub WithoutPastespecial()
Dim firstRange As Range
Set firstRange = ThisWorkbook.Worksheets("Short Form").Range("S4:S2000") 'can change S4:S2000 to the range you want to copy
Dim newBk As Workbook
Dim secondRange As Range
Set newBk = Workbooks.Add
Set secondRange = newBk.Worksheets("Sheet1").Range("A1")
With firstRange
Set secondRange = secondRange.Resize(.Rows.Count, .Columns.Count)
End With
secondRange.Value = firstRange.Value
End Sub
Note this is not copying a Range rather the entire worskheet :)
If you use the method:
Worksheets("Short Form").Cells.Copy
Then you will copy only the cells, not the entire worksheet, and this method will NOT create a new workbook. You can tell it to add a workbook when necessary.
Here is an example:
Option Explicit
Sub CopyNew()
Dim wbNew As Workbook
Dim wb As Workbook
Set wb = ThisWorkbook 'It is a good idea to explicitly control workbooks using either a defined variable like "wb" or the "ThisWorkbook" object, instead of using "ActiveWorkbook" or referring to files by name.
Application.CutCopyMode = False
wb.Sheets("Short Form").Cells.Copy
'Add a new workbook for the values:
Set wbNew = Workbooks.Add
wbNew.Sheets(1).Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub