Copy cells to active worksheet from a defined RANGE of SHEETS - vba

I need to copy a particular cell value to the current activesheet from worksheets between a predefined worksheet range. For example only the sheets between Sheet5 and Sheet7. I cannot specify the worksheet names as they do not yet exist. They will be created by the user via another macro upon request and placed within this predefined worksheet range upon creation.
In essence, I think I need to somehow define a Workbook Range between Sheet5 and Sheet7. Coincidentally, the new sheets will ALWAYS be within the sheet range of sheet 5 to 7.

You can use the sheet index such as
Sub SheetIndx()
If Sheets.Count >= 7 Then
ActiveSheet.Range("A1:B4").Copy Sheets(6).Cells(Sheets(6).Rows.Count, "A").End(xlUp).Offset(1)
End If
End Sub
Or the actual sheet6, regardless of that the name of the sheet is, it is still sheet6 and doesn't matter what order it is in the workbook.
Sub Sheet()
If Sheets.Count >= 7 Then
ActiveSheet.Range("A1:B4").Copy Sheet6.Cells(Sheet6.Rows.Count, "A").End(xlUp).Offset(1)
End If
End Sub

Related

Copy and paste data from different worksheet to another worksheet in a same excel spreadsheet

Sub Save7()
Dim NextRow As Range
Set NextRow = Range("AC" & Sheets("Sheet1").UsedRange.Rows.Count)
Sheet3.Range("AC14:AG14").Copy
Sheet1.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
My purpose of this code is to copy data ( Five columns of 'NO' in AC14 to AG14) from sheet 3 and paste to sheet 1 where the last active cell is at.
The code above is working well, however I made some modification to the sheet tab name for sheet 1. Sheet 1 is now called "Equipment stuffs", while sheet 3 name is remaining unchanged.
After those changes, the macro stopped working. The cause is probably because I don't know how to declare "Equipment stuffs" in the code .
There's no need to do copy/paste to move data from one place on the spreadsheet to another. You should simply assign the Value of the respective Range objects, for example:
Sheet1.Range("NamedRange2").Value = Sheet1.Range("NamedRange2").Value
Also, use code names for the sheets, instead of Sheets("SheetName"), and defined named for the ranges, instead of Range("AC14:AG14", otherwise your code will stop working if the user renames the sheet or inserts or deletes any rows above your reference.
If you want to automate this a little you could collect the active workbook and loop through each sheet using wb.Worksheets. Then collect the name with targetSheet.Name.
Option Explicit
Public Sub getSheet()
Dim wb As Workbook
Dim targetSheet As Worksheet
Set wb = ActiveWorkbook
For Each targetSheet In wb.Worksheets
Debug.Print targetSheet.Name
Next targetSheet
End Sub
I’m brazilian hehe, I understood your question , I’ve a code for alter the data in same worksheet (I’ll attach it here), for you to change the data in another worksheet, you need put on:
Worksheets("NameWorkSheet) Activate
for the VBA that’s refers to this tab.

How to use range object with a worksheet object

I am writing a simple program to merge cells in different sheet.
I wrote this code
Dim ws as worksheet
Set worksheet = sheets ("sheets2")
ws.range (cells (1,1),cells (1,5)).merge
This is not working and error comes saying method range of object worksheet failed
Please help me with this
Thank you
Try like this:
Option Explicit
Sub TestMe()
Dim ws As Worksheet
Set ws = Sheets("sheets2")
With ws
.Range(.Cells(1, 1), .Cells(1, 5)).Merge
End With
End Sub
The range object is a bit difficult in Excel and when you are referencing to cells you should also reference to the sheet.
Take a look at the Microsoft examples, they are good:
https://msdn.microsoft.com/en-us/library/office/ff838238.aspx
Especially the third note:
Be aware that the period in front of each occurrence of the Cells property. The period is required if the result of the preceding With statement is to be applied to the Cells property—in this case, to indicate that the cells are on worksheet one (without the period, the Cells property would return cells on the active sheet).

Copy and moving an entire sheet to another workbook. 1mil rows to 65536 rows

The following is part of my code that involves copying an entire named sheet from one master file to a new unsaved file that's being worked on:
ActiveWorkbook.Sheets("VehicleList").Copy _
After:=Workbooks(2).Sheets(1)
So this worked fine to place the sheet into workbook 2 but now the files we're dealing with are in old excel mode which is throwing up the following error due to old excel having less rows:
"Excel cannot insert the sheets into the destination workbook, because it contains fewer rows and columns"
How can I tweak the copy and pasting into Workbooks(2) without breaking the code? I thought defining a range of 1000 rows to copy and move would work, but this also gave an error. Thanks for your help.
Assuming you just want the values (i.e. I'm speeding it up by not doing a copy paste) you can do:
Workbooks(2).Sheets.Add After:=Sheets(1)
Workbooks(2).Sheets(2).Range("A1:F1000").Value = ActiveWorkbook.Sheets("VehicleList").Range("A1:F1000").Value
I'd go like follows
Option Explicit
Sub main()
Dim targetWs As Worksheet
With Workbooks("MyWorkbookname").Sheets("VehicleList") '<--| fully qualify reference wanted worksheet in the wanted workbook
Set targetWs = SetOrGetSheet(Workbooks(2), .name) '<--| get the (new) target worksheet on the target workbook named after referenced sheet
Intersect(.Range("A1:F1000"), .UsedRange).Copy targetWs.Cells(1, 1) '<--| copy range to target worksheet on the target workbook
If targetWs.name <> .name Then MsgBox "a new sheet has been created in " & targetWs.Parent.name & " with name " & targetWs.name
End With
End Sub
Function SetOrGetSheet(targetWb As Workbook, shtName As String) As Worksheet
targetWb.Worksheets.Add '<--| add a new sheet in the target workbook
On Error Resume Next
Set SetOrGetSheet = targetWb.Worksheets(shtName) '<--| try and get any possible target workbook sheet with the passed name
If SetOrGetSheet Is Nothing Then targetWb.ActiveSheet.name = shtName '<--| if target workbook has no worksheet with passed name then name the new one after it
Set SetOrGetSheet = targetWb.ActiveSheet 'return the new worksheet
End Function
should you be afraid your range-to-copy could exceed 65 rows and/or 256 columns than you should add its size check
edit for values pasting only
should you be interested in pasting values only then you can go like follows:
Sub main()
SetOrGetSheet(Workbooks(2), "VehicleList").Range("A1:F1000").value = ActiveWorkbook.Sheets("VehicleList").Range("A1:F1000").value '<--| copy values form source to target worksheet
End Sub
while SetOrGetSheet() function stays the same as above
as you may have guessed that function is there for a more general approach where you may want (or just have) to handle the possibilty of target workbook having a worksheet named after "VehicleList" already

VBA script to copy a column in the next sheet and paste to the next empty column in the first sheet

I’d need to write a short VBA script for the following situation. The workbook contains multiple sheets. The first sheet is the summary sheet. After the summary sheet there is an irregular number of sheets that contain the information I would like to display on the summary sheet. The information is always in “Column B”. The script should copy “Column B” of each sheet and paste it to the next empty column in the summary sheet. In other words, it should copy “Column B” in “Sheet 2” and paste it to the next empty column in “Sheet 1”, then copy “Column B” in “Sheet 3” and paste again to the next empty column in “Sheet 1”, etc. etc.
All the help is appreciated, thank you!
You will need to declare a Worksheet type variable (e.g. Dim ws As Worksheet), to loop by a For Each ws In Activeworkbook.Worksheets ... Next ws. Then you need to define which sheet is the master sheet. I recommend once again a Worksheet, e.g. Set wsMain = ActiveWorkbook.Worksheets("Summary").
To complete it use a
wsMain.Cells.Find("*",SearchOrder:=xlByColumns,SearchDirection:=xlPrevious).Column
to tell you the index of the last used column in your summary sheet. You can either store it in a variable then increase it by one in your loop or run it in your loop (not resource-efficient but who cares about that two milliseconds).
You can reference columns by index which in your case will be ws.Cells.Columns(2) and wsMain.Cells.Columns(LastColumn + 1).
Have fun writing your code, I hope I could help!
This worked:
Sub NextWsToNextEmptyColumn()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Columns(2).Copy Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
Next ws
End Sub

when executing macros/vba how can they be applied on merged cells?

I have the following question: A member from this forum yesterday provided a very good solution to my problem. I have two similar excel workbooks with more than 100 sheets in each, with the same sheet names. I wanted VBA to copy the data from sheet A in workbook A to sheet B in workbook B.
Sub GetDate()
Dim workbookA As Workbook, workbookB As Workbook, sheetA As Worksheet, sheetB As Worksheet, ws As Worksheet, sheetName As String
Set workbookA = Workbooks("clients.xls")
Set workbookB = Workbooks("KYC.xls")
'Loop through each worksheet in Book A
For Each ws In workbookB.Worksheets
Set sheetA = workbookA.Worksheets(ws.Name)
Set sheetB = workbookB.Worksheets(ws.Name)
sheetA.Range("C5").Copy sheetB.Range("E31")
Next
End Sub
I want to use it agan to get another values from another cell and insert it on workbookB on the approprate sheet each time. The problem is that on workbook A the cell I want to copy is a merged cell and so is the destination cell. I tried to give a name to each of the merged cells but I can't seem to make it. Can someone help if it can be solved?
thank you
Try
sheetA.Range("C5").MergeArea.Copy ' copies the merged cells that include C5
sheetB.Range("E31").PasteSpecial ' pastes what was copied into E31 and any merged cells it is part of