VBA Copying text from one worksheet to active worksheet - vba

Sorry for the basic question, but much googling only gave me complicated answers. On the click of a button I am making a copy of another worksheet, I need to copy some text from the first worksheet to this newly created worksheet on the same button click (who's name may differ I.e. sheet1 (1), sheet1 (2). I'm sure it's very simple referencing the active sheet, help appreciated.
Private Sub CommandButton2_Click()
ActiveWorkbook.Sheets("AUTHORITIES VISIT").Copy _
after:=ActiveWorkbook.Sheets("CREATE REPORT")
End Sub

You can also have a look at the macro recorder and try it by your own.
This code should work for you:
Sub test()
Sheets("AUTHORITIES VISIT").Copy After:=Sheets("CREATE REPORT")
ActiveSheet.Cells(14, 3).Value = Sheets("CREATE REPORT").Cells(8, 2)
End Sub
As I understand from your comments above it copies the sheet "AUTHORITIES VISIT" after the sheet "CREATE REPORT" and copy from "CREATE REPORT" cell "B8" into the new sheet cell "C14"

Related

Use a part of macro code independently

I have one doubt!
I have a code for consolidation of 22 sheets in a workbook.
Now I want to prepare separate buttons for each sheet so that if the user wants they can consolidate only sheets required by them and not all 22 sheets.
I only know the way by creating 22 separate module having part of the codes related to each sheet. Is there any other concise way to do it which do not make me prepare 22 separate modules?
Code example:
sheets("AT").select
"whatever code that was required"
Sheets("DE").select
"whatever code that was required"
and so on....
Do let me know if question is not clear.
You only need a single module. When you click a button on a sheet it will be on the activesheet. Therefore you will only need to act on the activesheet and not every sheet in the workbook.
If I was doing the job I would create a userform with a List of worksheet names that the user can select and then the module will step through each selected worksheet name in the list and do whatever actions you need. A checkbox for whole workbook action would also be useful.
Make a new Userform with a commandbutton and also a list box called "myListBox" and ensure that the MultiSelect property is set to multi and not single then add the following code. This will step through each sheet in the workbook and adds the name to the listbox. Once you select a number of names and click the command button it will print the selected names to the Immediate window
Private Sub UserForm_Initialize()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
Me.myListBox.AddItem wks.Name
Next
End Sub
Private Sub CommandButton1_Click()
For i = 0 To myListBox.ListCount - 1
If myListBox.Selected(i) Then
Debug.Print myListBox.List(i)
End If
Next i
End Sub

Activating a different worksheet using VBA does not change the focus on it permanently

I want to select and modify different worksheets programmatically every time the workbook is saved. At the end however, I want to set the focus on a particular worksheet so that the workbook is saved with that particular worksheet in focus. What I'm noticing is that whenever the code executes it activates the worksheets, modifies them but at the end it goes back to the worksheet that I had selected before running the code.
Here's my code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets(1).Activate
Debug.Print Sheets(1).Name
End Sub
The code above is executed in an empty, local workbook with 2 empty worksheets
Sheet1 and Sheet2. Whenever I save the workbook with Sheet2 selected, I see that it is indeed activated because the console log prints Sheet1, in the workbook however, the selected worksheet remains Sheet2.I'm using SAP's BusinessObjects Analysis but as noted above, the workbook is a local macro-enabled workbook that is not saved on the SAP NetWeaver platform.
Is it possible for me to permanetly set the focus to a different worksheet so that it's visible in the workbook?
Thanks
EDIT:
Oh no!!! I have the annoying problem of inconsistent behavior with the different save buttons once again and that is yet to be resolved! I just realized that if I save through the workbook save button the sheet permanently changes, however when I save through the code editor it doesn't. The previous problem I had experienced was on workbooks saved on SAP NetWeaver where VBA code is not executed through the workbook save button but is, through the code editor save button. I guess I will have to log an Oss with SAP for this inconsistency.
What you are saying is only possible, if someone has written:
Private Sub Worksheet_Activate()
Sheets(2).Activate
End Sub
At Worksheets(1).
Otherwise, the code you are using:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets(1).Activate
Debug.Print Sheets(1).Name
End Sub
should activate the first Sheet and it should not be changed later.
I have no idea where Sheet2 is compared to Sheet1.
You say
The code (above) is executed in an empty, local workbook with 2 empty
worksheets Sheet1 and Sheet2. Whenever I save the workbook with
Sheet2 selected, I see that it is indeed activated because the console
log prints Sheet1, in the workbook however, the selected worksheet
remains Sheet2
Your code doesn't do anything to a sheet called Sheet2. It only looks at the first sheet in the tab order - the sheet could be called anything.
It activates the first sheet and then puts the name of the first sheet in the immediate window.
This code will select the sheet with the tab name Sheet2, it will then put the name of the activesheet (Sheet2) in cell A1 of the sheet with the tab name Sheet1.
Finally it selects the sheet with the codename Sheet3 (The codename is the name not in brackets in the Project Explorer).
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
With ThisWorkbook
.Worksheets("Sheet2").Select
.Worksheets("Sheet1").Range("A1") = ActiveSheet.Name
End With
Sheet3.Select
End Sub
Just use following code:
Sub activateSheet(sheetname As String)
'activates sheet of specific name you want.
Worksheets(sheetname).Activate
End Sub
then for select another sheet:
Sub activateSheet(sheetname As String)
'selects sheet of specific name you want.
Sheets(sheetname).Select
End Sub
Regards
Xsi

Macro for button to today's sheet in excel

I would like to know if it is possible to make a macro link to open a sheet named "Today()"? I have a Workbook with sheets named according to dates along with a "Home" sheet and "Month End" sheet. There is a Macro which copies the last sheet to create a new sheet for today with balance values from yesterday's sheet. I want to add a button on each sheet that would revert me back to the sheet named with today's date.
Here is my code
Sub TodaySheet()
Dim FindName As String, FindSheet As Worksheet
For Each FindSheet In ActiveWorkbook.Worksheets
If FindSheet.Name = "Today()" Then
FindSheet.Activate
Exit Sub
End If
Next
End Sub
Hope I'm on the right track?
You do not need to iterate through the worksheets, although it will work. You can ".Activate" the desired one directly, in one statement.

Dialog box that selects a worksheet in Excel

I'm trying to build a macro that will duplicate a worksheet from one workbook into a worksheet in another workbook. Is there a way I can use VBA code to allow me to manually select which worksheet I shall be duplicating?
Right now the macro works, as long as I have the full worksheet name typed into the actual VBA code. Ideally, I'd like the macro to allow me to select the worksheet through a dialog box. I know you can just copy/paste the sheet or its contents, but the guys I'm working for don't want to do that, due to the size.
You can use Worksheets collection to populate the ListBox in a user form. This should get you started:
Code in the user form (!):
Private Sub UserForm_Initialize()
Dim v As Worksheet
For Each v In Worksheets
UserForm1.lstWorksheets.AddItem v.Name
Next
End Sub
Private Sub cmdSelectWorksheet_Click()
MsgBox "You selected " & lstWorksheets.Value
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