vba: Do same code in all sheets - vba

Simple question. The following code works fine:
Dim hoja as worksheet
For Each hoja In Workbooks("Origen.xlsx").Worksheets
Msgbox hoja.name
next
However, the following code does NOT work. Can anybody say why and make it work?
Dim hoja as worksheet
For Each hoja In Workbooks("Origen.xlsx").Worksheets
Range("a1:a2").copy Destination:=Range("a3:a4")
next
The second code only does the copy/paste thing in one of the sheets, but not in all.
Please can you help? I'm so frustrated with such a simple thing.
Thanks!

The Range object, if not qualified, always refers to active sheet.
If you want to copy paste each time on different sheet, use the code below:
Dim hoja as worksheet
For Each hoja In Workbooks("Origen.xlsx").Worksheets
hoja.Range("a1:a2").copy Destination:=hoja.Range("a3:a4")
next

Try hoja.Range instead of simply Range.

Related

Excel macro modification ( or maybe VBA ) to update the last included worksheet

Situation
Trying to copy a range of cells that include formulas from worksheet called "Sheet1" to the rest of other worksheets I found that I could do it performing "Fill Across Worksheets". It worked fine, so my next step was to record a Macro for it to be more efficient and worked just fine too.
The Problem
The problem is that when I include a new worksheet and run the Macro, the Macro does not consider the new worksheet so this last worksheet doesn't get updated.
I am including below the code created by the macro. In it I can see that it's including only the worksheets I have now in the workbook, so this is where I need help.
( My excel is in Spanish so when you read Ctrl+Mayus+Q, Mayus means Shiftkey )
Help
What I need is a way to modify this Macro so when it runs it will check and update all worksheets. Or, maybe it's because a Macro can't do this I may need a VBA code ? If this VBA is the way to resolve it, can you help me here with this ?
I appreciate all help
Thank you
Javier
Sub Macro2()
'
' Macro2 Macro
'
' Acceso directo: Ctrl+Mayús+Q
'
Range("A5:D12").Select
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
Sheets("Sheet1").Activate
ActiveWindow.SelectedSheets.FillAcrossSheets Range:=Selection,
Type:=xlAll
Sheets("Sheet1").Select
End Sub
This is a work around, concerning that you want all the worksheets to have the value of the first worksheet in range A5:D12:
Sub TestMe()
Dim ws As Worksheet
Dim selAddress As String
selAddress = "A5:D12"
For Each ws In Worksheets
'ws.Range(selAddress).Value2 = Worksheets(1).Range(selAddress).Value2
ws.Range(selAddress).Formula = Worksheets(1).Range(selAddress).Formula
Next ws
End Sub
See How to avoid using Select in Excel VBA.

Copy / paste same range of cells of many sheets from one book to another

I'm rather new to VBA and I'm trying hard to solve the following problem. I usually get an excel file with 10-20 sheets. Then, I copy the same range of cells (i76:i133) of every sheet from one book to another book that has the same sheet structure.
I'm trying to make a loop to code this easily but I'm failing.
Sub copy()
Dim Sourcebook As Workbook
Dim Destinationbook As Workbook, mysheet As Worksheet
Set Sourcebook = Workbooks("Quarterly.xlsx")
Set Destinationbook = Workbooks("Master.xlsx")
For Each mysheet In Sourcebook.Worksheets
Sourcebook.Sheets(mysheet).Range("I76:I133").Copy
Destinationbook.Sheets(mysheet).Range("I76").Paste
Next
End Sub
I get a
run-time error 13, type mismatch
in Sourcebook.Sheets(mysheet).Range("I76:I133").Copy
Any help would be very welcome!
Your mySheet variable is of Worksheet type while you are trying to use it as a String. Here is your loop improved:
For Each mysheet In Sourcebook.Worksheets
'Sourcebook.Sheets(mysheet).Range("I76:I133").Copy 'instead of this...
mysheet.Range("I76:I133").Copy '...use this
Destinationbook.Sheets(mysheet.Name).Range("I76").Paste
Next

Copy Row A1 from Sheet 1 into Row A1 Sheet 2

