Excel Macro module to copy variable columns - vba

I'm trying to create a Macro that copies variable columns from one sheet to another. For example I would like to copy column A-sheet 3 to column A-sheet 6, column B-sheet 3 to column C-sheet 6, etc.
I was hoping to create one procedure that can be called many times.
Sub Copy_Column(a, b)
Sheets(3).Select
Range("a11:a1000").Select
Selection.Copy
Sheets(6).Select
Range("b15").Select
ActiveSheet.Paste
End Sub
Sub Master()
Call Copy_Column(a, a)
End Sub
What happens is it copies Column A in sheet 3 to Column B in sheet 6, not in Column A.
Thanks in advance!

Well you never use the a and b parameters in the routine.
Did you mean them to represent column letters? If so, then
Sub Copy_Column(byval a as string, byval b as string)
Sheets(3).Range(a & "11:" & a & "1000").Copy Sheets(6).Range(b & "15")
End Sub
Sub Master()
Call Copy_Column("a", "a")
End Sub

Related

Print non contiguous columns in Excel

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!

Excel VBA: How to change value of cell in last used column?

I am new to VBA and am trying to locate the last used column (changes monthly) and change the second row cell in that column to "Apple." The values in the last used column start from row 5, but I need to change row 2. Can anyone help? This the code I have come up with, I understand it's flawed:
Sub NameCell()
.Cells(5, Columns.Count).End(xlToLeft).Column.Select
.FormulaR3C1 = "Apple"
End Sub
Sub NameCell()
With ActiveSheet
.Cells(2,.Cells(5, .Columns.Count).End(xlToLeft).Column).Value = "Apple"
End With
End Sub

Copy range from multiple sheets rather than just one sheet

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

Remove certain columns from sheet

I have a master sheet, but out of this sheet I only need certain columns to be displayed. Doing it manually is taking a long time and this worksheet is something I have to do once in a fortnight.
Can anyone please suggest VBA code to do this?
Let's say you need to delete columns numbers 13, 58 and 101, then do as follows:
Sub DeleteColumns()
Columns(101).EntireColumn.Delete
Columns(58).EntireColumn.Delete
Columns(13).EntireColumn.Delete
End Sub
For keeping the original column numbers, make sure you delete them from the highest to the lowest. Note that columns are numbered from 1 (which is "A") onwards.
If you don't want to delete the columns, but just hide them, then use the Hidden property:
Sub HideColumns()
Columns(13).EntireColumn.Hidden = True
' ... etc.
End Sub
You could go as follows:
Sub ColumnsDelete()
Range("A1, E1, AH1").EntireColumn.Delete
End Sub
Sub ColumnsHide()
Range("A1, E1, AH1").EntireColumn.Hidden=True
End Sub
Where you simply have to type columns headers followed by any row number (I chose "1" for simplicity)

How to copy only formulas from one excel sheet which can dynamically grow to another sheet using macro

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