VBA anchoring specific cell??

Jeevz_87

New Member
Joined
Sep 21, 2021
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi All,


I'm still new to VBA but have since been experimenting with it for a piece of work I'm doing. I'm trying to add a row to the bottom of my table and have succeeded to an extent, however the formula it copies down when it adds the row becomes anchored using the "$" for some reason.

The code is as follows;

SubButton53_Click()

Dim ws As Worksheet
Dim tbl As ListObject

Set ws = ActiveSheet
Set tbl = ws.ListObjects("Table Name")

tbl.ListsRows.Add

End Sub


When I run the Macro it copies all formats and formulas down but it seems to anchor a cell in one of my formulas from;

=IF(ISTEXT(D89),$F$5,"") <----- This is what I wanted to see when the row was added but I got.....

=IF(ISTEXT($D$89),$F$5,"")

Is any one able to tell me whether the VBA code is correct and if not, what could i do instead please?

Many thanks,


Jeevz

Disclaimer: I cannot post the workbook as the data is commercially sensitive.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Welcome to the Board!

Can you make a copy and replace the sensitive data with generic data, and paste the last few rows of your table using the tool mentioned below (which will allow us to see the exisitng formulas)?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hi @Joe4

Apologies for the long wait!

I've managed to create an excel template with all information removed but leaves the table and the macro for you to review. As you can see from the VBA code, it seems to copy the last row in the table which is fine but it doesn't copy the formatting down, nor does it insert within the table and it seems to copy 2 rows at a time for some reason.

Please see the mini sheet below and a couple of screenshots with macro (just because it doesnt show in the minisheet);

