Solved

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

Posted on 2014-04-20
12
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
move line without macro or copy/paste 6 44
Lookup range formula 7 27
count values within multiple bands 7 33
Excel - IF criterion 2 22
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

739 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