Excelpromax123
Board Regular
- Joined
- Sep 2, 2021
- Messages
- 172
- Office Version
- 2010
- Platform
- Windows
Hello everyone. I need to edit the vba code below to apply to many lines (as shown in the image). Currently, I can only write the code for the first line. Thank you very much
Test VBA.xlsb
drive.google.com
VBA Code:
Sub RunMainCode() ' original code, unmodified
Dim lastRow As Long
Dim i As Long
Dim combinedText As String
Dim lineHeight As Double
Dim numLines As Long
Dim cellWidth As Double
' Start combining from cell A3
combinedText = Range("K9").Value & vbCrLf
' Loop through the cells from B3 to B(lastRow) and combine them into combinedText
For i = 9 To 12
combinedText = combinedText & Range("L" & i).Value & " x " & Range("M" & i).Value & " x " & Range("N" & i).Value & " = " & Range("O" & i).Value & vbCrLf
Next i
' Assign the combined content to cell A1
Range("B2").Value = Trim(combinedText)
' Format the first line in cell A1 (make the first part bold)
Range("B2").Font.Bold = False
With Range("B2").Characters(1, Len(Range("K9").Value)).Font
.Bold = True
End With
' Calculate the number of lines in cell A1
numLines = UBound(Split(Range("B2").Value, vbLf)) + 1
Rows("2:2").RowHeight = 16 * numLines
Range("C2").Value = Range("L7").Value ' temporary number
Range("D2").Value = Range("O7").Value ' temporary number
End Sub
Please note