I am busy creating a production schedule in excel 2010. I have used formulas to give me due dates for each job and a check box for when the job are done. I used the following VB to move the information to sheet 2 when the check box is checked.
Sub MoveData()
If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
Range("A2:B2:C2:D2:E2:F2:G2:H2:I2:J2:K2:L2:M2").Select
Selection.Cut
Sheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End If
End Sub
The problem I have now is that it moves the formulas with the details in the cells from sheet 1 to sheet 2. I need the formulas to be locked in sheet 1 and affect new information inserted into the cells.
When the data in the row moves to sheet 2 the row is empty in sheet 1. Is there a way to automatically move all the remaining data up to fill the empty cells
Thanx
Do you just want to copy the values from Worksheets(1) to Sheets("Sheet2")?
Sub MoveData()
Dim r As Range
If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
Set r = Worksheets(1).Range("A2:B2:C2:D2:E2:F2:G2:H2:I2:J2:K2:L2:M2")
Sheets("Sheet2").Range("A1").Resize(1, r.Count) = r.Value
'r.ClearContents
End If
End Sub
Instead of cut and paste try the following.
Sub MoveData()
If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
Sheets("Sheet2").Range("A1:M1").Formula = Sheets("Sheet1").Range("A2:M2").Formula
Range("A1").Select
End If
End Sub
Edit:
If you were cutting to remove the originals from Sheet1 just use,
Sheets("Sheet1").Range("A2:M2").Clear
Related
I am trying to loop through a bunch of sheets and check the same cell in each sheet and report back the highest value. I currently have some code that loops through a sheet and copies some data and paste in below and it cycles through all the sheets but I cannot figure out how to get the determine max value part. Data used to copy data from one sheet to another shown below.
Public Sub CopyData()
Dim i As Integer
For i = 2 To ThisWorkbook.Worksheets.Count
'Method 1
Sheets("4-1-21").Range("A53:F100").Copy Destination:=Sheets(i).Range("A53:F100")
'Method 2
'Copy the data
Sheets("4-1-21").Range("A53:F100").Copy
'Activate the destination worksheet
Sheets(i).Activate
'Select the target range
Range("A53:F100").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = True
Next i
End Sub
I am trying to print several non-contiguous columns on one sheet of paper. I am using the set print area in page layout. However it has created a page break after each column range. I have a macro that allows you to paste a range of data, and then it clears the data and prints the page. So hiding the columns isn't working. Can someone tell me how to print just the columns I need on one piece of paper. Here is the macro where I would like to include the printing code.
Sub cleardatanotformulas()
ActiveWindow.SelectedSheets.PrintOut Copies:=1
On Error Resume Next
Cells.SpecialCells(xlCellTypeConstants).ClearContents
ActiveWorkbook.Close SaveChanges:=False
End Sub
You can create a new page to receive your selected columns for printing Like this code below. Mu non-contiguous columns are in MySourceSheet. You have to copy them in MytargetSheet and print it.
Sub CopyInAdjacentColumnsForPrinting()
Sheets("MySourceSheet").Select
Range("A:A,C:C,E:E").Select 'We select A, C and E columns for example, you can specify your own columns
Range("E1").Activate
Selection.Copy
Sheets("MyTargetSheet").Select ' This sheet is for receiving selected columns in contiguous columns.
Range("A1").Select
ActiveSheet.Paste ' We copied selected columns in this sheet.
Range("A1").Select
End Sub
Hope this can help!
Sorry #Anne B, I cannot add my code in comment bloc, then I add a new answer :)
Based on the fact that you copied your non-contiguous columns in the sheet named MyTargetSeet (in this example).
You can use this.
Sub ZoneImp()
Dim FirstCol As Integer, LastCol As Integer, FirstLin As Integer, FirstLin As Integer
FirstCol = 1
LastCol = 3
FirstLin = 1
LastLin = 6
'Here I create my Area for printing
Sheets("MyTargetSheet").PageSetup.PrintArea = Range(Cells(FirstLin, FirstCol), Cells(LastLin, LastCol)).Address
End Sub
Hope this can help!
Background
I have a workbook with 7 sheets but only 6 with data. All of these 7 sheets have the same structure/headings, it's just that the last sheet (sheet 7) is blank.
I want to write a code that will copy the contents of sheets 1-6 in the range of A2:J15 into the sheet 7 worksheet. I currently have a code that I have been using to test and see if it works but the code I have only copies and pastes from one worksheet only (see below). Any suggestions?
In the below, I have two sheets where I want the data to come from and the destination sheet where I want the data to go:
Sub sbCopyRangeToAnotherSheet()
Sheets("Source1").Range("A1:B10").Copy
Sheets("Source2").Range("A1:B10").Copy
Sheets("Destination").Activate
Range("A1:B10").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Your problem is coming from your attempt to copy two items directly after each other. The second copy call is overwriting the data you copied in the first call.
Sheets("Source1").Range("A1:B10").Copy
Sheets("Destination").Activate
Range("A1:B10").Select
ActiveSheet.Paste
Sheets("Source2").Range("<your new range here>").Copy
Sheets("Destination").Activate
Range("<your new range here>").Select
ActiveSheet.Paste
Application.CutCopyMode = False
The code above should explain what I mean albeit not the most efficient way. A more effective way would be to use:
Sheets("Source1").Range("A1:B10").Copy Destination:=Sheets("Destination").Range("A1:B10")
Sheets("Source2").Range("A1:B10").Copy Destination:=Sheets("Destination").Range("<range>")
As sugguested in the comments:
Sub sbCopyRangeToAnotherSheet()
Sheets("Source1").Range("A1:B10").Copy Sheets("7").Range("A1")
Sheets("Source2").Range("A1:B10").Copy Sheets("7").Range("A1").end(xlDown).offset(1,0)
Sheets("Source3").Range("A1:B10").Copy Sheets("7").Range("A1").end(xlDown).offset(1,0)
Sheets("Source4").Range("A1:B10").Copy Sheets("7").Range("A1").end(xlDown).offset(1,0)
Sheets("Source5").Range("A1:B10").Copy Sheets("7").Range("A1").end(xlDown).offset(1,0)
Sheets("Source6").Range("A1:B10").Copy Sheets("7").Range("A1").end(xlDown).offset(1,0)
End Sub
Assuming that you want to paste the data by rows (and not overwrite it), and your sheets are named Source1 to Source6, then the following should work:
Sub testSO()
For i = 1 To 6
Sheets("Source" & i).Range("A1:B10").Copy Sheets("Destination").Range("A" & Rows.Count).End(xlUp).Offset(1)
Next i
End Sub
I have two excel sheets sheet1 and sheet2.Sheet1 is a dynamic excel sheet,there may be chance of adding columns.I have already coded to copy column heading from sheet1 to sheet2 dynamically.
Sheet1:
Prdct Id PrdctQty Unitprice PrdctQty
1 5 10 50
2 10 10 100
sheet2:
Prdct Id PrdctNme Unitprice PrdctQty
When i open sheet2,these headings automatically appears from sheet1(using macro).There are 2 buttons in sheet2.
1.Display-display product details on matching Prdct Id entered by the user(that also done through macro)
2.Add- To add new product,user can enter Prdct Id , PrdctNme, Unitprice and it will be copied to sheet1 (through macro)
Sheet1 also contains other columns having fromulas(which i didnt show in the example)and sheet1 can grow dynamically.
So what i want is when user enters Prdct Id , PrdctNme, Unitprice then PrdctQty should automatically come in sheet2 (along with other calculated columns which i am not including for the time being) and after that i can add the new product to sheet1
i tried this code (from stackoverflow)
Sub dural()
Dim r As Range, ady As String
For Each r In Sheets("Sheet1").Cells.SpecialCells(xlCellTypeFormulas)
ady = r.Address
r.Copy Sheets("Sheet2").Range(ady)
Next
End Sub
but what i am getting is a whole copy of sheet1 in sheet2 along with values.What i need is only formulas not values
Try Something like this :
Sub moveformulas ()
Sheets(1).UsedRange.SpecialCells(xlCellTypeFormulas).Copy
Sheets(2).Range("A1").PasteSpecial
End Sub
I found a way even though i am not sure its the right way.
Sub dural()
Dim r As Range, ady,ady2 As String
For Each r In Sheets("Sheet1").Cells.SpecialCells(xlCellTypeFormulas)
ady = r.Address
ady2=r.formula
Sheets("Sheet2").Range(ady).formula=ady2
Next
it worked for me
Sub CopyOnlyFormulas()
Sheets(1).UsedRange.Copy
Sheets(2).Cells.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone
For Each cell In Sheets(2).UsedRange
If Not cell.HasFormula Then
cell.Clear
End If
Next
End Sub
Sub CopyDataAndFormulas()
Sheets(1).UsedRange.Copy
Sheets(2).Cells.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone
End Sub
I am trying to select and a row of 5 cells to the right (the selection should include my active cell) of my active cell. My current code is:
Sub SelectandCopy()
'
' SelectandCopy Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
ActiveCell.CurrentRegion.Select
Selection.Copy
Windows("Study.xlsx").Activate
End Sub
I do want not a specific range from my worksheet because I want to be able to make a selection of these data sets from anywhere within my worksheet. Any help would be greatly appreciated!!
Would you use the Selection.Extend?
This copies the 5 cells to the right of the activecell. If you have a range selected, the active cell is the top left cell in the range.
Sub Copy5CellsToRight()
ActiveCell.Offset(, 1).Resize(1, 5).Copy
End Sub
If you want to include the activecell in the range that gets copied, you don't need the offset:
Sub ExtendAndCopy5CellsToRight()
ActiveCell.Resize(1, 6).Copy
End Sub
Note that you don't need to select before copying.
This example selects a new Range of Cells defined by the current cell to a cell 5 to the right.
Note that .Offset takes arguments of Offset(row, columns) and can be quite useful.
Sub testForStackOverflow()
Range(ActiveCell, ActiveCell.Offset(0, 5)).Copy
End Sub