get cell values of variable cells in messagebox - vba

I have a table with names and skills where 1 means "you dont have this skill" and 3 "you have this skill". When I change it from 1 to 3 you get a msgbox saying: are you sure you want to do this. I want the names and the skills in the msgbox, but don't know how.
so for example I change cell D12 from 1 to 3. The message should say:
Are you sure you want to change (value of cell D1 / the skill) of (value of cell A12 / the name of the person)
That's ok. But it is applicable on the active cell though, so the D1 and A12 change depending on which cell is active. How do I do this?

You can use R1C1 Reference Style. In this case you'll have to switch the settings of Excel to R1C1 Reference style:
In Office 2007, Click the Office button and click Excel Options which you will find at the end near Exit Excel.
Go to Formulas tab and under Working with formulas, Check R1C1 reference style to use it.
If you specify formula like =R[2]C[1] in A1 cell, it will return you a value from B3 cell.
The second way is to use the following formula:
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),2,1)
In this case you don't have to change any settings
EDIT:
I tried your example. If you want to achieve this by using VBA it's even easier:
Dim skillValue As String
skillValue = Range(Cells(1, Selection.Column), Cells(1, Selection.Column))
Dim nameValue As String
nameValue = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 1))
If MsgBox("Are you sure you want to change " & skillValue & " of " & nameValue & "?", vbOKCancel) = vbOK Then
...............

Related

To multiply column with textbox value in vba

I have textbox1 in excel sheet and list of values in column A. As textbox is user defined, I have to multiply given value with the all the values available in column A. How it can be done using command button if I am not using a form.
rng = Evaluate(rng.Address &"*TextBox1.Value")
But is giving an error #NAME? in excel.
I got the solution and it should be
Value = Textbox1.Value
rng = Evaluate("=" & rng.Address & "*" & value)
Hope it will be useful for someone
Thanks

Excel Formula =Ax&Bx&Cx&Dx... when merged cell appears

I'm working with big table with structure similar to picture below, but more columns(A-AZ) and really more rows. And for some reason I need to get whole row into one string, like you can see as "Expected result", but I'm getting really bad data. Do you know how to get right results? Without VBA, if possible
OK, I don't know how to close this question, but I'm closing it by this. I'm using merged cells, because it's required to use them, and everybody here yells, that merged cells are evil so this question is closed as unsolvable.
To expand on the comments from mattdeak and Brad, the issue is that cells A3:A6 are merged. As a result, the value "Lamp" is actually only in cell A3, and cells A4:A6 are blank. Therefore, J4 sees A4="" and B4="", giving you the indicated result (only the value in C4).
The easiest way, as the commenters noted, is to unmerge the cells and copy all the data into each row. If your cells have to stay merged, I recommend the approach used in this answer. Here's how it works:
Pick a space where you can add a second set of Table columns. I'll use CA since you said you have columns A-AZ.
In CA3, enter the formula =IF(ISBLANK(A3),CA2,A3).
Fill right from CA3 through DZ3. DZ3 should thus refer to AZ3.
Fill down CA3:DZ3 for as many rows as you have. At this point. CA3:DZ<last row> is a copy of your table with everything filled in.
Update the formula to be =CA3 & CB3 & ... for however many columns you need to merge. Use the values from CA:DZ and you should be OK!
If you can use a little VBA, you can create a User Function in a public module as follow:
Public Function MergedValue(r As Range) As Range
Application.Volatile
If Not r.MergeCells Then
Set MergedValue = r
Else
Set MergedValue = r.MergeArea.Cells(1, 1)
End If
End Function
Then replace all =Ax & Bx & Cx ... formulas by =MergedValue(Ax) & MergedValue(Bx) & MergedValue(Cx) ...
Edit: Added Application.Volatile at the start
Edit 2: Without using Application.Volatile
Public Function MergedValue(r As Range, RangeToCheck as range) As Range
If Not r.MergeCells Then
Set MergedValue = r
Else
Set MergedValue = r.MergeArea.Cells(1, 1)
End If
End Function
Call it with =MergedValue(A1,$A$1:$C$3) for example if you want to check cells in range [A1:C3].
=concat(offset(A3,-mod(row()+3,6),,4)) & concat(offset(B3, -mod(row()+1,2),,2)) & C3 & concat(offset(D3,-mod(row()+3,6),,4)) & concat(offset(E3, -mod(row()+1,2),,2)) & concat(offset(F3, -mod(row()+1,2),,2))
to get A column: in j3, -mod(row()+3,6) = 0. so concat(offset(A3,0,0,4)) = A3&A4&A5&A6 = A3.
to get B column: in j3, -mod(row()+1,2) = 0. so cancat(offset(B3,0,0,2)) = B3&B4 = B3.
And so on D, E, F column.
There is another method (writed on 2016/6/20)
step 1.Select A3 cell and press Ctrl + A.
step 2.Then press Ctrl + 1 (The "1" key with "!", Not the "1" in number pad)
step 3.Select "Alignment" tag, disable "Merge Cells"
step 4.Then "Home" -> "Find & Select" -> "Go to special" -> select "Blanks" (the short cut key sequence is: Alt -> h -> f -> d -> s -> k -> Enter)
step 5.The active cell be B4, then write =b3 and press ctrl + Enter
step 6.Then in J3 cell write down: =CONCATENATE(A3,B3,C3,D3,E3,F3) or =CONCAT(A3:F3)

Excel - how to increment formula reference along a row

