?
Solved

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

Posted on 2014-04-20
12
Medium Priority
?
254 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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…

777 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