I am creating a calendar feature in Excel that takes inputs from a form and based on that I will put the job details into the dynamic calendar. The problem I'm facing now is that whenever I'm updating a textbox position, it will re-position to another position if there is another textbox in the same cell which I believe is the cause of the formula I have in the code but that is meant for creating the textbox below each other like based of previous textbox which is working fine.
For example : If I update some values in the first textbox after it was created nicely with second textbox just directly below it with some spacing,the first textbox will re-position into the second textbox position.
Transfer data to destination workbook
destSheet.Range("B1").Value = SVDateValue ' Example: Service Date
destSheet.Range("B2").Value = DPValue
destSheet.Range("B3").Value = companyValue
destSheet.Range("B4").Value = SVTimeValue
Convert SVDateValue to a proper date value
Dim searchDate As Date
searchDate = DateValue(SVDateValue)
' Extract month, year, and day
Dim extractedMonth As String
Dim extractedYear As Integer
Dim extractedDay As Integer
Dim formattedDay As String
extractedMonth = MonthName(Month(searchDate))
extractedYear = Year(searchDate)
extractedDay = Day(searchDate)
'formattedDay = Format(extractedDay, "dd")
formattedDay = Right("0" & extractedDay, 2) ' Format as two-digit day with leading zero
Dim calendarSheet As Worksheet
Set calendarSheet = calendarWorkbook.Sheets("Calendar")
' Update the month and year cells
calendarSheet.Range("D1").Value = extractedMonth
calendarSheet.Range("G1").Value = extractedYear
' Search for the specific date within the range B:H
Dim searchRange As Range
Dim searchCell As Range
Set searchRange = calendarSheet.Range("B:H")
Set searchCell = searchRange.Find(What:=formattedDay, LookIn:=xlValues, LookAt:=xlWhole)
If Not searchCell Is Nothing Then
Dim targetCell As Range
For Each targetCell In searchRange
If IsDate(targetCell.Value) Then
Dim currentDate As Date
currentDate = DateValue(targetCell.Value)
' Compare currentDate with SVDateValue (day and month)
If Day(currentDate) = Day(searchDate) And Month(currentDate) = Month(searchDate) Then
' Set the targetRow and targetColumn based on the current targetCell
Dim targetRow As Long
Dim targetColumn As Long
targetRow = targetCell.Row
targetColumn = targetCell.Column
' Find the existing textbox in the same cell with matching DPValue and SVDateValue
Dim existingTb As Shape
For Each existingTb In calendarSheet.Shapes
If existingTb.Type = msoTextBox Then
'Dim lines() As String
lines = Split(existingTb.TextFrame2.TextRange.Text, vbCrLf)
If UBound(lines) >= 1 Then
'Dim dpValueFromShape As String
dpValueFromShape = Trim(lines(1)) ' Second line (index 1) is DPValue
'Dim dateFromShape As Date
dateFromShape = DateValue(lines(0)) ' First line (index 0) is the date
If dpValueFromShape = dpNumber And dateFromShape = SVDateValue Then
' Update the text content of the existing textbox
existingTb.TextFrame2.TextRange.Text = SVDateValue & vbCrLf & DPValue & vbCrLf & companyValue & vbCrLf & SVTimeValue
' Optionally, update other properties if needed
Exit For
End If
End If
End If
Next existingTb
' If no existing textbox found, create a new one in the correct cell
If existingTb Is Nothing Then
' Calculate the top position for the new textbox (below the existing ones)
Dim spacing As Double
spacing = 10 ' Adjust spacing as needed
Dim originalTextboxHeight As Double
originalTextboxHeight = 90 ' Fixed height for the original textbox
' Count the number of textboxes in the same cell
Dim textBoxCount As Long
For Each tb In calendarSheet.Shapes
If tb.Type = msoTextBox Then
If tb.TopLeftCell.Row = targetRow And tb.TopLeftCell.Column = targetColumn Then
textBoxCount = textBoxCount + 1
End If
End If
Next tb
' Calculate the top position using the formula: targetCell.Top + TextBox.Height * n + spacing
Dim topPosition As Double
topPosition = calendarSheet.Cells(targetRow, targetColumn).Top + spacing ' Start from the top of the cell
Dim tbTopPosition As Double
tbTopPosition = topPosition + originalTextboxHeight * textBoxCount + spacing
' Create a new textbox in the correct cell and position
Set tb = calendarSheet.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=calendarSheet.Cells(targetRow, targetColumn).Left, _
Top:=tbTopPosition, _
Width:=calendarSheet.Cells(targetRow, targetColumn).Width, _
Height:=80)
' ... Set properties for the TextBox
tb.Fill.Transparency = 1 ' Fully transparent fill
tb.Line.Visible = msoFalse ' No border
' Set TextBox text to SVDate and company values
tb.TextFrame2.TextRange.Text = SVDateValue & vbCrLf & DPValue & vbCrLf & companyValue & vbCrLf & SVTimeValue
' Change color of SVDateValue text to not filled
tb.TextFrame2.TextRange.Characters(1, Len(SVDateValue)).Font.Fill.Visible = msoFalse
' Increment the textbox count
textBoxCount = textBoxCount + 1
End If
End If
End If
Next targetCell
Else
MsgBox "Date not found in the specified range.", vbExclamation
End If
' Close the workbook and save changes
calendarWorkbook.Save
'calendarWorkbook.Close
'calendarWorkbook.Close SaveChanges:=True
End If
End Sub '
I have tried using ChatGPT but I cant really solve this, Like from troubleshooting i know that this part of the code is not really working cause the datefromshape will go through all the textbox in my calendar not the one I'm editing but that I am clueless on what it should be cause like it can't extract the right values since like dpvaluesfromshape would maybe be like the first textbox DP11 but the dpnumber is DP3 but that is cause it is going through all the textbox so there is DP3 later on also. I have also tried locking the cell but it doesn't work . Can someone provide me a solution to this problem and show me what is the working code . I would really appreciate it very much. I have been stuck for weeks and its my close to deadline. PLEASE HELP! Thank you in advance.
Dim existingTb As Shape
For Each existingTb In calendarSheet.Shapes
If existingTb.Type = msoTextBox Then
'Dim lines() As String
lines = Split(existingTb.TextFrame2.TextRange.Text, vbCrLf)
If UBound(lines) >= 1 Then
'Dim dpValueFromShape As String
dpValueFromShape = Trim(lines(1)) ' Second line (index 1) is DPValue
'Dim dateFromShape As Date
dateFromShape = DateValue(lines(0)) ' First line (index 0) is the date
If dpValueFromShape = dpNumber And dateFromShape = SVDateValue Then
' Update the text content of the existing textbox
existingTb.TextFrame2.TextRange.Text = SVDateValue & vbCrLf & DPValue & vbCrLf & companyValue & vbCrLf & SVTimeValue
' Optionally, update other properties if needed
Exit For
End If
End If
End If
Next existingTb '
This is what it look like after I successfully created the jobs[After Creating the Jobs in the CALENDAR
This is what will happen if I update textbox 1 or 2 while there is 3 textbox in that cell