How do I change specific worksheet to current worksheet? - vba

I've a command button with a macro attached in workbook A that copies cells A2:B2 from any workbook in a folder and pastes them starting at cell B2 in the AMT worksheet also in workbook A.
I'd like to place a command button on every worksheet and Instead of always pasting to the AMT worksheet I'd like to paste to whichever worksheet the command button was clicked on.
This is how I've been transferring the data from one sheet to another so far but I'm not sure how to change AMT to worksheet macro was clicked on I've been warned about using .Select and .Activate but I'm not sure how else to do this, thanks.
'Transfer cells B2 & C2 from the results worksheet
With ws.Range("A2:B2")
ThisWorkbook.Worksheets("AMT").Range("B4").Offset(wbc, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With

If you don't want to do active sheet, you could set some cell to change, for example from a 0 to 1 after the click and then loop through each sheet to see which range was changed to pull the value you want.

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.

Excel cover sheet that is used to populate cells across the whole workbook

I am trying to create a cover sheet for an excel workbook, the user will populate these cells then a macro will paste them across all worksheets inside the workbook.
The cover sheet asks for information in cells from column D and column I which could be common across the whole workbook. I am trying to make filling out this workbook faster and easier.
I have a basic understanding of Excel VBA but require help to write this, thank you.
You code use something like this
Sub Main
Dim cellsToCopy As Range
Dim shtNames as Variant, shtName As Variant
Set cellsToCopy = Worksheets("CoverSheet").Range("D5, D11, I3, I12") '<--| change both "CoverSheet" name and its cells address list to fit your needs
shtNames = Array("firstSheet", "secondSheet", "thirdSheet") '<--| change sheets names to paste cellsToCopy values into
For Each shtName in shtNames
Worksheets(shtName).Range(cellsToCopy.Address).Value = cellsToCopy.Value
Next shtName
End Sub

Copy a range from excel to another workbook in a new worksheet

I'm very new to VBA macros and have taught myself some code but I'm struggling with my current piece of work and can't find the answer I am looking for.
I want to copy a range of cells (B3:N21) from one workbook to another "master" workbook - which seems simple enough - but I would like it to copy into a blank/new worksheet in the Master copy every time the Macro is run.
The range contains formulas, I would only need the values copied to the Master workbook.
Any help with this would be greatly appreciated.
Thanks
Worksheets("Sheet1").Range("C1:C5").Copy
Worksheets("Sheet2").activate
Worksheets("Sheet2").Range("D1:D5").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
End With
I think you only need paste special, this is an example
try this
Option Explicit
Sub main()
Dim masterWb As Workbook
Dim mySht As Worksheet
Set mySht = ThisWorkbook.ActiveSheet '<~~ assuming you're copying values from active worksheet of the workbook the macro resides in
' beware: if you start the macro while the active sheet is not the one you want, this will lead to unespected results
Set masterWb = Workbooks("Master") '<~~ Change "Master" with whatever name your master workbook must have
' beware: we're assuming "Master" workbook is already open, otherwise this line will throw an error
With masterWb.Worksheets.Add
.Range("B3:N21").Value = mySht.Range("B3:N21").Value
End With
End Sub
mind the comments
the code above can be reduce to a much less verbose (and self explanatory, too) one like follows
Sub main2()
Workbooks("Master").Worksheets.Add.Range("B3:N21").Value = ThisWorkbook.ActiveSheet.Range("B3:N21").Value
End Sub
where apply the same comments of the lengthy code, which is:
assuming you're copying values from active worksheet of the workbook the macro resides in
beware: if you start the macro while the active sheet is not the one you want, this will lead to unespected results
change "Master" with whatever name your master workbook must have
beware: we're assuming "Master" workbook is already open, otherwise an error would be thrown

Copy cells to active worksheet from a defined RANGE of SHEETS

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

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