I have a workbook where i have the following formula in cell D3.
"=VLOOKUP($C3,'[NIGHT ROTA.xlsx]15'!$A$5:$I$32,D$1,0)"
The source workbook has 52 sheets and when i want to search a certain sheet i have to change the sheetname manually and then run the code below to copy it across the cells in my workbook using worksheet change.
Is there a way to have the sheetname as a variable that i would have in a cell in my workbook or that would be chosen from a drop down? I know i could use INDIRECT but don't want to have to open the source workbook.
Sub WeekChange()
Range("D3").Select
Selection.Copy
Range("D3:I26").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Sub
You can put the sheet names in a drop-down cell, say E1 (or any of your choice) and capture the change in this cell:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("E1").Address Then
Range("D3:I26").Formula = "=VLOOKUP($C3,'[NIGHT ROTA.xlsx]" & Target.Value & "'!$A$5:$I$32,D$1,0)"
End If
End Sub
To populate the drop-down box in E1, you can eventually set it upon the opening of the workbook, but since you dont want to open it (I guess you have some reasons), you might have to populate the validation of E1 manually.
Related
I have an Export to sheet button but I can't get it working correctly.
I selects the correct cells to copy but can't then transpose them on to selected sheet that appears in the drop down box in cell A1, I then also need it to paste on the next available row in that specific sheet. The problem is that I can't just list the sheets in VBA as the list in the drop down box changes. I have tried several ways to with no success. If someone could help it would be great
Sub Button2_Click()
Worksheets("Sheet1").Range("a2:x2").Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1!A1").Range("a:x")
End Sub
Here is some more code I have tried for the issue but still does not seem to work.
Sub ExportButton1()
'
' ExportButton1 Macro
' Exports Data to staff sheet from drop down box
'
' Keyboard Shortcut: Ctrl+e
'
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click(ByVal Target As Range)
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Data")
'On Error Resume Next
'If Not (Application.Intersect(Range("H2"), Target) Is Nothing) Then _
Set pasteSheet = Worksheets(ActiveSheet.Range("H2"))
copySheet.Range("G5:AA5").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
you need to change the sheet name in your worksheet function.
This might work.
Sub Button2_Click()
'Copying the Data
Worksheets("Sheet1").Range("a2:x2").Copy
'Pasting the data
' What we were missing was to pass the name of the tab dynamically. Now this code will pick up the name that appears in the Cell A1.
ActiveSheet.Paste Destination:=Worksheets(Worksheets("Sheet1").Range("A1").value).Range("A1")
End Sub
Also in the paste range you only need to put the first cell range to paste values.
To paste the Transposed Values check the function PasteSpecial with Transpose property set to True.
'I have found another way around the problem to copy paste cells to certain sheet then on 'staff sheet a formula in a table to tests column A value on data sheet for name and only 'transpose those rows
Sub Macro1()
Range("A2:J2").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
End Sub
'Formula on staff sheet is -=IF(Sheet3!A:A="Column1",Sheet3!$B:$B,"")
new to this forum and hoping someone can help!
I'm creating a database of resources for a project. I have created a sheet called 'Summary' to enter details of the resource (Organisation, type, location, contact details etc). The list is organised vertically and has a few drop-down data validation fields.
see summary
What I'm trying to do is attach a macro to the 'Add to database' button that will copy the data from the 'Summary' sheet to one of the other worksheets based on the value in the drop-down (cell: E4) and transpose this horizontally and then clear the field.
I've created individual Macro's to perform this (9 altogether) such as:
Sub EMPTADD()
Application.ScreenUpdating = False
Range("E2:E19").Select
Selection.Copy
Sheets("Employability & Training").Activate
Range("A750").End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("Summary").Activate
Range("E2:E19").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("E2").Select
Application.ScreenUpdating = True
End Sub
I just can't work out where to go from there. Can I use an IF function to determin if E4 hold a specific value and call the relevant Macro? Any help much appreciated :)
You don't need to utilize the activate/selection type commands. VBA is capable of working much more dynamically than that. Using activate/selection type commands can also open your coding up to a number of unintended consequences. Sometimes what you think is active, and what excel thinks is active, can be two different things. It's best practice to fully qualify your ranges so there is never any doubt.
As far as coding goes, it looks like you're trying to manually replicate how you would go about clicking on specific ranges and manipulating the data (maybe basing it off of recording a macro?). We can use VBA more dynamically than that! I would use something like this:
Sub CopyPasteData()
'Declare your variables
Dim wb As Workbook
Dim criteria As String
Dim i As Long
Application.ScreenUpdating = False
'Set values for your variables.
Set wb = ThisWorkbook
criteria = wb.Sheets("Summary").Range("E4")
i = wb.Sheets(criteria).Range("A" & Rows.Count).End(xlUp).Row
'Tell excel where to copy and paste your data
wb.Sheets("Summary").Range("E1").Copy
wb.Sheets(criteria).Range("A" & i + 1).PasteSpecial xlPasteValues
wb.Sheets("Summary").Range("E2").Copy
wb.Sheets(criteria).Range("B" & i + 1).PasteSpecial xlPasteValues
wb.Sheets("Summary").Range("E3").Copy
wb.Sheets(criteria).Range("C" & i + 1).PasteSpecial xlPasteValues
wb.Sheets("Summary").Range("E4").Copy
wb.Sheets(criteria).Range("D" & i + 1).PasteSpecial xlPasteValues
Application.ScreenUpdating = True
End Sub
If you use variables to set some of your values, you'll be able to use one macro for all of your Workbooks. No If statement will be necessary! The wb variable creates a shortcut for declaring which workbook the macro should operate in. The criteria value is set to whatever you have entered into cell E4 on the SUMMARY worksheet. As long as your Worksheet names match your dropdown list in E4, this will tell excel which Worksheet to put your data in. The i variable counts the number of rows that contain data (and that way we can paste new data at the bottom of those rows).
Then the last bit of code just tells Excel where to copy data, and where to paste it. Adjust the ranges as needed to do what you need it to.
this may be very simple but I am not able to work around it.
I could free up the column from where I am copying data by using Application.CutCopyMode=False but the column where data is pasted is still remain selected, I tried ActiveSheet.EnableSelection = xlNone as suggested in one of the forum without any success rest of my code working fine:
Dim wb As Workbook
Dim t1 As Worksheet
Dim r As Worksheet
Set wb = ActiveWorkbook
Set t1 = Sheets("dataT1")
Set r = Sheets("HPV1report")
t1.Range("D3:CR5").Copy
r.Range("A2").PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=True
Selection.EntireColumn.AutoFit
End sub
Other thing I can think of is selecting Cell A1 so the column will get free but I will prefer to use select if i can.
You can not really un-select a cell, because it is in the nature of a worksheet that there is always a selected cell.
The only reason why there might be no selected cell is
you select an object like a control/button, etc.
or to protect the worksheet against user interaction like #moosli pointed out.
So the correct way to go is, as you already found out:
Select any other cell, eg. A1.
Worksheets("MySheet").Range("A1").Select
You can achieve it in this way
Application.ScreenUpdating = False
Worksheets("name sheet selected data").Activate
Worksheets("name sheet selected data").Range("A1").Select
Worksheets("name sheet you want goto").Activate
Application.ScreenUpdating = True
As PEH pointed out, there is always something selected in Excel, so he proposes to select a hardcoded cell like A1. I prefer to store the cell that is active before the copy/paste process and reselect this cell after pasting.
Dim ActCell As Excel.Range
Set ActCell = Excel.Application.ActiveCell
'Do the copy and paste process
Excel.Application.CutCopyMode = Excel.XlCutCopyMode.xlCut
ActCell.Select
I have a cell (S1) that has a dynamic value in it on 'sheet 1' on 'sheet 2' I would like to take the value of (S1) and copy the state of the (S1) cell - to the second sheet but retaining the value before it was changed. I would like this to be done dynamically is there a way?
With vba you can get the value like the following, just link the macro to a button:
Worksheets(2).Range("s2").value = worksheets(1).Range("S1").value
Or you can cut and paste special, values only
If you prefer using vba you need to paste the code to sheet1 ( code section).
When the value in cell S1 of Sheet1 changes then Worksheet_Change event will be called and it will copy the new value of the cell (it does not retain the old value). This will be be helpful if you want to create a log of all the values for S1 Sheet1.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Application.EnableEvents = False
Dim rng As Range
Set rng = Range("S1")
If Not Intersect(Target, rng) Is Nothing Then
' Sheets("Sheet2").Range("S1") = Target.Value
Sheets("Sheet2").Range("S" & Sheets("Sheet2").Range("S" & Rows.Count).End(xlUp).Row + 1) = Target.Value
End If
Application.EnableEvents = True
End Sub
Try this:
Sheets("Sheet1").Range("S1").Copy
Sheets("Sheet2").Range("S1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
This is a simple way of transferring value of S1 from Sheet1 to S1 of Sheet2.
Im not sure how your Sheet1-S1 value changes but you can put this code in between the transition.
Update: I was able to solve this problem. I just remembered VBA use * and ? to present the string 'If Sheet.name Like "*CopyA"'
I would like to make a copy special value with original format of the sources of all sheets contains the word: "CopyA" for example "Apple CopyA","Mango CopyA"
I can use the macro record to see how VBA create sheet and copy special like similar example below.
I am having trouble in the searching for sheet name - All examples I found either I must know exact name (same as Macro record) or I have to make a copy of all sheets (loop though all sheet and copy, no matter the name is).
Sub Macro1()
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Sheets(1)
Sheets("Sheet1(2)").Select
Cells.Select
Selection.Copy
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
How can I search for sheet's name? In this case, any sheet that has "CopyA"?
Another way :) You can also use Instr(). For example
For all sheets in the workbook, use this
Option Explicit
Sub Macro1()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If InStr(1, ws.Name, "CopyA") Then
'~~ > Your code
End If
Next
End Sub
For ActiveSheet use this
Option Explicit
Sub Macro1()
If InStr(1, ActiveSheet.Name, "CopyA") Then
'~~ > Your code
End If
End Sub