Excel VBA Replace characters in cells that contain html code - excel

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.

Related

Encountering an error while appending text in Excel comment - VBA

Below is my code that I am trying to append new text to an existing comment. However, when I execute, it is spitting an error - "Application defined/Object defined error". Could someone help?
For Each c In Range("G" & iCellIndex)
If StrComp(JSONObj2("fields")("worklog")("worklogs")(j + 1)("author")("displayName"), Range("G" & iCellIndex).Value) = 0 Then
Set cmt = ActiveCell.AddComment
cmt.Comment.Text iTimeStamp & "--" & JSONObj2("fields")("worklog")("worklogs")(j + 1)("timeSpent") , 1, False
Range("G" & iCellIndex).AddComment iTimeStamp & "--" & JSONObj2("fields")("worklog")("worklogs")(j + 1)("timeSpent") , 1, False
End If
iCellIndex = iCellIndex + 1
Next c
For those interested, this is how I fixed the issue:
If StrComp(JSONObj2("fields")("worklog")("worklogs")(j + 1)("author")("displayName"), Range("G" & iCellIndex).Value) = 0 Then
Range("G" & iCellIndex).NoteText Text:=Range("G" & iCellIndex).NoteText & Chr(10) & iTimeStamp & " -- " & JSONObj2("fields")("worklog")("worklogs")(j + 1)("timeSpent") & Chr(10)
End If

VBA user form that returns data to next empty row in excel spreadsheet and limit data type for date in specific textbox

I am trying to make an excel user form through VBA. It should:
Take the data entered in the textboxes and put it into the next empty row in a specific sheet
In some textBoxes (specifically TextBox3, TextBox4, TextBox5 and TextBox6) should only be entered date data type. If the user enters any other data format an error message should appear and the form should close, not filling the next empty row that it was about to fill in the sheet.
All textBoxes should have an input, except TextBox5 and TextBox6, these could be empty, if any other textbox is empty an error message should appear.
After the proper inputs are made a confirmation msgbox should appear for the user to check any error, before the form closes
Step 1 I've menaged to do but 2 and 3 are not working properly with what I have so far (I can me more specific with the errors if needed). I'm new to VBA and programming and think I've messed with the 'Ifs'. Thanks in advence for any help!
Private Sub CommandButton1_Click()
'Check if data in TextBox is date
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not ((IsDate(TextBox3.Text)) And (IsDate(TextBox4.Text)) And (IsDate(TextBox5.Text)) And (IsDate(TextBox6.Text))) Then
MsgBox "Date Required"
Cancel = True
Else
'Last ditch validation before committing input values to document.
Dim booConfirmation As Boolean
'Check for data
If Len(TextBox1.Text) = 0 Or Len(TextBox2.Text) = 0 Or Len(TextBox3.Text) = 0 Or Len(TextBox4.Text) = 0 Or Len(TextBox7.Text) = 0 Or Len(TextBox8.Text) = 0 Or Len(TextBox9.Text) = 0 Or Len(TextBox10.Text) = 0 Or Len(TextBox11.Text) = 0 Then
MsgBox "Empty entries", vbOKOnly, "Input Error"
End If
'Display name so user can check and confirm.
booConfirmation = MsgBox("Are the entries " & TextBox1 & " " & TextBox2 & " " & TextBox3 & " " & TextBox4 & " " & TextBox7 & " " & TextBox8 & " " & TextBox9 & " " & TextBox10 & " " & TextBox11 & "correct?", vbYesNo)
'If booConfirmation Then
If booConfirmation = vbNo Then
MsgBox "Please correct the entries"
Set ws = Sheets("Inputs")
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
ws.Range("B" & LastRow).Value = "" 'if entries are incorrect erase the data that should be entered into the sheet'
ws.Range("C" & LastRow).Value = ""
ws.Range("D" & LastRow).Value = ""
ws.Range("E" & LastRow).Value = ""
ws.Range("F" & LastRow).Value = ""
ws.Range("G" & LastRow).Value = ""
ws.Range("H" & LastRow).Value = ""
ws.Range("I" & LastRow).Value = ""
ws.Range("J" & LastRow).Value = ""
ws.Range("K" & LastRow).Value = ""
ws.Range("L" & LastRow).Value = ""
Exit Sub
Else
Set ws = Sheets("Inputs")
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
ws.Range("B" & LastRow).Value = TextBox1.Text 'Adds the TextBox1 into Col B & Last Blank Row
ws.Range("C" & LastRow).Value = TextBox2.Text 'Adds the TextBox2 into Col C & Last Blank Row
ws.Range("D" & LastRow).Value = TextBox3.Text
ws.Range("E" & LastRow).Value = TextBox4.Text
ws.Range("F" & LastRow).Value = TextBox5.Text
ws.Range("G" & LastRow).Value = TextBox6.Text
ws.Range("H" & LastRow).Value = TextBox7.Text
ws.Range("I" & LastRow).Value = TextBox8.Text
ws.Range("J" & LastRow).Value = TextBox9.Text
ws.Range("K" & LastRow).Value = TextBox10.Text
ws.Range("L" & LastRow).Value = TextBox11.Text
End If
End If
Unload Me
End Sub
This is not an answer this is just to help you see it more clearly
All I've done is re-arrange your code and provide consistent indentation
You had, no end sub for one procedure and code mixed up between the two, with an endif that belonged in one in the other
Anyway - this is your code ... with only minor changes .... make exit procedure just check the 1 field
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Check if data in TextBox is date
If Not (IsDate(TextBox3.Text)) Then ' And (IsDate(TextBox4.Text)) And (IsDate(TextBox5.Text)) And (IsDate(TextBox6.Text))) Then
MsgBox "Date Required"
Cancel = True
Else
End If
End Sub
Private Sub CommandButton1_Click()
'Last ditch validation before committing input values to document.
Dim booConfirmation As Boolean
'Check for data
'Display name so user can check and confirm.
booConfirmation = MsgBox("Are the entries " & TextBox1 & " " & TextBox2 & " " & TextBox3 & " " & TextBox4 & " " & TextBox7 & " " & TextBox8 & " " & TextBox9 & " " & TextBox10 & " " & TextBox11 & "correct?", vbYesNo)
'If booConfirmation Then
If booConfirmation = vbNo Then
MsgBox "Please correct the entries"
Set ws = Sheets("Inputs")
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
ws.Range("B" & LastRow).Value = "" 'if entries are incorrect erase the data that should be entered into the sheet'
ws.Range("C" & LastRow).Value = ""
ws.Range("D" & LastRow).Value = ""
ws.Range("E" & LastRow).Value = ""
ws.Range("F" & LastRow).Value = ""
ws.Range("G" & LastRow).Value = ""
ws.Range("H" & LastRow).Value = ""
ws.Range("I" & LastRow).Value = ""
ws.Range("J" & LastRow).Value = ""
ws.Range("K" & LastRow).Value = ""
ws.Range("L" & LastRow).Value = ""
Exit Sub
Else
Set ws = Sheets("Inputs")
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
ws.Range("B" & LastRow).Value = TextBox1.Text 'Adds the TextBox1 into Col B & Last Blank Row
ws.Range("C" & LastRow).Value = TextBox2.Text 'Adds the TextBox2 into Col C & Last Blank Row
ws.Range("D" & LastRow).Value = TextBox3.Text
ws.Range("E" & LastRow).Value = TextBox4.Text
ws.Range("F" & LastRow).Value = TextBox5.Text
ws.Range("G" & LastRow).Value = TextBox6.Text
ws.Range("H" & LastRow).Value = TextBox7.Text
ws.Range("I" & LastRow).Value = TextBox8.Text
ws.Range("J" & LastRow).Value = TextBox9.Text
ws.Range("K" & LastRow).Value = TextBox10.Text
ws.Range("L" & LastRow).Value = TextBox11.Text
End If
Unload Me
End Sub

