Sometimes when I run the code it works, other times I get Run-Error '1004' and I don't know why? - vba7

First of all I'm a beginner with VBA so I don't understand a lot. I tend to record my macros and then slightly modify them.
I get the following Error 1004 message
You can't paste this here because the Copy Area and paste area aren't the same size. Select just one cell in the paste area or an area that's the same size, and try pasting again.
Private Sub TransferExpenses_Click()
ThisWorkbook.Activate 'Transfer ExpenseImport Data over to CMiCExport Tab
Sheets("ExpenseImport").Select
Range("A1:AE1", Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Activate
Sheets("CMiCExport").Select
Sheets("CMiCExport").Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Sheets("CMiCExport").Paste
MsgBox Title:="Expenses Imported Successfully!", Prompt:="The data for your expenses was verified and transferred to the CMiCExport Tab. Please double check column C -Job & Scope- and revise the .XXDefault entries."
End Sub
I'm basically just trying to copy data from one sheet "ExpenseImport" to "CMiCExport" on the next blank row. The data will always be from column A to AE since it's mapped, but the rows will always vary depending on the amount of entries for that particular week. When I step into and run the code using the "F8" it works just fine, but when running the code using an active control, it fails. Can someone help me?

I think it´s because the blank cells. Try this:
Private Sub TransferExpenses_Click()
ThisWorkbook.Activate 'Transfer ExpenseImport Data over to CMiCExport Tab
Sheets("ExpenseImport").Select
Range("A1:AE1", Selection.End(xlDown)).Select
'Avoid the copy if all cells are blank
If Application.WorksheetFunction.CountBlank(Selection) = Application.WorksheetFunction.CountBlank(Selection) Then
Exit Sub
End If
Selection.Copy
ThisWorkbook.Activate
Sheets("CMiCExport").Select
Sheets("CMiCExport").Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Sheets("CMiCExport").Paste
MsgBox Title:="Expenses Imported Successfully!", Prompt:="The data for your expenses was verified and transferred to the CMiCExport Tab. Please double check column C -Job & Scope- and revise the .XXDefault entries."
End Sub

Related

how to set range for a dynamic area and pasting with filters on in vba

I'm having some trouble with a 2 step problem.
The first part is setting a range to constantly changing data. I've been trying to categorize claims on a worksheet that has data that is being added to the same sheet daily, so the last active cell keeps changing. For instance my issue with the specific line of code is as follows.
Columns(“D:D”).Select
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select
ActiveCell.Offset(0, 91).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Columns(“D:D”).Select
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select
ActiveSheet.Paste
ActiveSheet.Range("$A$1:$EE$3000”).AutoFilter Field:=22, Criteria1:= Array( _
"DUPLICATE PAID/CAPTURED CLAIM:MORE CURRENT REFILL EXISTS", _
"REFILL TOO SOON:CLAIM ALREADY PROCESSED FOR STORE, RX, DOS", _
"REFILL TOO SOON:DISPENSED TOO SOON"), Operator:=xlFilterValues
Columns(“A:A”).Select
Selection.End(xlDown).Select
Selection.Copy
ActiveCell.Offset(1).Select
ActiveSheet.Paste
But I'm now recognizing setting the end range to a value of $EE$2007 isn't working. The column of EE will always remain the same, however the row changes.
The second part has to do with filtering. There are around 56 different categories in a separate column, V which then get flagged with a keyword in column A. I've gotten as far as being able to do the filtering portion and copying from the next cell up, but I'm having trouble pasting that keyword down the next which is the next blank cell and down to the last active row, again which all happens in column A. Above is what I have so far.
And this is where I get stuck. I'm new to all this, and am hoping to learn if there is a better way to go about this.
I was able to get to the second part by changing the range from $EE$2007 to $EE$3000 which caused my data to go further, but it is a possibility.
Thanks in advance.

Refresh all connections VBA not working in a run all macro

I have an SQL Server connected excel workbook in which I am trying to execute three VBA macros in order
Refresh all connections (data and charts)
Copy all values from the from a summary sheet and past as values
Copy a range of values from summary sheet and paste to another
I created a macro called RunAllMacros to execute in that order. For some reason it says it is connecting to the data source and running the SQL but on the summary page there are a number of formulas to show the updated results. For some reason it does not update those numbers when ran together. When I run the first (refresh all connections) macro it works fine - the numbers are updated. Any idea what might be going on here? Just to note I am quite a novice at VBA.
VBA:
Private Sub RefreshAllConnections()
'This step uses the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub
Private Sub CopyPasteAsValues()
'This step copies the summary sheet and pastes as values
Sheets("Summary").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
Private Sub PasteChartData()
'This step pastes the range of values to the chart data tab
Sheets(1).Range("A6:J24").Copy _
Destination:=Sheets("Chart Data").Cells(Sheets("Chart Data").Rows.Count, 1).End(xlUp).Offset(1, 0)
End Sub
Sub RunAllMacros()
RefreshAllConnections
CopyPasteAsValues
PasteChartData
End Sub

Command button to paste table based on cell value

I have an Excel sheet in which I'm trying to make a walkthrough type of thing, for training new employees. At its core, I want to have a dropdown menu (got that already) filled with options, and then a command button that will check the contents of the drop down cell, and copy-paste a table from a hidden sheet onto the main sheet. For some reason, I can't get the button to work. This is what I've got so far:
Private Sub button_desk_Click()
Application.ScreenUpdating = False
'Create Table
If Worksheets("Walkthrough").Range("A2").Value2 = "Getting your desk set up" Then
Sheets("Settings").Select
Range("lookup_desksetup[[#All],[Getting your desk setup]]").Select
Selection.Copy
Sheets("Walkthrough").Select
Range("B5").Select
ActiveSheet.Paste
End If
Application.ScreenUpdating = True
End Sub
I've tried a few different approaches for this, including not using the .Select command, but I can't seem to get anything to work.
Sheets("Settings").Select
Range("lookup_desksetup[[#All],[Getting your desk setup]]").Select
Selection.Copy
Sheets("Walkthrough").Select
Range("B5").Select
ActiveSheet.Paste
is a long winded way of saying
Sheets("Settings").Range("lookup_desksetup[[#All],[Getting your desk setup]]").copy _
destination:= Sheets("Walkthrough").Range("B5")
However I suspect that your problem is that "lookup_desksetup[[#All],[Getting your desk setup]]" is a very funny range name and may not actually exist as a valid name in that sheet?

Macro Copy&Paste

I'm trying to create a macro that will copy data from one worksheet and place into another. This I can do with no problem. But, when I want to use the same macro in another row is where I have my problem. Basically what I want to do is copy cell D11 from sheet1 and place that in cell B4 on sheet2, etc (What I'm doing is obviously more complicated than that, but that doesn't matter here).
My problem is when I want to now run this macro and copy cell D12 from sheet1 and paste into B5 on sheet2 the value pasted jumps to B4. I understand that this happens because of where the VBcode is saying to paste the copied value.
My question is how to I just have it paste in whatever row I choose? Maybe based on what row/cell I have selected.
Current code, written by recording the macro
Sheets("sheet1").Select
Range("D11").Select
Selection.Copy
Sheets("sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B4").Select
I'm assuming the last line is where I need to make the change, but I'm not sure what to change.
Thank you! Any and all help is greatly appreciated.
As a general rule, try to avoid Selection Copy-Paste (detailed discussion is provided in: "Application.Calculation = xlCalculationManual" statement causing run-time error 1004 in VBA Copy-Paste procedure). Instead, use direct copy statement, which will solve you issue and significantly improve performance:
Listing 1.
Sub DirectCopySample()
Application.ScreenUpdating = False
Sheets("Sheet1").Range("D11").Copy Destination:=Sheets("Sheet2").Range("B5")
Application.ScreenUpdating = True
End Sub
Sub in Listing 1 performs direct copy from Cell: Sheets("Sheet1").Range("D11") into cell: Sheets("Sheet2").Range("B5").
Also, your initial Copy-Paste Sub could be simplified (it will also make it work, though Listing 1 is preferred)
Listing 2.
Sub CopyPasteSample()
Sheets("sheet1").Range("D11").Copy
Sheets("sheet2").Range("B5").PasteSpecial Paste:=xlPasteValues
End Sub
Hope this will help. Best regards,
You seem to have recorded a Macro and are trying to replay it. Here is a real VBA code (not a Macro recording type):
Sheets("sheet2").Range("B5") = Sheets("sheet1").Range("D11").Value
This is all!
BTW, your predicament comes from the fact that the PasteSpecial method copies into the currently selected cell. You've tried running this Macro several times and the Range("B4").Select line did the trick. If you insist on your approach the insert Range("B5").Select BEFORE the PasteSpecial.

Copy Cell To Another Sheet, one after another

I have been trying to find the solution to this for a while now and I do not believe I am searching for the right thing.
Basically what I want to do is copy a cell contents and paste it to another sheet and then delete the contents of the cell that was copied from.
I have one button that corresponds to each cell and I want to write a bit of code which enables you to do this at the click of a button.
Another issue is I don't know the code that would not constantly paste in the same cell over and over, but rather go to the next row and paste there.
What I have at the moment is the following:
Sub Macro1()
Range("I2").Select
Selection.Copy
Sheets("Completed Tasks").Select
Range("A72").Select
ActiveSheet.Paste
Sheets("Projects Live!").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("H2").Select
End Sub
This only refers to the top cell and i have recorded a macro to show essentially what i want.
Hope you can help or point me in the right direction.
Consider:
Sub luxation()
Range("I2").Copy Sheets("Completed Tasks").Range("A72")
Range("I2").Clear
End Sub
This is for a single cell...............you would embed this kind of syntax in a loop.