Command button to paste table based on cell value - vba

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?

Related

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

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

Excel Macro for checkbox if true

i need a macro or a line to add to my code for when the checkbox is true or "Checked" i need it to copy the content of a specific cell into another in a sheet in the same workbook what i was able to get to is this code
Sub macro2()
Range("A1:C4").Select
Selection.Copy
Sheets("sheet2").Select
Range("A1:C4").Select
ActiveCell.PasteSpecial
End Sub
but as you can see it only gets me to get this range to be copied and pasted once i have checked the box what i need is to copy it when checked and maybe not to let it get m to the cell in sheet2 to allow multiple selections to be pasted in some specified cells i know i have the code sheet2.select but i honestly don't know any other coding and i need to put that kind of coding for over hundred cells so do appreciate your assistance with this if anybody can please am dying here
If CheckBox1.Value = True Then
ThisWorkbook.Sheets("Sheet2").Range("A1:C4").Value = ThisWorkbook.Sheets("Sheet1").Range("A1:C4").Value
End if

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.

Excel 2010 Macros Fun with Buttons

Ok so I am creating a spreadsheet that can be edited by another user but locked otherwise. What I am hoping to do is create 3 buttons. "What If" "Exit What if" and "Reset"
"What if" will allow for the user to input data.
"Exit what if" will allow for the user to exit the input mode and revert back to the default. document.
Then "Reset" will allow for the user stay in "What if" but reset all the values to default.
Then I want the button "What if" to appear somewhere up in the left but when you click it, its replaced by "Exit" and "Reset"
I suggest you to explain a little more you question, but so far for what I can infere you have the folloing issuse:
have a excel sheet with formulae and data locked.
offer edit the page, but no save the changes, as "a consult" to the data.
I can primarily offer the following:
Create a backup sheet where your save you base page
Unlock the sheet for editing.
if you exit the editing, restore the data from the backup to the main sheet.
if you reset the editing, do the same procedure as exit plus unlock again the data. (for how it was charted the code flow, the copied sheet has his data locked)
This will result in the followin code:
Sub BackUpData() 'this will be linked to you "what if" button
Sheets("Data_Sheet").Select 'select shhet with data, just in case
Range("A1:M56").Select ' range of your important data in your excel sheet
Cells.Select
Selection.Copy
Sheets("BackUp_Sheet").Select
Range("A1").Select 'lets paste the data in the same positión
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Data_Sheet").Select
End Sub
This make a copy of the data and the formulas, copying charts without breaking his datasource is another problem, maybe your can elabore on this matter. Have any charts?
Sub RestoreData() 'this will be linked to you "Reset" and "Exit" button
Sheets("BackUp_Sheet").Select 'select shhet with data, just in case
Range("A1:M56").Select ' range of your important data in your excel sheet
Cells.Select
Selection.Copy
Sheets("Data_Sheet").Select
Range("A1").Select 'lets paste the data in the same positión
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Usual room for improvement:
Dinamicaly select the range, but no select all the sheet, because memory isssuses may arise. (I run out of resources when try to copy all the cell of excel 2007 in my laptop :P).
Remove flicker with Application.ScreenUpdating.
I haven`t check if this work when the *backup_Sheet* is hidden.
The other isssuse is unlock the data in the sheet.
Sub UnlockMySheet()
'password here won`t protect the business logic or the code from prying eyes, just the user from themselves
ActiveWorkbook.Unprotect
ActiveSheet.Unprotect
Range("D9,B13").Select ' select the editable cells
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
ActiveWorkbook.Protect Structure:=True, Windows:=True
End Sub
Usual room for improvement:
Maybe I forgot the protect protocol and I`m just leaving the page exactly as it was. (sorry no time to proof this code).
Sugestion from stackoverflow collective mind.
and that is, for now