Solved

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

Posted on 2014-04-20
12
245 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

740 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