Change color in text from cell with VBA excel

I want to change some text when saving to worksheet, the cell has multiple textbox values together.
When textbox is colored red in VBA-excel, it has to save that text value in the cell in red font.
When it is not red, it has to save in black font color.
Range("w" & lmaxrows + 1).Value = "S " & TextBox42.Value & " |VP " & TextBox43.Value & " |NP " & TextBox47.Value & " |E " & TextBox48.Value
If TextBox42.BackColor = vbRed Then
With Range("w" & lmaxrows)
d = "S " & TextBox42.Value
If Range("w" & lmaxrows).Value > "" Then Replace(Range("w"), d, Range("w")).Font.Color = vbRed Range("w")).Font.Color = vbRed
end if
end if
I have this for now, but it doesn't work.
How about checking the length of your textbox and then using that to change the color of that many characters on the Cell:
Private Sub CommandButton1_Click()
Sheet1.Cells(1, 1).Value = TextBox1.Text & " some other text"
lengthofRed = Len(TextBox1.Text)
'check the length of the text box
If TextBox1.BackColor = vbRed Then
Sheet1.Cells(1, 1).Characters(1, lengthofRed).Font.Color = vbRed
End If
End Sub
EDIT:
Changing the above to match your code, I believe the following should do what you expect:
Range("w" & lmaxrows + 1).Value = "S " & TextBox42.Value & " |VP " & TextBox43.Value & " |NP " & TextBox47.Value & " |E " & TextBox48.Value
If TextBox42.BackColor = vbRed Then
With Range("w" & lmaxrows)
d = "S " & TextBox42.Value
If Range("w" & lmaxrows).Value > "" Then
Range("W" & lmaxrows + 1).Characters(1, Len(d)).Font.Color = vbRed
End If
End If

how to get your answers to show up on a new page

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

Find cell location if blank

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

Resources