I have really long strings of text saved in roughly 1000 cells down Column A.
I have created the following formula (some VBA included for FindN), placed in cell B1:
=MID($A1,FindN("978",$A1,1),13)
I can copy this formula down Column B just fine. However, I also want to copy this formula across each row, so for example the formulas for the cells across the row should be as follows:
Cell C1: =MID($A1,FindN("978",$A1,2),13)
Cell D1: =MID($A1,FindN("978",$A1,3),13)
Cell E1: =MID($A1,FindN("978",$A1,4),13)
etc...
If I copy the formula in Cell B1 across the row, it will copy across =MID($A1,FindN("978",$A1,1),13) - but I need the "1" to increment by 1 each time.
I think I'd need to adjust the formula slightly, but a bit lost on how to do this...
Any help would be greatly appreciated. Please let me know if I should clarify further.
Use COLUMN() - it gives the column number of the current cell. You can offset this as required.
In this case for your incrementing number use COLUMN() - 1, so that in B you have 1, C; 2 etc.
You need use CELL formula to get current column number. Try something like this:
=MID($A1,FindN("978",$A1,CELL("column";A1)+1),13)
I dont have English Excel and im not sure first argument in CELL forumla is "column"
Try this :
Sub Fill()
With Sheets("Sheet1")
For i = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
.Cells(i, 1).FormulaR1C1 = "=MID($A1,FindN(" & _
Chr(34) & "978" & Chr(34) & _
",$A1," & i - 1 & "),13)"
Next i
End With
End Sub

How to count up text of a different font colour in excel

I have a list of names that has been exported from another database into excel. The names in the list that are of interest are highlighted in red font. I would like a way to count it, i.e. John Smith appears 5 times in total in a column but 3 of the 5 times, his name comes up highlighted in red font. So I would like to see how many instances of his name comes up red.
I know How to search all instances of his name e.g. =COUNTIF(A1:A100,"John Smith ")
I've also had help in creating a VB function which counts all values that are red (=SumRed) (once the colour index is specified) in a worksheet by using this:
Function SumRed(MyRange As Range)
SumRed = 0
For Each cell In MyRange
If cell.Font.Color = 255 Then
SumRed = SumRed + cell.Value
End If
Next cell
End Function
I just can't find a way to combine the two counting conditions. Any help would be much appreciated!
You don't need VBA for this but still if you want VBA Solution then you can go with any of the other two answers. :)
We can use Excel formula to find the Font Color of a cell. See this example.
We will be using XL4 macros.
Open the Name Manager
Give a name. Say FontColor
Type this formula in Refers To =GET.CELL(24,OFFSET(INDIRECT("RC",FALSE),0,-1)) and click OK
Explanation of the formula
The Syntax is
GET.CELL(type_num, reference)
Type_num is a number that specifies what type of cell information you want.
reference is the cell reference
In the above formula the number 24 gives you the font color of the first character in the cell, as a number in the range 1 to 56. If font color is automatic, returns 0. And Hence the drawback. Ensure that the entire font color is red. We could have used 64 but that is not working properly.
OFFSET(INDIRECT("RC",FALSE),0,-1) refers to the immediate cell on the left.
Now enter this formula in a cell =IF(AND(Fontcolor=3,B1="John Smith"),1,0) and copy it down.
Note: The formula has to be entered on the Right of the cell which contains the Text.
Screentshot
EDIT (10/12/2013)
To count cells with specific backcolor see THIS link
I think you're almost there but this deserves another function #user bet me to the punch line :(
Function CoundRedAndText(MyRange As Range, Mytext as string) as long
CoundRedAndText = 0
For Each cell In MyRange
If cell.Font.Color = 255 and cell.value like MyText Then
CoundRedAndText = CoundRedAndText + 1 'you had cell.value but dont know why?
End If
Next cell
End Function
Usage, =CountRedAndText(A1:A25, "John Smith")
For Each cell In Range("A1:A100")
If cell.Font.Color = 255 And cell.Value = "John Smith" Then
myCount = myCount + 1
End If
Next

How to find the last value with specific conditions?

Sheet 1 column A has the following values (it has around 3000 records. I’ve given the below sample values). I need to find the last value of a specific text.
RVT-01
RVT-02
RVT-03
RVT-04
RVT-05
RVT-06
RHT-01
RHT-02
RHT-03
RHT-04
RHT-05
ROI-01
ROI-02
ROI-03
SWO-01
SWO-02
SWO-03
SOR-01
SOR-02
SOR-03
SOR-04
SOR-05
SOR-06
SOR-07
Using VBA code
If enter short tex in sheet1.cells(2,2) = SWO , I need the last value in sheet1.cells(2,4)=SWO-03
If I enter sheet1.cells(2,2) = RHT , I need the last value in sheet1.cells(2,4)=RHT-05
If I enter sheet1.cells(2,2) = RVT , I need the last value in sheet1.cells(2,4)=RVT-06
If I enter sheet1.cells(2,2) = SOR , I need the last value in sheet1.cells(2,4)=SOR-07
What would be the VBA code for the above process?
As Skip Intro suggested, there is no need for VBA: in Column B, put a formula like this:
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,RIGHT(TRIM(A:A),2),"") (to get the just the max number):
or
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,A:A,"") (to get the complete contents of the cell)
Both will show you the highest values. Then you could AutoFilter that column, hiding the blanks and voila :)
Or
=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"")
will enable you to use SpecialCells in VBA to get a range that you can interrogate for the maximum values in each group, as below:
Sub test()
Dim rng As Range
Dim cell
Range("B1:B" & Range("A65536").End(xlUp).Row).Formula = "=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"""")"
Set rng = Range(Range("B1:B" & Range("A65536").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(0, -1).Address)
For Each cell In rng
Debug.Print cell.Address & " =" & cell.Value
MsgBox cell.Address & " =" & cell.Value
Next
End Sub
For more information on the SpecialCells magic tricks, see How to delete multiple rows without a loop in Excel VBA.