I'm trying to create a repeated copy and paste of a table I have in one of my worksheets and assign it to buttons in my budget worksheet.
For example whenever you click on the contract button, it will install the new table. Click it again, it will leave a line break and insert the new table. Same thing with the variation button.
Budget worksheet
Table sample
My attempt in a related copied sample in one of the questions asked here.
I understand to copy and paste I've been told to use this code
Sub CopyPasteToAnotherSheet()
Worksheets("Dataset").Range("B2:F9").Copy Worksheets("CopyPaste").Range("B2")
End Sub
It works, but I can only copy it once. I don't know how to copy it numerous times each time I run the macro.
Welcome to stack!
So I think I understand the want, it's usually good to have a go and show us the code you have but as its the first time here's a start of a code that might do that:
Sub variations()
Dim rng As Range
Set rng = ActiveWorkbook.Sheets("Variations").Range("A1:B5")
Run copyAcross(rng)
End Sub
Sub contracts()
Dim rng As Range
Set rng = Worksheets("Contract").Range("A1:B2")
Run copyAcross(rng)
End Sub
Function copyAcross(rng As Range)
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Budget")
targetRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 2
ws.Cells(targetRow, "A").Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
End Function
this gives two subs that the buttons can be pointed to. In each sub you can define the range you want and the function will copy it across.
Note: for this to work column A in the Budget file would need at least some value somewhere in it, the first copy would then be put two rows below that
Related
I was looking to create dynamic information on a cell when you don't have a lot of space but would like to display it.
I wanted to use on a job sheet to let the user know what the job No. referred to if their not familiar with it.
I used the hyperlink method combined with vba to achieve self updating list.
This is the hover over I wanted.
https://superuser.com/questions/1296838/customize-hyperlink-hover-over
But i wanted it dynamically updated from a table where I could update the table and it would pull the information and add the tooltips as descriptions.
So the solution is this
Sub toolmicro()
'
' toolmicro Macro
Dim rng As Range: Set rng = Application.Range("Sheet1!C2:C30")
Dim cel As Range
For Each cel In rng.Cells
With cel
If cel.Value = "" Then
Else
Dim tooltiptoolmine As String
tooltiptoolmine = Application.WorksheetFunction.IfError(Application.VLookup(cel.Value, Sheet1.Range("A1:B3"), 2, False), "Add Description to job")
cel.Hyperlinks.Add Anchor:=cel, Address:="", ScreenTip:=tooltiptoolmine
End If
End With
Next cel
End Sub
The Range looks for a set of cells to add the if to you could make it a column like C:D.
It skips blanks
Has error handling if the description hasn't be written yet.
Applys the link based on vlookup.
Then to get it to automagically update as I add jobs to the list
This will look for a change on the worksheet and call the list update.
Private Sub Worksheet_Change(ByVal Target As Range)
Call toolmicro
End Sub
If anyone has a better way let me know.
This is a Follow up question to a previously "Solved" question.
Please go through the previous code to get the idea.
here: How to prevent a row from copying into another workbook when the range has no data while using 'LastRow' 'xlUp'
I have managed to successfully copy and paste a certain range (containing values) onto another workbook. Now to check if the data was entered, I use the 'Cltr+Down Arrow' button to goto the last cell with value. The problem is, since there are people using this who have little to no knowledge on computers, they accidentally press the wrong buttons which could erase a cell value. So I need to make a Macro to find the last range pasted.
I managed to use a MsgBox to show the last row column 'A's value.
Sub LastUsedRow_SpecialCells_1()
Dim LastRow As Long
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
MsgBox LastRow
End Sub
But I need the MsgBox to display the range as well since the code pastes multiple rows depending on the number of rows entered.
One more method I found was this
Sub test()
Dim r As Range, ary
Dim LastRow As String
LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Set r = Sheets("Sheet1").Range("A1:R1")
ary = Application.Transpose(Application.Transpose(r.Value))
MsgBox Join(ary, " ")
End Sub
But this only shows the first row as I have not defined the Last row function.. then again I need a range consisting of multiple rows.
I am trying to unhide a group of worksheets if they meet a certain condition. This uses a user form, triggered by a command button, with selection options and another command button. The expected behavior is that once the selection has been made and the button has been pressed, all worksheets meeting the criteria will be unhidden. The Target word is present at different locations along the first row and all cells before it are empty on that row. Ideally, the process will scan each cell in the first row of each worksheet in the workbook until it comes across the Target, unhide the worksheet, then move on to the next worksheet to start the process over again until all worksheets with the workbook have been checked.
Upon activation of the command button on the user form I have the following:
Private Sub ContinueCommand_Click()
Dim Valid As Boolean, wks As Worksheet, c As Integer, actCol As Long
actCol = ActiveSheet.Range("A1").End(xlToRight).Column
For Each wks In ActiveWorkbook.Worksheets
For c = 1 To actCol
If ActiveCell.Value = "Target" Then
wks.Visible = xlSheetVisible
Exit For
End If
Next c
Next wks
Valid = True
If Valid = True Then
Unload Me
End If
End Sub
I've borrowed from several sources, including here for using ActiveCell, determining if a value exists, unhidding worksheets, Finding values within a range, and searching for a string. Any help would be greatly appreciated.
As I said in my comments there are some issues with the way you've chosen to implement this.
Your For c = 1 To actCol loop is not needed. This can be easily seen because c is not really used anywhere in the loop.
Let's assume your Target value is in wks.Range("A100") (the 1st row and 100th column).
Your code would then perform the exact same operation 100 times and would come up with the exact same result. That's what leads you to use Exit For, which is a bad practice.
If I understood your initial post correctly, if Target exists in a particular worksheet, then all cells before Target are empty.
If that's the case, the Target will either be in wks.Range("A1") or in wks.Range("A1").End(xlToRight). If it's not in either of these two cells then it doesn't exist at all in this particular worksheet, which would mean that the 1st row is completely empty. You don't need to check any more cells apart from these two.
Your code does not check whether Target is in wks.Range("A1").
Also your use of Application.Match, makes me believe that you have probably been misled by the common misconception that wks.Range("A1").End(xlToRight) is a range of cells starting from A1 and extending all the way to the last non-empty cell in the 1st row.
The truth is that wks.Range("A1").End(xlToRight) is a single cell rather than a range of cells. Selecting A1 and then pressing CTRL+right arrow, will show you exactly which cell it is.
I might be missing something, but according to your description in the initial post, I would do something like the following:
Dim sht As Worksheet
For Each sht In ThisWorkbook.Worksheets
If sht.Range("A1").Value = "Target" Or sht.Range("A1").End(xlToRight).Value = "Target" Then
sht.Visible = xlSheetVisible
Else
MsgBox "target was not found in " & sht.Name
End If
Next sht
I want to thank BruceWayne, Scott Craner, Stavros Jon, Darell H whom all helped me get closer to this answer. The final result looked like this:
Private Sub ContinueCommand_Click()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
If Not IsError(Application.Match("Target", wks.Range("A1").End(xlToRight), 0)) Then
wks.Visible = xlSheetVisible
End If
Next wks
Unload Me
End Sub
If anyone in the future has issues getting this to work, let me know and I will post a more complete version.
a little stuck on this VBA. Havent done VBA in a while so might need to cut me some slack :)
Introduction
Working on a Doc that manages the skills and competencies of several employees in my organisation. All worksheets (with the exception of home have identical layout and cell organisation)
Problem Statement
I need a VBA that searches one particular cell (B2) for a factory location (i.e. York) if this cell = York, I then need it to copy and paste a list of values (i.e. B2) into a range of cells on another worksheet in the same workbook named "York". I then need this to loop through all worksheets in the doc (kinda like looping through all employees; each employee will have a new worksheet) for the cell value York and copy and paste those cells into the next empty row on the worksheet York. I cannot seem to get the loop to run through, but after writing the code I couldnt even get the copy paste function to work.
Anyone up for the challenge.
This is my current code. Very poor I know.
Sub testv2()
Dim ws As Worksheet
Dim sh As Worksheet
Dim rng As Range
Dim c As Range
Set ws = Sheets("MATRIX - MASTER")
"Matrix-master was template copy for all skills"
Set rng = ws.Range("$B$2")
For Each sh In Sheets
For Each c In rng.Cells
If rng.Value = "York" Then
ActiveWorkbook.Range("B1").Copy
Sheets("York").Select
ActiveWorkbook.Range("J6").Paste
End If
Next c
Next sh
End Sub
Any help at all would be appreciated!
I don't understand what you are tring to do.
rng is set to one cell of one sheet "$B$2" on "MATRIX - MASTER"
why do you loop it with for each?
I think you ment to write instead of: ActiveWorkbook.Range("J6").Paste
ActiveSheet.Range("B1").Copy ' source
ActiveWorkbook.Sheets("York").Range("J6").Paste ' target
range on the workbook level is used by mamed range.
Try to avoid Select... If you must, use Activate.
I'm tyring to look for a way to return a range of cells with just the lookup function. Is this really possible?
Basically like this:
=LOOKUP([MasterBook.xlsm]Sheet3!$A:$A,?)
I just want the function to look in the main workbook through all of Column A and return all cells for Column A that have something in them.
EDIT for poster below:
Sure. I have two workbooks; one workbook is essentially a local product that has a "master" sheet on top and then individual worksheets following it that have all of their information extracted from the master sheet. The second workbook is a local copy of a product that I send to a non-local entity higher up the food chain. Basically I need to pull information from the individual sheets in my first workbook and put it in the appropriate columns in the second workbook. I have a macro that gets the info from my sheets in the one workbook over to the other, but the second workbook is formatted differently. I was looking for a way to use a formula if possible.
The macro I am referring to is:
Sub CopyTest()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn = Workbooks("Local Workbook.xlsm").Worksheets("Sheet3").Columns("A")
Set targetColumn = Workbooks("Nonlocal Workbook.xlsm").Worksheets("Sheet1").Columns("A")
sourceColumn.Copy Destination:=targetColumn
End Sub
All this does is pull the specified column from one sheet and put it in the column on the second book; but it just pastes it starting in the first block. Since the non-local book is formatted differently, the column I need to transfer to doesn't start until Row 9. shrug I have ideas abuot what I'm trying to do with this, but my ideas tend to exceed my technical ability (which occasionally makes it difficult to explain). :)
Depending on how different your workbooks are formatted. Here is two way to handle this:
Adapt your macro
Instead of copying the whole column, you can copy paste, only the values you want to.
Here is an example:
Sub CopyTest()
Dim rSource As Range, rTarget As Range
Dim lEnd As Long
lEnd = Range("A65536").End(xlUp).Row
Set rSource = Workbooks("Local Workbook.xlsm").Worksheets("Sheet3").Range("A1:A" & lEnd)
Set rTarget = Workbooks("Nonlocal Workbook.xlsm").Worksheets("Sheet1").Range("A9")
rSource.Copy Destination:=rTarget
End Sub
Use a formula
If your data are not in the same order, you'd better use a VLOOKUP formula.
See how it works.
Don't hesitate to post another question with what you've built for some help. Please give as much details as possible so we could help you the best way.
[EDIT] Another try following the comments
Option Explicit
Dim wTarget As Workbook
Sub mainCopy()
Dim bGo As Boolean
bGo = True
'Add a new workbook to copy the data - do you want the user to select one?
Set wTarget = Application.Workbooks.Add()
Do While bGo
CopyTest
bGo = MsgBox("Do you want to import data from another workbook?", vbYesNo, "Continue?")
Loop
End Sub
Sub CopyTest()
Dim rSource As Range, rTarget As Range
Dim lEnd As Long, lCol As Long
Dim ws As Worksheet
Dim vFile As Variant
vFile = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile
For Each ws In ActiveWorkbook.Worksheets
'do you need to copy the columns separately?
' For lCol = 1 To 10
'find the last cell of the 10th column
lEnd = ws.Cells(65536, 10).End(xlUp).Row
Set rSource = ws.Range("A1:J" & lEnd)
'How can we define the target worksheet?
Set rTarget = wTarget.Worksheets("Sheet1").Range("A9")
rSource.Copy Destination:=rTarget
' Next lCol
Next ws
End Sub