Macro test 1.xlsx
ABCDEFGHIJKLMNO
1
2
3[Select Business Unit]
4
5
6NoBusiness UnitProject nameOfficesClientSectorValue (£)OpportunityStart On SiteDuration (months)Year 1Year 2Year 3Consolidation (Balance)Check
70 [Select Offices]#N/A#N/A--0-#REF!
80 [Select Offices]#N/A#N/A--0-#REF!
90 [Select Offices]#N/A#N/A--0-#REF!
100 [Select Offices]#N/A#N/A--0-#REF!
110 [Select Offices]#N/A#N/A--0-#REF!
120 [Select Offices]#N/A#N/A--0-#REF!
130 [Select Offices]#N/A[Select Sector]--0-#REF!
140 [Select Offices]#N/A[Select Sector]--0-#REF!
150 [Select Offices]#N/A[Select Sector]--0-#REF!
160 [Select Offices]#N/A[Select Sector]--0-#REF!
170 [Select Offices]#N/A[Select Sector]--0-#REF!
180 [Select Offices]#N/A[Select Sector]--0-#REF!
190 [Select Offices]#N/A[Select Sector]--0-#REF!
200 [Select Offices]#N/A[Select Sector]--0-#REF!
210 [Select Offices]#N/A[Select Sector]--0-#REF!
220 [Select Offices]#N/A[Select Sector]--0-#REF!
230 [Select Offices]#N/A[Select Sector]--0-#REF!
240 [Select Offices]#N/A[Select Sector]--0#REF!
25 #N/A#REF!
26 #N/A#REF!
27 #N/A#REF!
28 #N/A#REF!
29 #N/A#REF!
30 #N/A#REF!
Sheet2
Cell Formulas
RangeFormula
F7:F12F7=INDEX('[RTR_Master_Template v5.3.xlsm]Data'!$AO$3:$AO$603,MATCH([@No],'[RTR_Master_Template v5.3.xlsm]Data'!$AB$3:$AB$603,0))
B7:B12B7=IF(ISTEXT(C7),#REF!,"")
B13:B19B13=IF(ISTEXT(C13),$D$3,"")
B20:B24B20=IF(ISTEXT(#REF!),$D$3,"")
B25:B30B25=IF(ISTEXT($C$7),$D$3,"")
E7:E30E7=INDEX('[RTR_Master_Template v5.3.xlsm]Data'!$AE$3:$AE$603,MATCH(A7,'[RTR_Master_Template v5.3.xlsm]Data'!$AB$3:$AB$603,0))
O7:O30O7=#REF!-SUM($K7+$L7+$M7+$N7)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
O28Cell Valuebetween 1 and 9.99999999999999E+30textNO
O28Cell Valuebetween -9.99999999999999E+41 and -1textNO
A28Cell ValueduplicatestextNO
O29Cell Valuebetween 1 and 9.99999999999999E+30textNO
O29Cell Valuebetween -9.99999999999999E+41 and -1textNO
A29Cell ValueduplicatestextNO
O30Cell Valuebetween 1 and 9.99999999999999E+30textNO
O30Cell Valuebetween -9.99999999999999E+41 and -1textNO
A30Cell ValueduplicatestextNO
O7:O27Cell Valuebetween 1 and 9.99999999999999E+30textNO
O7:O27Cell Valuebetween -9.99999999999999E+41 and -1textNO
A7:A9Cell ValueduplicatestextNO
A10:A27Cell ValueduplicatestextNO
Cells with Data Validation
CellAllowCriteria
D7:D30List=IF(#REF!="LSS",LSS_list,IF(#REF!="NS",NS_list,IF(#REF!="EM",EM_list,IF(#REF!="KP",KP_list,IF(#REF!="KME",KME_list,IF(#REF!="WW",WW_list,IF(#REF!="DB",DaBS_list,IF(#REF!="KIN",K_int))))))))


1632912620046.png


1632912713359.png
 
Upvote 0
Oops! Sorry I sent the wrong VBA screenshot, please use the VBA code in the first post in this thread?
 
Upvote 0
Really odd, I am having trouble setting this up. Admittedly, I seldom use Tables in Excel, and I never use VBA to control tables, so I don't have a lot of experience in that realm.
However, I cannot even get your code to run. I set up a table and tried running the code to add the new row, and I get a 438 error on the row that adds the new row to the table.
So far, my Google searches as to why have proven fruitless.

If you are able to upload a copy of your file to some file sharing site (i.e. OneDrive, DropBox, etc), I would be willing to download it and take a look at it.
If you are able to do that, just be sure to remove any "sensitive" data first, and provide a link to that file in this thread here.
 
Upvote 0
Ah thats annoying! Ive just made a blank copy of the updated template with a VBA code I know that will work which Ive enclosed on the Google drive link below;


All sensitive info has been removed (i hope ?)
 
Upvote 0
OK, the file you updated doesn't seem to have any of the formulas in it, nor does it have the VBA code you posted in your original thread.
So the two things we want to test are not there.
 
Upvote 0
Okay, hmm I don't seem to be getting this right on my side.

My apologies, the user requirements changed in the last week and i think what i posted as a mini table is an older table. I have resent the latest file in the link below with the latest formulas and VBA codes underneath;


Sub Button78_Click()

ActiveCell.Offset(1, 0).EntireRow.Insert
ActiveCell.EntireRow.Copy ActiveCell.Offset(1, 0).EntireRow

End Sub

Macro Test 2.xlsm
ADEFGHIJKLMNOPQRSTUVWX
1
2
3Choose a Business Unit…London, South & Strategic Projects
4
5
6CRM NoProject nameOfficesClientClient TypeFrameworkSectorProcurement TypeTender Value (£)Variations (£)Projected Final Account (£)Opportunity Category Tender Notification DateTender Submission DateDate Secured (Contract Signed)Anticipated Start On SiteProject Duration (months)21/22 (£000s)22/23 (£000s)23/24 (£000s)FY25+ (Balance)Check
70Project 1[Select Offices]Client 1Client 1F1CustodialType 110,00010,000On-Hand0-10,000
80Project 2[Select Offices]Client 2Client 2F2CustodialType 220,00020,000On-Hand0-20,000
90Project 3[Select Offices]Client 3Client 3F3CustodialType 330,00030,000On-Hand0-30,000
100Project 4[Select Offices]Client 4Client 4F4CustodialType 440,00040,000On-Hand0-40,000
110Project 5[Select Offices]Client 5Client 5F5CustodialType 550,00050,000On-Hand0-50,000
120Project 6[Select Offices]Client 6Client 6F6CustodialType 660,00060,000On-Hand0-60,000
130Project 7[Select Offices]Client 7Client 7F7CustodialType 770,00070,000On-Hand0-70,000
140Project 8[Select Offices]Client 8Client 8F8CustodialType 180,00080,000On-Hand0-80,000
150Project 9[Select Offices]Client 9Client 9F9CustodialType 290,00090,000On-Hand0-90,000
160Project 10[Select Offices]Client 10Client 10F10CustodialType 3100,000100,000On-Hand0-100,000
170Project 11[Select Offices]Client 11Client 11F11CustodialType 4110,000110,000On-Hand0-110,000
180Project 12[Select Offices]Client 12Client 12F12CustodialType 5120,000120,000On-Hand0-120,000
190Project 13[Select Offices]Client 13Client 13F13CustodialType 6130,000130,000On-Hand0-130,000
200Project 14[Select Offices]Client 14Client 14F14CustodialType 7140,000140,000New in Year0-140,000
210Project 15[Select Offices]Client 15Client 15F15CustodialType 1150,000150,000New in Year0-150,000
220Project 16[Select Offices]Client 16Client 1F16DefenceType 2160,000160,000New in Year0-160,000
230Project 17[Select Offices]Client 17Client 2F17DefenceType 3170,000170,000New in Year0-170,000
240Project 18[Select Offices]Client 18Client 3F18DefenceType 4180,000180,000New in Year0-180,000
250Project 19[Select Offices]Client 19Client 4F19DefenceType 5190,000190,000New in Year0-190,000
260Project 20[Select Offices]Client 20Client 5F20DefenceType 6200,000200,000New in Year0-200,000
270Project 21[Select Offices]Client 21Client 6F21DefenceType 7210,000210,000New in Year0-210,000
280Project 22[Select Offices]Client 22Client 7F22DefenceType 1220,000220,000New in Year0-220,000
290Project 23[Select Offices]Client 23Client 8F23DefenceType 2230,000230,000New in Year0-230,000
300Project 24[Select Offices]Client 24Client 9F24DefenceType 3240,000240,000New in Year0-240,000
310Project 25[Select Offices]Client 25Client 10F25DefenceType 4250,000250,000New in Year0-250,000
320Project 26[Select Offices]Client 26Client 11F26DefenceType 5260,000260,000New in Year0-260,000
Master Input
Cell Formulas
RangeFormula
M7:M32M7=[@[Tender Value (£)]]+[@[Variations (£)]]
X7:X32X7=$M7-SUM($T7+$U7+$V7+$W7)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A9:A84Cell ValueduplicatestextNO
A7:A8Cell ValueduplicatestextNO
X7:X102Cell Valuebetween 1 and 9.99999999999999E+30textNO
X7:X102Cell Valuebetween -9.99999999999999E+41 and -1textNO
Cells with Data Validation
CellAllowCriteria
E3List=Data!$B$4:$B$12
N7:N32List=Data!$A$4:$A$12
E7:E32List=IF(C7="LSS",LSS_list,IF(C7="NS",NS_list,IF(C7="EM",EM_list,IF(C7="KP",KP_list,IF(C7="KME",KME_list,IF(C7="WW",WW_list,IF(C7="DB",DaBS_list,IF(C7="KIN",K_int))))))))
G7:G110List=Data!$W$3:$W$18
H7:H131List=Data!$T$3:$T$72
I7:I110List=Data!$C$4:$C$12
J7:J110List=Data!$Y$3:$Y$10


1632931615833.png



1632931760401.png



I do hope I've posted it correctly this time. Do let me know, I really do need help with this...

Many thanks
 
Upvote 0
OK, so now there is NO formula like you showed in your original question:
When I run the Macro it copies all formats and formulas down but it seems to anchor a cell in one of my formulas from;

=IF(ISTEXT(D89),$F$5,"") <----- This is what I wanted to see when the row was added but I got.....

=IF(ISTEXT($D$89),$F$5,"")

So I am not even sure what it is you are asking now. You seemed to have changed so much that your original question does not even apply.

One thing I did see. It does not cause a problem, but is an unnecessary redundancy. You can either use SUM or +, They both do virtually the same thing (depending upon how they are used), so it usually is not necessary to apply both to the same range.

So this formula:
Excel Formula:
=$M101-SUM($T101+$U101+$V101+$W101)
should either be:
Excel Formula:
=$M101-SUM($T101:$W101)
or
Excel Formula:
=$M101-($T101+$U101+$V101+$W101)

Like I said, what you wrote will not give the wrong result, but it is confusing and unnecessary (kind of like a "double positive").
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top