Differences between A1 fixed referencing and formulaR1C1 relative positioning VBA - excel

I have a macro that populates cells on a blank worksheet with values from another form worksheet using R1C1. The issue is that the position of the source data on the form worksheet changes as users add rows to input their data. The A1 solution to this would be to use a fix Cell column reference and allow the reference for the row to change 9ex. Cell=Sheet1!$A25, which could then change to Cell=Sheet1!$A26 as a row is added above. How can I make my FormulaR1C1 change in the same way as my A1?
Correct situation:
Cell A1=Sheet1!$F13
Therefore, ActiveCell.FormulaR1C1 = "=Sheet1!R[12]C6"
But when I add a row above F13:
Cell A1=Sheet1!$F14
But, ActiveCell.FormulaR1C1 = "=Sheet1!R[12]C6", Does not respond to the shift row down

Related

how do I get value from a sheet then show on another sheet in excel base on matching id

I have an ID and Name column in sheet A and in sheet B I have an ID column and an empty column
what I want to do is to fill the empty column with values from the Name column in sheet A where both the ID columns are matched
here is what I want to do in MySQL statement
select SheetA.Name from SheetA join SheetB on SheetA.id = SheetB.id
can someone help translate this to excel code?
Refer to screenshots when interpreting the ranges (which will require modification to suit your specific needs / Excel data list positioning)...
VB code:
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveCell.Formula2R1C1 = _
"=INDEX('67650244 (2)'!R4C3:R7C3,MATCH('67650244'!R4C2:R7C2,'67650244 (2)'!R4C2:R7C2,0))"
Range("C5").Select
ActiveWindow.SmallScroll Down:=-3
End Sub
Screenshots
Sheet 1 (entitled "67650244"):
Sheet 2 (entitled "67650244 (2)"):
This assumes the source values you're looking up from are contained with the 2nd sheet (i.e. "67650244 (2)"), and the index/match function "places" the relevant ID/lookups into Sheet 1 (i.e. "67650244"). Further, the "activecell" is simply cell "C4" in sheet 1 (the function in the VB code above will insert the response/'looked-up' values as an array to fill all values adjacent to those in column B of this sheet (assuming a successful lookup, if not, then it will display "#Value!" next to the relevant Lookup value that couldn't be found in sheet 2....
Note: VB using "Row/Column" referencing when using a function like this, where "R1C1! simply refers to cell A1 in excel. To decipher this more easily for writing up the applicable version of code for your case, you can go to "Tools, Options, Formula" in the left had pane (within Excel, not VB), and then select "R1C1 Reference Style" box, per screenshot below:
You can always change this back later (deselect the option) as desired the same way.
Note: it would be far easier / more convenient to just use the index(range to return, match(value to lookup, range to lookup, 0)) method within excel as opposed to within VB - but this depends on desired functionality of your setup/circumstances/excel model/etc.

vba offset with reference to cell

I need to copy a column of data to another column on a different worksheet, pasting values only. The appropriate paste column is identified in a single cell. This cell will be changed manually each time the macro is applied. So one time I might want to copy/paste in the first column, so my identifier cell is 1. The next time I might input 5 in this cell so that I offset 5 columns to the right to copy/paste data. Thank you.
You can reference the columns in a worksheet using the Columns property. You can achieve what I think you're trying to do with code like this.
Dim col As Integer
col = SomeSheet.Cells(1,1).Value
FromSheet.Columns(col).copy
ToSheet.Columns(col).PasteSpecial xlPasteValues

VBA: Insert number of rows based on cell value

Looking for a way to insert X rows above cell B9 in my workbook. The value of X will change and can be found in cell B4. This seems very simple, but I can't get this portion of my code to work.
ActiveSheet.Range("B9").Select
Selection.EntireRow.Insert.Cells("B4"), CopyOrigin:=xlFormatFromRightOrBelow
You can use Resize to do that, and you don't need to Select at all.
Range("B9").EntireRow.Resize(Range("B4").Value).Insert CopyOrigin:=xlFormatFromRightOrBelow
Also, you should avoid ActiveSheet — instead, fully qualify the sheet using Sheets("YourSheetName") or a variable referencing the sheet in question.

How can I make a macro to copy an entire column but change a single row number?

I have inherited a poorly designed workbook, and I am trying to make it work a bit better without starting from scratch.
The last problem I have is that I have a formula in a column that I need to copy to the next column, but change the row number referenced in the formula. The easiest thing to do would be to change the format of the workbook but that will cause an uprising by the users.
=IF((CommaSeparatedListContains(RTM!$I$8,ROW()-2))=TRUE,"X","")
The code above is what I need to copy, but I need to change it so that it looks at I9 instead of I8. RTM is the name of the sheet that the cell is on, and CommaSeparatedListContains is a macro that will return true if the referenced cell has a value (ROW()-2) in the comma delimited list.
Basically I need a macro to add a new column to the worksheet that works like the others, so that the end users who don't know how to use Excel can just click a button and add a column.
For example, that code is in cell A1, and I need to move it to B1 keeping the I the same but increment the row number. If I remove Both $ signs it would change it to J8, if I have $I8 it stays I8, and if I have $I$8 it stays I8.
Thanks for re-affirming my understanding. I'll give this a shot with providing some code, based on your string:
Dim LC as Long, i as Integer
Columns(9).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(1,10).Value="" 'Add your header
LC = Cells(1, Columns.Count).End(xlToLeft).Column
i = LC-1 'See description below code
Range("J2:J100").Formula="=IF((CommaSeparatedListContains(RTM!$I$" & i & ",ROW()-2))=TRUE,""X"","""")"
For i, you want to ensure that you substract the correct number... given the example of I8 being the cell you want to reference, and assuming that Column I is the last column of your sheet, then the 9th column, 8th row, is the cell you want to reference. So, the variable i = last column - 1, in this example.
In this case, the column is always added to the right of column I, the assumed last column in the sheet.
One other assumption is that you're using rows 2 to 100 for the range that you have the formula... So, Range("J2:J100").Formula will be affected by your actual range for the formula.

copying excel cells one by one and not range at once

I'm developing some excel macros, and now I'm stucked with following,
I want the macro getting the cells from another file and put it on the new one but it is important to consider that copy the full range is not an option, so for example first i Need to copy a1 then a2 , etc ...
the reason is because after each "paste" process, I have to check, the old value and then start a triger of another macro depending on both values, and if ai paste the full range at once it is not working,.
Try getting the Cells value at given row and column from the Worksheet objects you are manipulating.
Example : I want to copye the value (cell content only, not style) from worksheet1 cell A1 to worksheet2 cell B3.
worksheet2.Cells(3, 2).Value = worksheet1.Cells(1, 1).Value
Simple as that.

Resources