Copy and Paste Column Regardless of Worksheet Open - excel

I currently have 3 worksheets in my workbook ("SheetJS", "Sheet1", and "Sheet2").
I want the macro to copy and paste the values of Sheet1 from Column K to Column L.
My code works but copies and pastes the columns on whichever worksheet is open/active.
I want the macro to code and paste the values only in "Sheet1" regardless of which one of the worksheets is open.
Any tips would help.
Thanks!
Sub Copy_K_to_L_2()
With ThisWorkbook.Sheets("Sheet1")
Columns("K:K").Copy Destination:=Columns("L:L")
End With
End Sub

Try to edit your existing line:
Columns("K:K").Copy Destination:= Thisworkbook.sheets("Sheet1").Columns("L:L")

Related

Excel VBA Copy and Paste loop accounting for blank cells

I am new to using VBA. I have an Excel document that has 500 sheets worth of data each sheet only has at most 45 rows and up to column W. I am trying to have a macro that copies the data from each of the 500 sheets and pastes it into a sheet named "Master". I am able to use the following code to perform this successfully.
Sub CopyPaste()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If Not wks.Name = "Master" Then
wks.Range("A1:W" & wks.Cells(Rows.Count, "A").End(xlUp).Row).Copy _
Destination:=Worksheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1)
End If
Next
End Sub
The issue I am running into is that some of the sheets have a row that is blank below the header before the data begins and as my code is written currently it seems like the macro thinks this is the end of the page and moves to the next sheet and is missing data that should be copied and pasted. I am looking for some assistance/guidance on how to account for these blank cells/rows so that the copy and pasting continue for the sheet through the end. Any help would be much appreciated, thank you.

Exact copy from 1 sheet to another

In excel 2010, I want to copy sheet 1, including all formatting and data, page setups, page breaks etc onto sheet2. In summary I want to do an exact copy of sheet1 on sheet2 through to sheet7. Ideally I want to insert a command click button on sheet1, so that once I have finished inputting data I can click the button and duplicate the information on each sheet. Any ideas?
Try this ,
Sub test()
Dim i As Integer
For i = 2 To 7
Sheets("Sheet1").Cells.Copy Sheets("Sheet" & i).Cells
Next i
End Sub
As an alternative solution I think you should create a button and assign the code below to it.
This will select all the sheets while you're on sheet1 and then whatever changes you make to sheet1 will automatically be available on all sheets instantly.
Sub test()
Dim ws As Worksheet
For Each ws In Sheets
If ws.Visible Then ws.Select
Next
End Sub

Run VBA Function in selected sheets

I need to write VBA code that will delete column B from all worksheets in the open workbook except for Sheet1. All these worksheets are on the right from Sheet1, and their names are as well stored as range in sheet1 in column AA. Moreover in each of these worksheets (apart from Sheet1) in column A have to be insert autonumbering (1.2.3. etc) beginning from cell A2 and going down. In each worksheet column headings are the same but number of rows is different depending on data included in each sheet. I dont know how to repeat this macro in every sheet.
You need to loop using the Worksheets collection contained in the ActiveWorkbook object:
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
If Not sheet.Name = "Sheet1" Then
Debug.Print sheet.Name
End If
Next
When you're inside that loop the sheet object is just a normal Worksheet object and you can do anything you would normally do to the ActiveSheet.

Excel: Open in the first empty cell

Is it possible to let Excel automatically select the first empty cell in column A, whenever I open the document?
I have got the following to find the the first empty line:
ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Value + 1.Count, 1).End(xlUp).Value + 1
In order to get Excel to select the first empty cell in column A when you open your workbook, you need to place the following code in the ThisWorkbook module.
Private Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
End Sub
This will select the first empty cell in the ActiveSheet. If you got multiple sheets in your workbook and you want to select the first empty row in a specific sheet, say Sheet1, you should change the second line of code to:
Set ws = ActiveWorkbook.Sheets("Sheet1")
You can do that.
You need write VBA(macro) program to realize.
Code you need is as follow
Private Sub Workbook_Open()
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select
End Sub
Meaning of code is:
"Private Sub Workbook_Open()" is predefined name subroutine which will be executed when the workbook be opened.
"ActiveWindow.Range("A65536").End(xlUp)" will find last cell with data in A column ("last cell")
"ActiveWindow.Range("A65536").End(xlUp).Offset(1,0)" will move to cell next to "last cell", that will be first blank cell.
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select will select tha first blank cell.
I assumed that you use Excel 2003 or older, OR number of rows with data in your worksheet is less than 65536.
If you use Excel 2007 or newer and you have rows with data in your worksheet more than 65536, please modify 65536 to the value large enough to cover rows in your worksheet.

VBA Excel: copy paste named range with hidden rows

Is there any smart way to copy paste values from a named range from one sheet to another not including hidden rows?
Suppose you have a named range called MyRange then you can copy visible cells only to another worksheet using:
Sub CopyNamedRange()
Range("MyRange").SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("Sheet2").Range("A1")
End Sub

Resources