Run VBA Function in selected sheets - excel

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.

Related

Referencing a range on another sheet in vba code to copy a formula down to the last row of data

I have a workbook with two sheets. On sheet1 I have a column (A) for which I need to copy the formula in A2 down for as many rows as there are in sheet2 column (B).Currently I use the following code
Range("B3:B" & Cells(Rows.Count,"C").End(xlUp).Row).Formula = Range ("B2")
to do something similar but where the range referred to is on the same worksheet. What I need to know is how to adapt this code to replace the "C" above with the range on sheet 2. Can someone please help?
you could use:
With Sheet1 ' reference Sheet1 worksheet
.Range("B3:B" & Sheet2.Cells(Rows.Count, "B").End(xlUp).Row).Formula = .Range("B2").Formula
End With
to copy sheet1 cell B2 formula down as many rows as Sheet2 column B last not empty cell row index
For every Range(), Cells(), Rows() and Columns() object specify in which sheet they are. Don't miss any of them or your code can randomly fail.
Example
Worksheets("Sheet1").Range("A1") 'addresses cell A1 in Sheet1
You can use variables as references:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Range("A1") 'now also addresses cell A1 in Sheet1
while
Range("A1")
will address the sheet that has focus (is on top) while the code runs. Don't rely on this because this can easily change by a single mouse click of the user. Therefore always specify which sheet you mean.

Copy and Paste Column Regardless of Worksheet Open

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")

How to copy data to multiple sheets from first excel sheet in a workbook

I want to copy cells A103 to Y148 from first sheet to every sheet in my excel workbook. What code I should use?
Here is a four-step approach.
1. Design the action in plain words, like,
' loop through rows 103 to 148 in Sheet1
' copy the row
' loop through all other sheets in the workbook
' paste the row in the first empty row at the bottom of each
Research code for each action. For example, google for "VBA Excel loop through rows"
Be amazed at the multitude of ready-to-use code you will find, and at how quickly you will learn to do impossible things.
Come back here with any problems you can't solve on your own.
Depending on what you mean by 'every sheet in my excel workbook', this may be faster than looping a paste operation through all of the remaining worksheets.
Sub galumph()
Dim w As Long, wss As String, wsa as Variant
For w = 2 To Worksheets.Count
wss = wss & IIf(CBool(Len(wss)), ";", vbNullString) & Worksheets(w).Name
Next w
wsa = Split(wss, ";") 'make array of worksheet names
Worksheets(1).Range("A103:Y148").Copy 'copy from A103:Y148 on Sheet1
Worksheets(wsa).Select 'select all of the remaining worksheets
Worksheets(wsa(0)).Range("A1").Activate 'activate the destination on one of the worksheets
Worksheets(wsa(0)).Paste 'paste the data into all worksheets
End Sub

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.

How to copy a cell value from one excel file and paste it to another excel file?

I would like to copy a cell value from one workbook to another, but the problem is that the cell values are from different sheets of the workbook. How can I do this? Could you help me with this?
I'm guessing you want a formula, and not instructions on copy and paste.
Cell references can be made using the following format.
=[Workbook Name]SheetName!CellAddress
Example, you have two workbooks open, Book1 and Book2.
Book1, Sheet1, Cell A1 = =[Book2]Sheet1!A1
Book2, Sheet1, Cell A1 = Hello World
The value of [Book1]Sheet1!A1 will now be "Hello World" (it will always reflect the value that is in [Book2]Sheet1!A1, providing the book is open)
If you do want a copy of the actual value, and not a reference to it, you will need to just use plain copy and paste or use the above formulas and when you're done use copy+paste special (paste values) to convert the formulas to their values.
Update
Just realised it was tagged excel-vba so you'd need to use something like #DavidZemens' answer.
You could also use the macro recorder to do this, but doing it manually like David suggests will result in nicer code that's easier to re-use/modify later on.
This is the bones of what you need, you can do a lot more with looping over ranges of cells, sheets in workbooks, etc., but fundamentally you will need to specify in VBA what is the target workbook, worksheet, and cell range, and then set that .Value equal to a specific workbook/worksheet/cell .Value in another workbook.
I use Workbook and Worksheet variables in this example, you can also use range variables for the cell(s) if you desire or if you need to apply to multiple cells.
Sub TransferValues()
Dim wb1 as Workbook
Dim wb2 as Workbook
Dim ws1 as Worksheet
Dim ws2 as Worksheet
Set wb1 = Workbooks("First Workbook.xlsx") '<modify as needed'
Set ws1 = wb1.Sheets("The sheet name") '<modify as needed'
Set wb2 = Workbooks("Another Workbook.xlsx") '<modify as needed'
Set ws2 = wb2.Sheets("Another sheet name") '<modify as needed'
'This line puts the value from wb1.ws1 in wb2.ws2, cells specified:
ws2.Range("A1").Value = ws1.Range("A1").Value
End Sub

Resources