Solved

using VBA to find the very last used cell and use this cell ref in formula

Posted on 2014-04-20
12
225 Views
Last Modified: 2014-05-09
Dear Experts:

Below is a rather simple formula which is filled into cell B4 by means of a macro.

Works just fine, but there is one thing I would like to get adjusted:

The second cell reference in this formula 'N42' is not fixed but dynamic.

It could be 'N42' in one case, in another computation case it is 'N17'.

The column letter stays the same but the row index can change. In all of the cases the dynamic cell reference is always the very last used cell in the column 'N' (Range("N65536").End(xlUp).Select))

So how should the below macro be tweaked to accommodate this requirement?

Help is much appreciated.  Thank you very much in advance. Regards, Andreas

Sub ShowDiscrepancies()

Dim strFormula As Variant

 strFormula = _
        "=IF(N2<>N42,""A total of "" & "" "" &N2-N42& "" Products have not been properly counted!"")"
ActiveWorkbook.Sheets("Results").Range("B4").Formula = strFormula

End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 6
  • 4
12 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40011071
Does this do what you want:
Sub ShowDiscrepancies()

    Dim strFormula As Variant

    strFormula = _
    "=IF(N2<>INDIRECT(CONCATENATE(""N"",IF(ISERROR(MATCH(9E+99+306,N:N)),MATCH(""*"",N:N,-1),IF(ISERROR(MATCH(""*"",N:N,-1)),MATCH(9E+99+306,N:N),MAX(MATCH(9E+99+306,N:N),MATCH(" * ",N:N,-1)))))),""A total of ""&"" ""&N2-INDIRECT(CONCATENATE(""N"",IF(ISERROR(MATCH(9E+99+306,N:N)),MATCH(""*"",N:N,-1),IF(ISERROR(MATCH(""*"",N:N,-1)),MATCH(9E+99+306,N:N),MAX(MATCH(9E+99+306,N:N),MATCH(""*"",N:N,-1))))))&"" Products have not been properly counted!"")"
    ActiveWorkbook.Sheets("Results").Range("B4").Formula = strFormula

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 40011094
Dear MacroShadow,

first of all thank you very much for your swift and professional help.

wow what a formula, incredible, but I am afraid to tell you that the macro does not work. It throws an error message '13' Type mismatch.

Regards, Andreas
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40011220
This is the formula, please try inserting the formula directly in the cell, if it works we'll see where the vba version went bad.
=IF(N2<>INDIRECT(CONCATENATE("N",IF(ISERROR(MATCH(9E+99+306,N:N)),MATCH("*",N:N,-1),IF(ISERROR(MATCH("*",N:N,-1)),MATCH(9E+99+306,N:N),MAX(MATCH(9E+99+306,N:N),MATCH("*",N:N,-1)))))),"A total of "&" "&N2-INDIRECT(CONCATENATE("N",IF(ISERROR(MATCH(9E+99+306,N:N)),MATCH("*",N:N,-1),IF(ISERROR(MATCH("*",N:N,-1)),MATCH(9E+99+306,N:N),MAX(MATCH(9E+99+306,N:N),MATCH("*",N:N,-1))))))&" Products have not been properly counted!")

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 40013279
Dear MacroShadow:

I will have a look at your solution tomorrow when I will be at my working place. There I got an English Excel Version.

In the meantime I found my own solution to my problem. Line 8 and 12 are the lines that did the trick for me. Nevertheless I will try out what you furnished.

Thank you very much again,

Andreas



Sub ShowDiscrepancies()
Dim x As String
Dim LastRow As Long
Dim strFormula As Variant
Dim strSheet As String

strSheet = ActiveWorkbook.Sheets("2_Auswertung").Name
LastRow = ActiveSheet.Range("N65000").End(xlUp).Row


strFormula = _
"=IF('" & strSheet & "'!$N$2<>$N$" & LastRow & ",'" & strSheet & "'!$N$2-$N$" & LastRow & "& "" Instrumente sind nicht mit Firmennamen versehen!"")"
ActiveWorkbook.Sheets("2_Auswertung").Range("R2").Formula = strFormula

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 40047410
I've requested that this question be closed as follows:

Accepted answer: 0 points for AndreasHermle's comment #a40013279

for the following reason:

Hi MacroShadow:

great job, thank you very much for your great help. Works just wonderfully.

Regards, Andreas
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AndreasHermle
ID: 40047113
Uuuups, my fault, I was gonna accept MacroShadow's answer (40011220). I inadvertently graded my own question. I am really sorry.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40047411
Uuuups, my fault, I was gonna accept MacroShadow's answer (40011220). I inadvertently graded my own question. I am really sorry.
0
 

Author Comment

by:AndreasHermle
ID: 40049844
I've requested that this question be closed as follows:

Accepted answer: 0 points for AndreasHermle's comment #a40013279

for the following reason:

Dear MacroShadow:

great job, works like a charm. Thank you very much for your great help.

Regards, Andreas
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40049845
Uuuups, my fault, I was gonna accept MacroShadow's answer (40011220). I inadvertently graded my own question. I am really sorry.

Dear MacroShadow:

great job, works like a charm. Thank you very much for your great help.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 40053410
Dear MacroShadow,

Finally my grade. Thank you very much for your great help. Works like a charm.

Regards, Andreas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now