Solved

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

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

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 26

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 26

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 26

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

757 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

16 Experts available now in Live!

Get 1:1 Help Now