Copy from cell range on 'sheet1' to same range on 'sheetB3' - vba

I had a hard time formulating the title, but I'll explain better here.
What I want to do is use a VBA-macro that I'm activating from a button click to copy the data found in the range B13:E52 on sheet1 to the same range on sheet"B3", where B3 is found on sheet1 containing the sheet name I want to copy to. This dynamically updates depending on what item is chosen in a list.
I know how to create my button, copy between sheet etc but I'm having a hard time figuring out how to reference the target sheet name that's contained in B3 for the target in VBA.

with sheet1
.range("b13:e52").copy sheets(.range("b3").value).range("b13")
end with

If you are looking to copy only the vlaues (without the formats or formulas), then you can use :
With Worksheets("Sheet1")
Worksheets(.Range("B3").Value).Range("B13:E52").Value = .Range("B13:E52").Value
End With

Related

Vlookup vba automatisation

I'm a beginner in VBA programmation, I want to create a button to help me search the price of a product from column A of sheet1 and to search for the price of that product from sheet2, column D of the same workbook.
The vlookup formula I use is:
=VLOOKUP(A2;sheet2!A2:G712;4)
My issue is that I have more than 100000 products and I want to use a button to simplify the process.
Change your top row formula to =VLOOKUP(A2;sheet2!$A$2:$G$712;4), select the cell you enetered the formula into, and then drag the formula down by using the little square in the bottom right hand corner of the cell you entered the formula into.
If you want to do it entirely in VBA, you can autofill using
Dim source As Range("A1")
Dim destination As Range("A1:A10")
source.AutoFill Destination:=destination
This will autofill from A1 to A10, as an example. You can also specify the autofill type with Type:
Dim source As Range("A1")
Dim destination As Range("A1:A10")
source.AutoFill Destination:=destination Type:=xlFillLinearTrend
The default type is xlFillDefault, which tries to find a pattern automatically and use the according fill type. Other types can be looked up here.

Macro to find and delete column based on cell value

I have two sheets in an Excel workbook. I would like a VBA code to search for the content of cell J19 in Sheet2 and delete the column with the matching cell in Sheet1. I have been searching all around Google to look for some VBA codes, but I haven't found anything yet that works.
Does anybody here know how to do this? I have zero experience in VBA.
You could try this code to perform such a task.
Sub FindMatchAndThenDeleteColumn()
FindContent = Worksheets("Sheet2").Range("J19")
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
If Cell.Value = FindContent Then Columns(Cell.Column).Delete
Next
End Sub
Put the code above in your workbook's module. Note that FindContent and Cell are randomly chosen here, you can use any names. I use
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
to loop through every cell that is in use in Sheet1. It will check everything in the range from cell A1 to the last cell with data (the bottom right-most cell). If the content of cell J19 is a text, you can declare the variable FindContent as a String type, i.e. Dim FindContent As String. If it's a number, you can declare it as a Long type or a Single type or any number type that fits the content. Here I don't declare it which means it defaulting to a Variant type. And since you're a beginner in VBA, you may learn it from Excel Easy. Hope this helps.

Update master workbook with another workbook

There is lots of questions about copying the content of one workbook to another that I'm aware of.
I would like to make a code that updates the workbook by importing the content from the other workbook without rewriting cells, just adding values. I want to make it that way in case the other workbook is corrupted or have some data deleted.
Some rows can be partially filled but the macro will fit the correct information in the right row.
Have you considered trying to copy over the workbook as a different name?
Imagine you start with
MasterWB -> sheet1
ChildWB -> sheet1
You copy over such that you have this worksheets.
MasterWB -> sheet1 , sheet2(childbook)
From there it is pretty easy to do a concat on a third page. First create a third sheet. Next, concat the values on sheet3 with values from sheet2 and 1
MasterWB.sheet3.cellA1.value = concat(sheet2.cellA1.value, sheet1.cellA1.value)
Sorry for the bad syntax, hope this helps!

How to highlight a cell when formula result from another sheet changes?

This is one that's been killing me and I've tried almost every solution on the Internet.
Here's background. I have an HR model that has each department broken out on separate tabs. I want to run an extract from our payroll system each payroll run and send highlight any updates individually. If someone's title or salary or status changes, I want to have that called out by highlighting the cell.
Each tab uses an INDEX/MATCH lookup to the extract tab to pull in the current information. What I want is if any value changes or is new(new hire, for example), highlight the cells.
I've played with Worksheet_Calculate and Worksheet_Change to no avail. Worksheet_Change doesn't fire because I'm not making the change directly on the sheet and Worksheet_Calculate doesn't have the Target object for to reference. I've tried the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range(Target.Dependents.Address)
If Not Intersect(updatedCell, Range("A:A")) Is Nothing Then
updatedCell.Interior.ColorIndex = 3
End If
End Sub
The range I actually need evaluated is A7:R104 but I've been trying to get anything to work when linked to another sheet.
This works fine if formula of target cell is pointing to another cell on same sheet. The moment you point to one on another sheet it doesn't work. I've tried most of the solutions on here with no success. I've even tried putting the Worksheet_Change on the extract sheet and see if I can trigger it that way with no luck.
Is there a recommended solution to triggering a change to a cell for a formula linked to another sheet?
so I just saw this post, I don't know if you've found the solution or are still looking, but:
if you select a cell in sheet 3, you can then go to the home tab, go to "conditional formatting" -highlight cell rules - more rules (at the bottom) - and "use formulas to determine which cells to format" and then put your cursor in the formula box. now, select a cell in sheet 1 (click the sheet1 tab, and click a cell) and you'll notice it should populate the address for sheet1, and the cell u selected. now type <> after that cells address, then select sheet2 and a cell. then click format, and choose a fill color. then ok. if you go to conditional formatting and manage rules it will show there the rule / formula and which cells it applies to.
doing this i was able to select cell D10 in sheet 3, and make it an ugly green if cells in sheet1 and 2 didnt match (I picked which cells) you can also select a range of cells.
thusly, you can apply this rule to whatever dells you want, and if you record a macro of you setting this conditional formatting, you can manitpulate that macro to apply it to a bunch of different cells, and change the ranges. (using loops / variables)

Excel Named Range resolving to its actual target

This is a weird problem I'm seeing in Excel
Say I have a named range MY_RANGE
which points to cell $A$1 (or A1)
If I then enter =MY_RANGE into cell A2 all looks good
If I then try to edit cell A2 I see =$A$1 rather than =MY_RANGE
If I enter =MY_RANGE on a seperate sheet in the same workbook
everything works as expected.
I'm guessing there may be some property on the sheet that controls this behaviour but I can't find it. The problem I'm having is that when I copy & paste the cell it copies the resolved reference rather than the named range
I've kind of figured out a solution...
I've written a macro to do the following
Copy the formula (as a string, so "=MY_RANGE") into a different sheet at the same relative location (so cell 3,3 on SheetA to cell 3,3 on SheetB)
Copy the cell on SheetB back to the Cell on SheetA
I still have absolutely no idea what is causing this odd problem but this is a reasonable workaround for me at the moment