I basically just need to know how to copy a header from sheet one that goes from A1-O1 into sheet two, three, four, five and so on...they all have the same header. Sheet one is on the right and sheet two is left and increases to the left. I tried this which I found on some website but it says object required. The error is Runtime Error 424
mainworkBook.Sheets(“Sheet1”).Rows(1).EntireRow.Copy
mainworkBook.Sheets(“Sheet2”).Range(“A1”).Select
mainworkBook.Sheets(“Sheet2”).Paste
A small loop code.let me know if it works.
Sub COPYPASTeHEADER()
Dim K As Integer
For K = 2 To ActiveWorkbook.Sheets.Count
Sheets("All_Data").Range("A1:O1").COPY Sheets(K).Range("A1")
Next
End Sub
You can do something like this instead of using Select:
For Each Sheet In ThisWorkbook.Sheets
ThisWorkbook.Sheets("ALL_DATA").Rows(1).Copy Destination:=Worksheets(Sheet.Name).Range("A1")
Next
This will loop through each sheet in your workbook, take the range you provided (row 1 from Sheet1), and paste it to each sheet by referencing the Name property of each Sheet you are looping through.
The error may have been from the workbook variable, as that is the only thing that is unclear.
I would also recommend looking into this post: How to avoid using Select in Excel VBA macros as it is tremendously helpful in avoiding Select/Activate when possible, which is a common occurrence among those who learn VBA through recording Macros.
Let me know if it works for you.
This is an excellent place to use a loop. For each sheet in the workbook, paste the same header.
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call pasteContents(ws)
Next
End Sub
Sub pasteContents(ws as Worksheet)
** Your code goes here
End Sub
EDIT: The ** section could be as such:
Sub pasteContents(ws as Worksheet)
ActiveWorkbook.Sheets(“Sheet1”).Rows(1).EntireRow.Copy
ActiveWorkbook.Sheets(ws).Range(“A1”).Select
ActiveWorkbook.Sheets(ws).Paste
End Sub
Or it could also be...
Sub pasteContents(ws as Worksheet)
ActiveWorkbook.Sheets("Sheet1").Rows(1).Copy Destination:=Worksheets(ws).Range("A1")
End Sub

VBA to work on all sheets

I tried many different sample codes before posting this, but I can't get any of them to work.
I need this to run not just in the active sheet but on all sheets in my file.
On Error Resume Next
Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Hope someone is able to help.
You need to loop over the sheets in the workbook and manipulate each one individually:
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
sheet.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Next
The solution of Alex K. is OK, only check if the sheet is empty or not, otherwise you will have an error with "SpecialCells" :
Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets
If WorksheetFunction.CountA(sheet.Cells) <> 0 Then
sheet.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If
Next
End Sub

How to access a closed Excel Workbook using vlookup vba

I'm trying to create a Excel VBA macro that uses VLOOKUP to access a range of cells in a closed workbook. I'm not too good at using the VBA editor, but it doesn't seem to show a lot of useful information about errors.
Sub WorkBookWithData()
Dim currentWb As Workbook
Set currentWb = ThisWorkbook
Dim currentWs As Worksheet
Set currentWs = currentWb.Sheets(1)
Dim strFormula As String
strFormula = "=VLOOKUP(currentWs.Range("B2"),'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B222,2,false)"
currentWs.Range("C2").Formula = strFormula
End Sub
Excel VBA editor is hanging up on the "strFormula = "=VLOOKUP..." section.
Thanks
Reference from Siddharth Rout's comments.
The main problem in your code is this line:
strFormula = "=VLOOKUP(currentWs.Range("B2"),'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B222,2,false)"
because of this code currentWs.Range("B2"). We know that you want to indicate Range("B2") of Current Sheet(same sheet). So, you can use as follow:
strFormula = "=VLOOKUP(B2,'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B‌​222,2,false)"
Why? It can use just B2 because you set formula to a cell which is in the same sheet. So, it is not need to indicate the Sheet Name.
And If you want to set a cell which is from other sheet, you need to indicate Sheet Name in that case. So, should use as follow:
strFormula = "=VLOOKUP(" & currentWs.name & "!B2,'Macintosh HD:Users:myself:Documents:l[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!A1:B222,2,false)"
This looks nothing like what I had previously, but it works.
Sub Check_Master_Values()
Dim newCurWb As Workbook
Set newCurWb = Workbooks(2)
newCurWb.Activate
newCurWb.Sheets(1).Range("C2").Formula = "=VLOOKUP(B2,'Macintosh HD:Users:myself:Documents:[Master_Terms_Users.xlsm]Master_Terms_Users.csv'!$A$1:$B$269,2,FALSE)"
End Sub
In my first attempt, I didn't follow the chain of assignments from workbook, to sheets, to ranges. As you can see in this code, I Dim a new Workbook - then the big ah-ha moment, I needed to assign it to the correct open workbook. Then, I activated the workbook, and finally accessed the Sheets object and Range.
I also know now that my workbook selection number will vary depending on how many other workbooks are open. The ThisBook didn't work because somehow in the process, the workbook that ThisBook referenced, changed. That is probably also why my initial code didn't work, in addition to the improper coding in the VLOOKUP.
It would be good if there was a way to specify which workbook on the fly.
Thanks to everyone who gave help on the VLOOKUP part.