i am trying to use pastespecial in vba..i basically need to paste the values (and not the formulas as the formula gets recalculated while pasting to the new sheet because of change in the cell values in that sheet) to another sheet...But i am getting error 1004 saying 'aaplication defined or object defined error'..heres the code...please help somebdy...
Sub Macro1try()
Dim i As Integer
Dim j As Integer
For i = 1 To 2
Worksheets("Volatility").Cells(1, "B").Value = Worksheets("Volatility").Cells(i, "S").Value
Call mdlMain.ExtractData
Range("A11:D2330").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
ActiveWorkbook.Sheets("Volatility").Activate
Next i
End Sub
This I learned the hard way: Avoid Copy/Paste if at all possible! Copy and Paste use the clipboard. Other programs may read from / write to the clipboard while your code is running, which will cause wild, unpredictable results at both ends.
In your particular case, Copy and Paste are completely unnecessary. Just use =.
For i = 1 To 2
'// Your stuff, which I won't touch:
Worksheets("Volatility").Cells(1, "B").Value _
= Worksheets("Volatility").Cells(i, "S").Value
Call mdlMain.ExtractData
Sheets.Add After:=Sheets(Sheets.Count)
'// The following single statement replaces everything else:
Sheets(Sheets.Count).Range("A11:D2330").Value _
= Sheets("Volatility").Range("A11:D2330").Value
'// Voilà. No copy, no paste, no trouble.
'// If you need the number format as well, then:
Sheets(Sheets.Count).Range("A11:D2330").NumberFormat_
= Sheets("Volatility").Range("A11:D2330").NumberFormat
Next i
You need to state where you're putting it on the sheet
Sub Macro1try()
Dim i As Integer
Dim j As Integer
For i = 1 To 2
Worksheets("Volatility").Cells(1, "B").Value = Worksheets("Volatility").Cells(i, "S").Value
Call mdlMain.ExtractData
Sheets.Add After:=Sheets(Sheets.Count)
Worksheets("Volatility").Range("A11:D2330").Copy
Sheets(Sheets.Count).Range("A11:D2330").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Next i
End Sub
Add
".Range("A1")."
Between 'ActiveSheet' and 'PasteSpecial'
Change A1 to the location you want to paste to.
Related
I am trying to create a sheet that I can fill in, when i click the button it copies the information from the box and paste (and Transpose) to the Rawdata tab, then deletes the information from the Dashboard and saves the file.
I have recorded a simple macro to do this for me, but the problem is that I dont know how to get it to add the data to the next free row, it just replaces the information already there, this is the code i am using to try make it happen
Sub Macro5()
'
' Macro5 Macro
'
'
Range("C3:C8").Select
Selection.Copy
Sheets("RawData").Select
Cells(Range("C1000000").End(xlUp).Row + 1, 3).PasteSpecial Paste:=xlValues,
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Dasboard").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
Range("C3:C8").Select
Selection.ClearContents
End Sub
any help here would be much appreciated.
I need it to transpose paste in the A:F columns
thank you
I recommend to read How to avoid using Select in Excel VBA. Using .Select is a very bad practice and results in many errors soon or later:
A better technique is defining worksheets and ranges so you can access them directly with a variable:
Option Explicit
Public Sub CopyData()
'define source range
Dim SourceRange As Range
Set SourceRange = ThisWorkbook.Worksheets("Dashboard").Range("C3:C8")
'find next free cell in destination sheet
Dim NextFreeCell As Range
Set NextFreeCell = ThisWorkbook.Worksheets("RawData").Cells(Rows.Count, "A").End(xlUp).Offset(RowOffset:=1)
'copy & paste
SourceRange.Copy
NextFreeCell.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ThisWorkbook.Save
SourceRange.ClearContents
End Sub
Try a direct value transfer without Select.
Sub Macro5()
dim ws as worksheet
set ws = workSheets("RawData")
with worksheets("Dasboard").Range("C3:C8")
ws.cells(rows.count, "A").end(xlup).offset(1, 0).resize(.columns.count, .rows.count) = _
application.transpose(.value)
.clearcontents
.parent.parent.save
end with
End Sub
I am very new to vba (and code in general) so I apologise if I haven't asked the right question or have missed a thread that covers this. I have spent a couple of weeks trying to find the answer so hopefully you may be able to help.
I am trying to copy, data from one sheet (Named Master Sheet) to another depending on a variable in column L (Variables "In Progress" or "Not Started") to an Overview / GUI sheet. My current code (below) does this for the first line of data, however I would like this to work for the whole sheet.Unfortunately it will have a changing amount of data added so the array will be expanding- unsure how much more difficult this will make it.
Thank you very much for any help you can provide, and I apologise for the marked out notes. I can add a picture too (if possible) if it would help make more sense of what I would like to do?
Sub Update_Uncompleted_Tasks()
' Update_Uncompleted_Tasks Macro
' Selects tasks from Master Sheet and copies to the Overview Sheets if assigned as uncompleted
'DON'T USE BELOW YET (UNSURE IF IT WILL WORK)
'Maybe Vlookup?
'Dim LastRow As Long, i As Long
'LastRow = Cells(Rows.Count, "L").End(xlUp).Row
'For i = 1 To LastRow
Sheets("Master Sheet").Select
If Range("L2") = "In Progress" Then
Range("A2:L2").Select
Selection.Copy
Sheets("Overview").Select
Application.Goto Reference:="R10000C2"
Selection.End(xlUp).Select
Selection.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ElseIf Range("L2") = "Not Started" Then
Range("A2:L2").Select
Selection.Copy
Sheets("Overview").Select
Application.Goto Reference:="R10000C2"
Selection.End(xlUp).Select
Selection.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
'Next i
End Sub
First of all you should take a look at this: Avoid using select in Excel-VBA-Macros.
The following code should fulfill your needs, but it requires that your data in column L has no empty cells (until the end is reached)
Sub Update_Uncompleted_Tasks()
Dim row as Long
'Initial value
row = 2
With ThisWorkbook.Worksheets("Master Sheet")
Do Until IsEmpty(.Range("L" & row))
If (.Range("L" & row).Value = "In Progress") Or (.Range("L" & row).Value = "Not Started") Then
.Range("A" & row & ":L" & row).Copy
ThisWorkbook.Worksheets("Overview").Range("B10000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
row = row + 1
Loop
End With
End Sub
The loop ends when a row is reached where the cell in column L is empty.
EDIT: You can also replace the Do Until -- Loop with a For row=2 To lastRow -- Next row. To determine the last row of your data, there are many ways, check out this link Excel-VBA find last row or just use the search function.
You can use this piece of code to get the number of last nonempty line on your sheet:
Dim LastDataLine As Long
LastDataLine = Sheets("Master Sheet").Range("A" & Rows.Count).End(xlUp).Row
And for last non-empty column (just in case):
Dim LastDataCol As Integer
LastDataCol = Sheets("Master Sheet").Cells(1, Columns.Count).End(xlToLeft).Column
If you also need help implementing a loop which goes through each line leave a comment.
I have a workbook that serves as source data for another Excel-based form (not a Userform, just a formatted spreadsheet). The source file has anywhere from 2-40 rows of data - starting from row 18 - and each row needs to be copied into the form and saved separately, i.e. 15 rows from the Source file equates to 15 distinct Form files.
Each cell within the row must be copied separately and pasted to specific cells on the Form. The Source form contains Clients and their relevant info. I am trying to use a macro on the Form to automatically pull line items from the Source file, save the Form as the client's name in a specified folder, and continue until a blank row is reached on the Source file. I have some basic VBA experience, but have little knowledge of loops, variables, or functions, which seem to be my best course of action here.
Here's what I have so far. All I've been able to accomplish is the copy/pasting of the first row from the Source file.
Range("B18").Select
Selection.Copy
Windows("Form.xls").Activate
Range("F7:K7").Select
ActiveSheet.Paste
Windows("Source.xls").Activate
Range("C18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("D8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Source.xls").Activate
Range("D18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("H29").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Source.xls").Activate
Range("E18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("E29").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Source.xls").Activate
Range("F18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("D33").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(“F7:K7”).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWorkbook.SaveAs
I can't even get my macro to save the Form by the client's name. I also know that my extensive use of "Range" and "Select" will slow my code down to a crawl, I just don't know how to make it more efficient. I've tried using a reference cell to that tells the macro which row of the Source file to copy but haven't had any luck down that road either. Any and all help will be greatly appreciated!
Here's a simple demo:
Note: Not Tested
Option Explicit
Sub CopyToForm()
Dim wbSource As Workbook, wbForm As Workbook
Dim wsSource As Worksheet, wsForm As Worksheet
Dim formpath As String, foldertosavepath as string
Dim lrow As Long, i As Integer
Set wbSource = Thisworkbook '~~> assuming you write your code in Source.xls
Set wsSource = wbSource.Sheets("NameOfYourSheet") '~~> put the source sheet name
'~~> put the path where your form template is saved here
formpath = "C:\Users\Username\FolderName\Processing Form.xls"
'~~> put the path where you want to save individual updated forms.
foldertosavepath = "C:\Users\Username\FolderDestination\"
With wsSource
'~~> get the number of rows with data
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
If lrow < 18 Then Msgbox "No data for transfer": Exit Sub
For i = 18 to lrow
Set wbForm = Workbooks.Open(formpath) '~~> open the form
Set wsForm = wbForm.Sheets("Sheetname") '~~> put the form sheet name
'~~> proceed with the copying
.Range("B" & i).Copy: wsForm.Range("F7:K7").PasteSpecial xlPasteValue
.Range("C" & i).Copy: wsForm.Range("D8").PasteSpecial xlPasteValues
.Range("D" & i).Copy: wsForm.Range("H29").PasteSpecial xlPasteValues
.Range("E" & i).Copy: wsForm.Range("E29").PasteSpecial xlPasteValues
.Range("F" & i).Copy: wsForm.Range("D33").PasteSpecial xlPasteValues
'~~> Save the form using the client name, I assumed it is in B?
wbForm.SaveAs foldertosavepath & .Range("B" & i).Value & ".xls"
wbForm.Close True
Set wbForm = Nothing
Set wsForm = Nothing
Next
End With
End Sub
In above code, I assumed that Form.xls is the same as Processing Form.xls.
This should give you the logic.
I hope this get's you started.
This is not test as I've noted, so if you encounter errors, comment it out.
You are frequently activating the workbooks. that's your code slows down..Below code will work faster
Sub test()
Dim dwb As Workbook
Dim swb As Workbook
Set dwb = Workbooks("Form.xls")
Set swb = Workbooks("Source.xls")
Set awb = Workbooks("Processing Form.xls")
With swb
.ActiveSheet.Range("B18").Copy Destination:=dwb.Sheet1.Range("F7:K7")
.ActiveSheet.Range("C18").Copy Destination:=awb.Sheet1.Range("D8")
.ActiveSheet.Range("D18").Copy Destination:=awb.Sheet1.Range("H29")
.ActiveSheet.Range("e18").Copy Destination:=awb.Sheet1.Range("E29")
.ActiveSheet.Range("F18").Copy Destination:=awb.Sheet1.Range("D33")
End With
End Sub
This might help steer you in the right direction:
Dim i As Long
For i = 1 To 10
With Range("A" & i)
.Copy Workbooks("ToWorkbook.xlsx").Worksheets("Sheet1").Range("B" & i + 9)
.Copy Workbooks("ToAnother.xlsx").Worksheets("Sheet2").Range("C" & i + 8)
.Copy Workbooks("AnotherOne.xlsx").Worksheets("SheetA").Range("D" & i + 2)
End With
Next i
i To 10 is used as a counter to loop through the rows in the source workbook.
For each i, you're taking the range from column A (i.e., with this, do something), copying and pasting it into different cells in different workbooks. In the first round, Range("A1") is being copied into 3 different workbooks at Range("B10"), Range("C9") and Range("D3"), respectively. The next turn, Range("A2") from the source book is going to be copied and pasted into the same destination workbooks from last time, but in Range("B11"), Range("C10") and Range("D4"). It's just a matter of finding the pattern for the different forms you need to paste into.
I have some macros that copy Sheet 2 from my exsisting work book to a new work book. This code works as it should except that there are hidden rows that should not be shown on the new work book.
Here is the code that I have written that copies the sheet over and pastes only its values:
Dim Output As Workbook
Dim FileName As String
Set Output = Workbooks.Add
Application.DisplayAlerts = False
ThisWorkbook.Worksheets(sourceSheetName).Cells. _
SpecialCells(xlCellTypeVisible).Copy
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
FileName = ThisWorkbook.Path & "\" & ThisWorkbook.Worksheets("Quote Questions").Range("AK545").Value & ".xls"
Output.SaveAs FileName
So where would the code go that will only display the unhidden cells and not the hidden ones?
EDIT
The code has changed slightly after an answer was submitted. Here is more info.
Some of cells in the sheet that is being copied are merged and I get an error on the line of code:
ThisWorkbook.Worksheets(sourceSheetName).Cells. _
SpecialCells(xlCellTypeVisible).Copy
Saying: Cannot change part of a merged cell, so im guessing there needs to another piece to add?
I do not want to go to the sheet and have un-merge all the cells manually.
Replace the line
ThisWorkbook.Worksheets("Quote & Proposal").Cells.Copy
with
ThisWorkbook.Worksheets("Quote & Proposal").Cells. _
SpecialCells(xlCellTypeVisible).Copy
and it should work.
Copy only visible Rows (not hidden)
You can check if the Row is hidden with this code
Sub RowIsHidden()
For i = 1 To 7
MsgBox Cells(i, 1).EntireRow.Hidden
Next
End Sub
Copy Cells and Paste only Values
This is similar to your code above. Instead of index of the sheet you could also use the sheetname
Sub CopyOnlyValuesFromSheet()
' Copy all Cells from first Sheet (SheetIndex =1)
ThisWorkbook.Worksheets(1).Cells.Copy
' Select second Sheet (SheetIndex =2)
ThisWorkbook.Worksheets(2).Select
' Paste only values into Selection
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
End Sub
Clear Values of hidden Rows
I have tried to use Cells(i, 1).EntireRow.Delete Shift:=xlUp but since this has consequence over which rownumber i you have to iterate next it is easier to just clear the values
Sub RowIsHiddenClearValue()
For i = 1 To 10
If Cells(i, 1).EntireRow.Hidden Then
Cells(i, 1).EntireRow.Value = ""
End If
Next
End Sub
Based of Peters answer
Make sure that the cursor in the destination sheet is placed in the first cell.
Sub AnotherAnswer()
Call CopyValuesOfVisibleRows("Quote & Proposal", "Quote Questions")
End Sub
Sub CopyValuesOfVisibleRows(sourceSheetName, destinationSheetName)
ThisWorkbook.Worksheets(sourceSheetName).Cells. _
SpecialCells(xlCellTypeVisible).Copy
ThisWorkbook.Worksheets(destinationSheetName).Paste
End Sub
If you need more pointers to put the pieces together please provide more details with which parts you have problems.
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