Count a the number of digits in a cell with VBA - vba

I get a spreadsheet but sometimes they dont give me all the digits I need when running my macro. So i am trying to find a way to count how many digits are in a cell and then use msgbox to tell me to add a digit to the cell.
If Len(A2) <> 7 Then
MsgBox ("Add # to the end ")
Exit Sub
End If
I was also wondering if there is a way to use an input box to add the number to the end of the value in excel. Like if the numbers are 123456 i can put 7 into the input box and it changes the cell value to 1234567

This should work for Characters, But if other Characters are in the cell besides just numbers it counts everything:
If Len([A2]) <> 7 Then
[A2] = [A2].Value & InputBox("Add # to the end ")
End If

Related

How to make textbox inputs numbers with 2 decimal places and then check their sum?

struggling with something in VBA and can't find a clear answer online. Quite new to this. Basically, for the user form below (linked image due to my level), I would like to make all the textbox inputs be numbers (2 decimal places) and then when you click "Next" it checks that all the values sum to 100. If not, error message appears. I've tried lots of different ways of doing this and can't get it to work. I would like for all textbox inputs to be numbers (2dp), and then all the numbers to just add and then check this sum. For each textbox, I've done the following Sub Routine (variable name changes each time for name of textbox):
Private Sub BinQnt_AfterUpdate()
On Error Resume Next
Dim BinQnt As Single
BinQnt = Format(BinQnt, "percent")
End Sub
For the Next button I have done the following:
Private Sub MaterialsData_Next_Click()
'Check mix design = 100%.
'If = 100%, input data to database.
tot = BinQnt + FillQnt + FineQnt + CoarQnt + RAPQnt + CRQnt 'Names of each text box.
If tot = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
'Go to next page.
BaseUserForm.MultiPage1.Value = 2
'If doesn't = 100%, then show error MsgBox
Else
MsgBox "Error, please check mixture design sums to 100%. Currently at " & tot & "."
End If
Screenshot of userform.

EXCEL VBA: finding lowest value in column

i have tried to make select to pick column and find lowest value, but i dont know whats wrong:
mazas = Application.WorksheetFunction.Min(Sheets("maping").Range(Range("C3"), Range("C3").End(xlDown)).Select)
I think the biggest problem is that i dont know lenght of column it can gave 3 number or 3000 numbers, but it will always start at C3. Any ideas how to make it work?
You don't need to worry about where the data ends, just skip the first two rows:
Sub NotTheFirstTwoRows()
Dim c As Range
Set c = Range("C3:C" & Rows.Count)
MsgBox Application.WorksheetFunction.Min(c)
End Sub
Because any blanks at the bottom of the column will be ignored.

Excel Copy Dynamic Cell Value

I've a very simple VBA function to copy the value of a cell(which is the summation of a range) to an empty cell. The program loops 50 times, the L53 cell contains the summation of a Range that changes each time as the NORMINV(RAND(),0,1) generates different values per loop.
Sub Run_Calc_Btn_Click()
For i = 1 To 50
Range("O" & i + 1).Value = Range("L53")
Next i
End Sub
However, the copied value does not equal to the original cell value and I couldn't figure out where does this value come from.
I tried running your function and I think that the problem is that after the last loop the value changes again.
If you as a quick debug add the following as the first line inside the for loop: MsgBox "Value of L53: " & Range("L53").Value, you will see that each cell in column O does indeed match the sum in cell L53.
Before you click OK in the messagebox, you can verify that the value in cell L53 is the same as the value shown in the messagebox and after you click OK the next cell in column O will have the value that was just shown in the messagebox.

Message Box if cell exceeds another cell

What would be the best way to write a VBA code to have a message box pop up if the value in one cell is less than or greater than another cell - and then display the difference?
Column N contains total appts (manual input)
Column R contains total results (formula generated)
If the cell in column R after calculated is less than or greater than the cell in column N the message box would pop up and say Total results is less than appts by # or Total results is greater than appts by #.
Add the following routine to your required sheet in the VBA project (e.g. Sheet1)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("N1") Or Target = Range("R1") Then 'Only attempt to run the below code if target is a range you care about
If Range("R1").Value2 <> Range("N1").Value2 Then
MsgBox "Values differ"
End If
End If
End Sub
On the assumption that you want to compare two cells with one another (as opposed to a whole column of cells):
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("N1") > Range("R1") Then
MsgBox "Oops. Results less than Input by " & Abs(Range("N1") - Range("R1"))
End If
If Range("N1") < Range("R1") Then
MsgBox "Oops. Results greater than Input by " & Abs(Range("N1") - Range("R1"))
End If
End Sub
That should achieve the following:
Compare two cells with one another whenever the sheet changes (regardless whether it be the formula generated value for R1, the manual input for N1, or anything else on the sheet)
Identify which is greater
Pop up an appropriate message

excel 2007: append certain characters to value in a column to make it proper lenght

I have following values in a column :
123
456
789
65
1
I want to append correct number of zeros in all the values in that column such that the total length of character is 5.
00123
00456
00789
00065
00001
How do I do that?
If there is one number per cell, you can do this easily by changing the format to "Custom."
Right-click on the cells you would like to format.
From the context menu, choose "Format cells"
Choose the Custom category.
Over the word "General," in the Type textbox, enter 00000. (This tells Excel to print with
leading 0s).
Click OK.
If the number is bigger than five digits, it will print all of the digits.
===EDIT===
You explained that these were all in one cell. #paulmorriss has an excellent Excel-function-only solution, but let me proffer a VBA solution as an alternative:
Sub Macro1()
Dim txt As String
Dim asWords() As String
Dim zeros As String
txt = vbNullString
asWords = Split(Range("A1").Value) 'asWords(0)="123" etc.
For i = 0 To UBound(asWords) ' emulate StrDup (missing in VBA)
zeros = vbNullString
For j = Len(asWords(i)) + 1 To 5: zeros = zeros + "0": Next j
txt = txt + zeros + asWords(i) + " "
Next i
Range("B1").Value = txt 'Places answer in B1
End Sub
If the value you specify is in cell A1 then put the following formulae in B1 to K1. The value in K1 is what you need. You could specify one massive formula, but it's easier for people maintaining the spreadsheet to see what's going on if it's split up like this.
in B1 =TEXT(VALUE(LEFT(A1,SEARCH(" ",A1))),"000000")
in C1 =RIGHT(A1,LEN(A1)-SEARCH(" ",A1))
etc. =TEXT(VALUE(LEFT(C1,SEARCH(" ",C1))),"000000")
=RIGHT(C1,LEN(C1)-SEARCH(" ",C1))
=TEXT(VALUE(LEFT(E1,SEARCH(" ",E1))),"000000")
=RIGHT(E1,LEN(E1)-SEARCH(" ",E1))
=TEXT(VALUE(LEFT(G1,SEARCH(" ",G1))),"000000")
=RIGHT(G1,LEN(G1)-SEARCH(" ",G1))
=TEXT(I1,"000000")
=B1&" "&D1&" "&F1&" "&H1&" "&J1