I have a Sud that tells me if a column has a blank cell.
Is there a way to also get the cell location if it is blank, There can be thousands of row and maybe one or two blank cells, they are easy to miss even if you know it is there.
Thanks
Sub CountBlankCellsComments()
Dim Lastrow As Long
Sheets("Comments").Select
With Sheets("Comments")
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
If WorksheetFunction.CountBlank(Range("A2:E" & Lastrow)) = 0 Then
MsgBox "There Are (0) Blank Cells For ""Comments"" Sheet"
Else
MsgBox "For Comments Sheet There are:" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("A2:A" & Lastrow)) & ") Blank Cells in Column A" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("B2:B" & Lastrow)) & ") Blank Cells in Column B" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("C2:C" & Lastrow)) & ") Blank Cells in Column C" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("D2:D" & Lastrow)) & ") Blank Cells in Column D" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("E2:E" & Lastrow)) & ") Blank Cells in Column E"
End If
End Sub
Dim blanks As Range
With Worksheets("Comments")
On Error Resume Next
Set blanks = Application.Intersect(.Range("A2", .UsedRange.SpecialCells(xlCellTypeLastCell)), .Range("A:E")).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
End With
If blanks Is Nothing Then
MsgBox "There Are (0) Blank Cells For ""Comments"" Sheet"
Else
blanks.Select
MsgBox "For Comments Sheet There are (" & blanks.Cells.Count & ") Blank Cells:" & vbNewLine & vbNewLine & _
blanks.Address
End If
just add this line in your code:
MsgBox "BlankCells are:" & Range("A2:E" & Lastrow).SpecialCells(xlCellTypeBlanks).Address
How about this:
Sub ShowBlanks()
Dim data As Range, cl As Range, blanks As String
Set data = Range("A1:E" & Range("A" & Rows.Count).End(xlUp).Row)
blanks = vbNullString
For Each cl In data
If cl.Value = vbNullString Then
blanks = blanks & cl.Address & vbCrLf & vbLf
End If
Next cl
MsgBox "These cell are empty:" & vbCrLf & vbLf & blanks
End Sub
Related
Failing againg with my project
I have formulas with variable Brand that is changed dynamically (AF Column). Basically all I want is to extract Brands into a column next (AE) to the formula column for visial convenience
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Next i
Range("AF:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
ActiveSheet.Range("AG2:AG8").Formula = ActiveSheet.Range("AF2:AF8").Formula
ActiveSheet.Range("AH2:AH8").Formula = ActiveSheet.Range("AF2:AF8").Formula
Dim ws As Worksheet
Set ws = Worksheets(1)
Dim j As Variant
j = Application.Match(""" & Brand(i) & """, ws.Range("AF2:AF8"))
ActiveSheet.Range("AE2").Value = Application.Index(ws.Range("AF2:AF8"), j, 0)
And I get #N/A Already lost two days for that. Would be enourmously grateful to anyone who could help.
It's not exactly clear from your question as to your desired output but here's a guess:
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Range("AE" & i + 2).Value = Brand(i)
Next i
Range("AE:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
I've added a line to write the brand to AE, and altered the Sort to accommodate this.
I have a for statement in a VBA script that goes through each cell in a range (number of cells in range is variable based on user input - could be three cells could be 100). Each instance of the for loop calls an input box. How do I assign the user input from each instance of the for loop to a variable for later use?
Here is the for with the input box:
For Each cell In MyQCData
text_string = cell.Value
WrdArray() = split(text_string, ",")
For i = LBound(WrdArray) To UBound(WrdArray)
strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i)
Next i
InputBox ("This part requires a " & WrdArray(0) & " measurement of the " & _
WrdArray(1) & vbNewLine & vbNewLine _
& "The range for this is input is " & vbNewLine & vbNewLine & "Lower Control Limit " _
& WrdArray(2) & vbNewLine & "Upper Control Limit " & WrdArray(3))
Erase WrdArray()
Next cell
Yes, use an array:
Dim inputBoxAnswers() As String
ReDim inputBoxAnswers(1 To MyQCData.Cells.Count)
Dim cellCounter As Long
For Each cell In MyQCData
text_string = cell.Value
WrdArray() = split(text_string, ",")
'Is this loop needed???
For i = LBound(WrdArray) To UBound(WrdArray)
strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i)
Next i
cellCounter = cellCounter + 1
inputBoxAnswers(cellCounter) = InputBox("This part requires a " & _
WrdArray(0) & " measurement of the " & _
WrdArray(1) & _
vbNewLine & vbNewLine & _
"The range for this is input is " & _
vbNewLine & vbNewLine & _
"Lower Control Limit " & WrdArray(2) & _
vbNewLine & _
"Upper Control Limit " & WrdArray(3))
Next cell
If your MyQCData range is not a single column or a single row, you may find it easier to use a two-dimensional array, which could (perhaps) be dimensioned using
Dim inputBoxAnswers() As String
ReDim inputBoxAnswers(1 To MyQCData.Rows.Count, 1 To MyQCData.Columns.Count)
but then you will need to rework the indexes to use when assigning the elements their values. It would probably need to be
inputBoxAnswers(cell.Row - MyQCData.Row + 1, cell.Column - MyQCData.Column + 1) = ....
but a lot depends on how you intend to use the array afterwards.
It's kind of hard to tell without the rest of your code, but I assume that MyQCData is a Range. Try the below. I sort of "brute forced" the Inputbox look with k, FYI.
Dim k As Long
k = 0
Dim inputArr() As Variant
ReDim inputArr(myqcdData.Cells.Count)
For Each cell In MyQCData
text_string = cell.Value
WrdArray() = Split(text_string, ",")
For i = LBound(WrdArray) To UBound(WrdArray)
strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i)
Next i
inputArr(k) = InputBox("This part requires a " & WrdArray(0) & " measurement of the " & _
WrdArray(1) & vbNewLine & vbNewLine _
& "The range for this is input is " & vbNewLine & vbNewLine & "Lower Control Limit " _
& WrdArray(2) & vbNewLine & "Upper Control Limit " & WrdArray(3))
k = k + 1
Erase WrdArray()
Next cell
'Check each value in the array. This is optional and can be removed/commented out
For k = LBound(inputArr) To UBound(inputArr)
Debug.Print inputArr(k)
Next k
Edited per #Yow's astute comment
I'm going to dress this in a standalone code that you can easily run to get a feel for what's going on. Array variables must be declared hence Dim userInput(99) and the 99 is the upper limit (0-99 = 100 possible values). The first line of the first loop sets the variables, that's userInput(j) = InputBox("Sample InputBox", "InputBox Title", "blah" & j) the "blah" & j bit is the default entry, which is useful when you're writing/debugging as it's a lot faster to keep entering dummy data...
Sub inputBoxEg()
Dim userInput(99)
Dim MyQCData As Range
Set MyQCData = Range("A1:A4")
'Set InputBox inputs to a variable array called userInput:
j = 0
For Each cell In MyQCData
userInput(j) = InputBox("Sample InputBox", "InputBox Title", "blah" & j)
If userInput(j) = "" Then Exit Sub 'if user pressed cancel or entered blank
j = j + 1
Next cell
'Collate variables collected by InputBoxes in a text string called allInputs:
allInputs = ""
For i = 0 To j - 1
If i = 0 Then
allInputs = i & ": " & userInput(i)
Else
allInputs = allInputs & vbNewLine & i & ": " & userInput(i)
End If
Next i
MsgBox allInputs
End Sub
The totals show up on a message box and I want them on a new sheet in excel.
'Output totals to a message box
sTtl = "Total stock at " & dStk & " = " & TotStk
sMsg = "Board No." & vbTab & "Cut Lenght" & vbCrLf
For k = LBound(DetStk, 2) To UBound(DetStk, 2)
sMsg = sMsg & DetStk(0, k) & vbTab & vbTab _
& DetStk(1, k) & vbCrLf
Next k
MsgBox sMsg, vbOKOnly, sTtl
End Sub
The code below will create a new sheet and insert the data generated for the message box, including the title and headings, into 2 columns. You can comment out or delete the message box line msgbox sMsg, ..... if no longer want it to appear.
'Output totals to a message box
Sheets.Add After:=Sheets(Sheets.Count) ' create a new sheet
sTtl = "Total stock at " & dStk & " = " & TotStk
Range("A1").value = sTtl 'put th title in cell A1
sMsg = "Board No." & vbTab & "Cut Lenght" & vbCrLf
Range("A2").value = "Board No."
Range("B2").value = "Cut Lenght"
Range("A2:B2").Font.Bold = True ' format the headings bold (optional)
intRow = 3 ' set starting row below the heading row.
For k = LBound(DetStk, 2) To UBound(DetStk, 2)
sMsg = sMsg & DetStk(0, k) & vbTab & vbTab _
& DetStk(1, k) & vbCrLf
Range("A"&intRow).value = DetStk(0, k)
Range("B"&intRow).value = DetStk(1, k)
intRow = intRow + 1 'add 1 to move to the next row.
Next k
MsgBox sMsg, vbOKOnly, sTtl 'you can delete this line or comment it out with "'" if you don't want to display the message box.
End Sub
I have data in 3 different columns that I want to place in a column of cells that contain 22000 - 24000 characters of html code each. I was able to execute successfully with fewer characters in the column of cells that contain the html code. Is it possible to replace data in the column of cells that contain 22000 -24000 characters of html code each?
And I use a VBA program to do this.
Sub GoodREPLACETeleModule()
' <PUTDESCRIPTIONHERE>
For MY_ROWS = 1 To Range("AF" & Rows.Count).End(xlUp).Row
Range("R" & MY_ROWS).Value = Replace(Range("R" & MY_ROWS).Value, "PUTDESCRIPTIONHERE", Range("AF" & MY_ROWS).Value)
Next MY_ROWS
' <PUTIMAGEHERE>
For MY_ROWS = 1 To Range("AF" & Rows.Count).End(xlUp).Row
Range("P" & MY_ROWS).Value = Replace(Range("P" & MY_ROWS).Value, "PUTIMAGEHERE", Range("AF" & MY_ROWS).Value)
Next MY_ROWS
End Sub
I have "PUTIMAGEHERE" and "PUTDESCRIPTIONHERE" placed within the 24000 lines of html code but it does not reach it.
If you run the following, does the message box always say it is successful, even for characters greater than 24,000. If not, which data points fail exactly and at what point. You can review these data sets to verify that the overall length will not be greater than the limit.
Sub GoodREPLACETeleModule()
Dim wFind As Long
' <PUTDESCRIPTIONHERE>
For MY_ROWS = 1 To Range("AF" & Rows.Count).End(xlUp).Row
MsgBox "Row " & MY_ROWS
wFind = InStr(Range("R" & MY_ROWS).Value, "PUTDESCRIPTIONHERE")
If wFind = 0 Then
MsgBox "Nothing to replace"
Else
a = Len(Range("R" & MY_ROWS).Value): b = Len(Range("AF" & MY_ROWS).Value)
Range("R" & MY_ROWS).Value = Replace(Range("R" & MY_ROWS).Value, "PUTDESCRIPTIONHERE", Range("AF" & MY_ROWS).Value)
wFind = InStr(Range("R" & MY_ROWS).Value, "PUTDESCRIPTIONHERE")
If Not wFind = 0 Then MsgBox "Replace Failed"
If Len(Range("R" & MY_ROWS).Value) < a + b - Len("PUTDESCRIPTIONHERE") Then
MsgBox "The replace did not happen successfully"
Else
If Len(Range("R" & MY_ROWS).Value) > 32766 then
Msgbox "Too many characters"
Else
MsgBox "Replace succeeded, old string was " & a & " characters, new string is " & Len(Range("R" & MY_ROWS).Value) & " characters."
End If
End If
End If
Next MY_ROWS
' <PUTIMAGEHERE>
For MY_ROWS = 1 To Range("AF" & Rows.Count).End(xlUp).Row
MsgBox "Row " & MY_ROWS
wFind = InStr(Range("P" & MY_ROWS).Value, "PUTIMAGEHERE")
If wFind = 0 Then
MsgBox "Nothing to replace"
Else
a = Len(Range("R" & MY_ROWS).Value): b = Len(Range("AF" & MY_ROWS).Value)
Range("P" & MY_ROWS).Value = Replace(Range("P" & MY_ROWS).Value, "PUTIMAGEHERE", Range("AF" & MY_ROWS).Value)
wFind = InStr(Range("P" & MY_ROWS).Value, "PUTIMAGEHERE")
If Not wFind = 0 Then MsgBox "Replace Failed"
If Len(Range("P" & MY_ROWS).Value) < a + b - Len("PUTIMAGEHERE") Then
MsgBox "The replace did not happen successfully"
Else
If Len(Range("P" & MY_ROWS).Value) > 32766 then
Msgbox "Too many characters"
Else
MsgBox "Replace succeeded, old string was " & a & " characters, new string is " & Len(Range("P" & MY_ROWS).Value) & " characters."
End If
End If
End If
Next MY_ROWS
End Sub
Unfortunately there are Excel Limits
as to how many characters can be contained in a cell.
I am trying to average a column but only if the value is greater than zero. I then want it to put the information in the next blank cell in that row.
The below code was working as a simple Average but I want it to exclude any values of zero from the above cells.
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGE(" & .Address & ":" & .End(xlDown).Address & ")"
End With
I Tried with the following code to have it as if the cell address is greater than zero. But it keeps giving me an error to debug?
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGEIF(" & .Address & ":" & .End(xlDown).Address & "," & Cell.Address & " > 0," & .Address & ":" & .End(xlDown).Address & ")"
End With
Any help would be great.
Thanks
Al
Your syntax for the formula is wrong.
You need to create a formula like
=AVERAGEIF(D2:Dxx, ">0")
So use this
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGEIF(" & .Address & ":" & .End(xlDown).Address & ","">0"")"
End With