Add a textbox below node in diagrams VBA Excel - excel

Hi i am making a organisational hierarchy chart and i want to have a textbox below each nodes. What i did until now was to retrieve the data and plot out the hierarchy. But how do i add textbox under them? I have to add 2 textboxes below each nodes. Any help will be appreciated!
Code:
Option Explicit
Sub OrgChart()
Dim ogSALayout As SmartArtLayout
Dim QNodes As SmartArtNodes
Dim QNode As SmartArtNode
Dim ogShp As Shape
Dim shp As Shape
Dim t As Long
Dim i As Long
Dim r As Long
For Each shp In ActiveSheet.Shapes
If shp.Type = msoSmartArt Then: shp.Delete
Next shp
Set ogSALayout = Application.SmartArtLayouts( _
"urn:microsoft.com/office/officeart/2009/3/layout/HorizontalOrganizationChart" _
)
Set ogShp = ActiveSheet.Shapes.AddSmartArt(ogSALayout, 630, 36, 1000, 1000)
Set QNodes = ogShp.SmartArt.AllNodes
t = QNodes.Count
For i = 2 To t: ogShp.SmartArt.Nodes(1).Delete: Next i
Set QNode = QNodes(1)
If Range("D1").Value = "CONFIRM" Then
QNode.Shapes.Fill.ForeColor.RGB = RGB(255, 0, 0)
ElseIf Range("D1").Value = "PENDING" Then
QNode.Shapes.Fill.ForeColor.RGB = RGB(128, 128, 128)
ElseIf Range("D1").Value = "SUSPECTED" Then
QNode.Shapes.Fill.ForeColor.RGB = RGB(255, 255, 0)
ElseIf Range("D1").Value = "NO" Then
QNode.Shapes.Fill.ForeColor.RGB = RGB(0, 255, 0)
End If
With QNode.TextFrame2.TextRange
.Text = Range("B1").Value
.Font.Fill.ForeColor.RGB = vbBlack
.Font.Size = 12
.Font.Bold = True
End With
With QNode.Shapes.AddTextbox(msoTextOrientationHorizontal, _
100, 100, 200, 50) _
.TextFrame.Characters.Text = "Test Box"
End With
r = 1
Call AddChildren(QNode, r)
ogShp.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
End Sub
Sub AddChildren(ByVal QParent As SmartArtNode, ByVal r As Long)
Dim QChild As SmartArtNode
Dim Level As Long
Dim s As Long
Const MyCol As String = "C"
Level = Range(MyCol & r).Value
s = r + 1
Do While Range(MyCol & s).Value > Level
If Range(MyCol & s).Value = Level + 1 Then
Set QChild = QParent.AddNode(msoSmartArtNodeBelow)
If Range("D" & s).Value = "CONFIRM" Then
QChild.Shapes.Fill.ForeColor.RGB = RGB(255, 0, 0)
ElseIf Range("D" & s).Value = "PENDING" Then
QChild.Shapes.Fill.ForeColor.RGB = RGB(128, 128, 128)
ElseIf Range("D" & s).Value = "SUSPECTED" Then
QChild.Shapes.Fill.ForeColor.RGB = RGB(255, 255, 0)
ElseIf Range("D" & s).Value = "NO" Then
QChild.Shapes.Fill.ForeColor.RGB = RGB(0, 255, 0)
End If
With QChild.TextFrame2.TextRange
.Text = Range("B" & s).Value
.Font.Fill.ForeColor.RGB = vbBlack
.Font.Size = 12
End With
Call AddChildren(QChild, s)
End If
s = s + 1
Loop
End Sub
This is what it looks like now:
Edit: Added screenshot of data layout.

Adding a textbox under a node would mean that you would have to move the node up to make room for the textbox. As far as I know, it's not possible to move the nodes using VBA.
As a workaround, you could create another node under each node and format it as a textbox.
The outcome would look something like this:
To do this, I would first remove this from OrgChart
With QNode.Shapes.AddTextbox(msoTextOrientationHorizontal, _
100, 100, 200, 50) _
.TextFrame.Characters.Text = "Test Box"
End With
And replace it with:
Dim PseudoTxtBox As SmartArtNode
Set PseudoTxtBox = QNode.AddNode(msoSmartArtNodeAfter) 'Pseudo text box
'Add some text to the textbox
With PseudoTxtBox.TextFrame2.TextRange
.Text = "Some Text"
.Font.Fill.ForeColor.RGB = vbBlack
.Font.Size = 12
End With
'Make the textbox background transparent
PseudoTxtBox.Shapes.Fill.Transparency = 1
Then I would insert the following code right after adding the node in AddChildren :
Dim PseudoTxtBox As SmartArtNode
Set PseudoTxtBox = QChild.AddNode(msoSmartArtNodeAfter) 'Pseudo text box
'Add some text to the textbox
With PseudoTxtBox.TextFrame2.TextRange
.Text = "Some Text"
.Font.Fill.ForeColor.RGB = vbBlack
.Font.Size = 12
End With
'Make the textbox background transparent
PseudoTxtBox.Shapes.Fill.Transparency = 1
'Get the parent shape
Dim mshp As Shape
Dim tempObject As Object
Set tempObject = QChild.Parent
Do While TypeName(tempObject) <> "Shape"
Set tempObject = tempObject.Parent
Loop
Set mshp = tempObject
'Set the corresponding connector (line) to be transparent.
mshp.GroupItems(Level).Line.Transparency = 1

Related

VBA Resize shape according to cell timevalue data

I want to populate my shape according to time range value in 1st Range and 2nd Range cell as shown in the image. Thank you. Your help is much appreciated
https://i.stack.imgur.com/XNNy2.jpg
I've tried this code but it won't work.
Dim z As Range
For Each z In Range("a4:a19").Rows
If z.Value >= Range("F4") Then Exit For
Next z
Dim x As Range
For Each x In Range("a4:a19").Rows
If x.Value >= Range("G4") Then Exit For
Next x
'MsgBox z & x
Dim c
Dim rnrn
c = Rows(3).Find(DateValue("12/11/2022")).Column
'Application.InchesToPoints(10)
Dim LLL As Single, TTT As Single, WWW As Single, HHH As Single
Set rnrn = Range(z.Address, x.Address).Offset(0, c - 1)
LLL = rnrn.Left
TTT = rnrn.Top
WWW = rnrn.Width
HHH = rnrn.Height
With ActiveSheet.Shapes
' .LockAspectRatio = msoFalse
.AddTextbox(msoTextOrientationHorizontal, LLL, TTT + Application.InchesToPoints(Range("F4").Value), WWW, Application.InchesToPoints(Range("F4").Value) + Application.InchesToPoints(Range("G4").Value)).Select
' .Placement = xlMove
' .LockAspectRatio = msoTrue
End With
Dim r1 As Byte, r2 As Byte, r3 As Byte
r1 = WorksheetFunction.RandBetween(0, 255)
r2 = WorksheetFunction.RandBetween(0, 255)
r3 = WorksheetFunction.RandBetween(0, 255)
With Selection.ShapeRange.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(r1, r2, r3)
.Transparency = 0
.Solid
End With
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
With Selection.ShapeRange.TextFrame2.TextRange.Characters.ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignCenter
End With
Selection.ShapeRange.TextFrame2.TextRange.Characters.Font.Size = 15
Selection.ShapeRange.TextFrame2.TextRange.Characters.Text = Range("F3").Text & " - " & Range("G3").Text
If I understand you correctly....
Below image is an example before running the sub
The expected result after running the sub :
If the image both is similar with your case, then maybe you want to have a look the code below then modify it according to your need. The code don't do any "fancy stuffs", such as coloring, font type, font size, etc.
Sub test()
Dim rg As Range: Dim sTxt As String: Dim eTxt As String
Dim dur: Dim pos
Dim h As Integer: Dim w As Integer
Dim L As Integer: Dim T As Integer
With ActiveSheet
For Each shp In .Shapes: shp.Delete: Next
End With
Set rg = Range("F2", Range("F" & Rows.Count).End(xlUp))
For Each cell In rg
sTxt = Format(cell.Value, "hh:mm AM/PM")
eTxt = Format(cell.Offset(0, 1).Value, "hh:mm AM/PM")
dur = Format(cell.Offset(0, 1).Value - cell.Value, "h:m")
dur = Split(dur, ":")(0) & "." & Application.RoundUp(Split(dur, ":")(1) * 1.666, 0)
pos = Format(cell.Value, "h:m")
pos = Split(pos, ":")(0) & "." & Application.RoundUp(Split(pos, ":")(1) * 1.666, 0)
With Range("D4")
h = dur * .Height: w = .Width
L = .Left: T = .Top + ((pos - 7) * .Height)
End With
With ActiveSheet.Shapes
.AddTextbox(msoTextOrientationHorizontal, L, T, w, h) _
.TextFrame.Characters.Text = sTxt & " - " & eTxt
End With
Next
End Sub
For the textbox size,
the height is coming from subtracting the end time with start time, split the value by ":", then add decimal point ".", then multiply the value after the decimal point with 1.666, so the approx value can be divided by 100, not 60, then multiply by the row height of row 4. The width is coming from column D width.
For the textbox position,
The top position is coming from the start time, then it
s the same process like for the height of the box. The left position is coming from the left position value of column D.

Need help using VBA to insert hyperlinks in excel to specified pictures on my computer

I have 600+ pictures in a folder on my computer and I want to link each one to a different cell in an excel file using vba instead of going through and linking each one manually. I'm not very good at vba but the end goal is a code that can go down the line in excel and pull the designated picture from my files and link it and then go to the next.
The code I have so far is partially going off another post I saw on here and it's just trying to do the first step of inserting the first picture but I am having trouble with it:
Dim Picture_1 As String
With ActiveSheet.Pictures.Insert("X:\roena10\Q ear crack pictures")
.Left = ActiveSheet.Range("photograph").Left + 2
.Top = ActiveSheet.Range("photograph").Top + 2
Picture_1 = .Name
End With
ActiveSheet.Pictures(profile).Select
With Selection.ShapeRange
.LockAspectRatio = msoFalse
.Width = 20
.Height = 20
End With
Any help is appreciated!
Try this code:
Sub AddImages()
Const path = "c:\test\", W = 20, H = 20, h_gap = 5
Dim img As Shape, cl As Range, ws As Worksheet
Dim fname As String, ext As String, pos As Integer, T As Long, L As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
Set cl = ws.Range("B1")
fname = Dir(path & "*", vbNormal)
Do While Len(fname) > 0
pos = InStrRev(fname, ".")
ext = vbNullString
If pos > 0 Then ext = LCase(Mid(fname, pos + 1))
Select Case ext
Case "jpg", "png", "bmp" 'and so on
With cl
T = .Top + 2
L = .Left + 2
.EntireRow.RowHeight = H + h_gap
End With
Set img = ws.Shapes.AddPicture(Filename:=path & fname, _
LinkToFile:=msoTrue, SaveWithDocument:=True, _
Left:=L, Top:=T, Width:=-1, Height:=-1)
img.LockAspectRatio = msoTrue
img.Height = H
With img.Line
.Visible = msoTrue
.ForeColor.RGB = vbBlack
.Transparency = 0
End With
ws.Hyperlinks.Add Anchor:=img, Address:=path & fname
T = T + H + h_gap
Set cl = cl.Offset(1)
End Select
fname = Dir
Loop
End Sub
Screenshot

Excel VBA - Copy pictures from on sheet to another in specified location

I'be built a code for a calendar-type plan with textboxes and pictures. I've managed to make a code so the shapes are all placed on the right spot. However, I'm struggling to copy some pictures from one sheet to another.
Sub AddEvent2()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim i As Integer, shp As Shape, s, s2, v, t1, t2, t3, h, p, w, rgb1, rgbULP, rgbPULP, rgbSPULP, rgbXLSD, rgbALPINE, rgbJET, rgbSLOPS As String
For Each shp In Sheets("Calendar").Shapes
shp.Delete
Next shp
For i = 4 To 21
t1 = Sheets("AdminSheet").Cells(i, 30).Value 'Cell location on Calendar
s = Sheets("AdminSheet").Cells(i, 29).Value 'Naming the shapebox
w = Sheets("AdminSheet").Cells(i, 28).Value 'Supplier
p = Sheets("AdminSheet").Cells(i, 27).Value 'Product
t2 = Sheets("AdminSheet").Cells(i - 1, 30).Value 'Next Cell location on Calendar
v = Application.WorksheetFunction.Text(Sheets("AdminSheet").Cells(i, 24).Value, "hh:mm") & " " & _
Sheets("AdminSheet").Cells(i, 25).Value & Sheets("AdminSheet").Cells(i, 26).Value & " " & Sheets("AdminSheet").Cells(i, 27).Value 'Text in shapebox
rgbULP = rgb(177, 160, 199)
rgbPULP = rgb(255, 192, 0)
rgbSPULP = rgb(0, 112, 192)
rgbXLSD = rgb(196, 189, 151)
rgbALPINE = rgb(196, 215, 155)
rgbJET = rgb(255, 255, 255)
rgbSLOPS = rgb(255, 0, 0)
If s <> "" Then
Sheets("Calendar").Select
If i > 4 And t2 = t1 Then
s2 = Sheets("AdminSheet").Cells(i - 1, 29).Value 'Name of the added shapebox
h = Sheets("Calendar").Shapes.Range(Array(s2)).Height 'Height of the added shapebox
t3 = Sheets("Calendar").Shapes.Range(Array(s2)).Top 'Top of the added shapebox
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Sheets("Calendar").Range(t1).Left + 1.5, 3 + t3 + h, 209, 36.6).Select
Else
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Sheets("Calendar").Range(t1).Left + 1.5, Sheets("Calendar").Range(t1).Top + 3, 209, 36.6).Select
End If
With Selection
.Name = s
With .ShapeRange
.IncrementLeft 0
.IncrementTop 0
With .Fill
.Visible = msoTrue
If p = "ULP" Then
.ForeColor.rgb = rgbULP
ElseIf p = "PULP" Then
.ForeColor.rgb = rgbPULP
ElseIf p = "SPULP" Then
.ForeColor.rgb = rgbSPULP
ElseIf p = "XLSD" Then
.ForeColor.rgb = rgbXLSD
ElseIf p = "ALPINE" Then
.ForeColor.rgb = rgbALPINE
ElseIf p = "JET" Then
.ForeColor.rgb = rgbJET
ElseIf p = "SLOPS" Then
.ForeColor.rgb = rgbSLOPS
End If
.Transparency = 0
.Solid
End With
With .TextFrame2
.MarginLeft = 5.7
.MarginRight = 38.6
.AutoSize = msoAutoSizeShapeToFitText
With .TextRange.Font
.NameComplexScript = "Lucida Console"
.NameFarEast = "Lucida Console"
.Name = "Lucida Console"
.Size = 14
End With
.TextRange.Characters.Text = v
End With
End With
End With
Sheets("AdminSheet").Select
ActiveSheet.Shapes.Range(Array(w)).Select
Selection.Copy
Sheets("Calendar").Select
ActiveSheet.Paste
Selection.ShapeRange.Name = w & s
Selection.Name = w & s
Sheets("Calendar").Shapes(w & s).Top = Sheets("Calendar").Shapes(s).Top + (Sheets("Calendar").Shapes(s).Height / 2) - (Sheets("Calendar").Shapes(w & s).Height / 2)
Sheets("Calendar").Shapes(w & s).Left = Sheets("Calendar").Shapes(s).Left + Sheets("Calendar").Shapes(s).Width - Sheets("Calendar").Shapes(w & s).Width
End If
Next i
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
As you can see I'm very new to coding... Anyway, what I'm trying to do is to copy a pic named after "w" and paste it to the right side of the added Textbox (within the box - I guess the location would be textbox.left + textbox.width - pic.width but it doesn't work). I've tried recording it but it doesn't work for me. Any ideas?
*Edit - I updated the with the code I use for that task and the error I get. The location is wrong as well - they go outside the textbox...
I also struggle to understand how to change the fillcolor.RGB of the shape dynamically. I made it work with "if" statement but looks ugly. Any ideas how to sort the code there? Why .ForeColor.RGB = "rgb" & p not working?
Thanks in advance
I found the problem.
On the initial code, the name of the picture I was copying was not changing, hence not unique on the Sheet. So, when I was tying to move the Shape("example"), I was moving all with the same name.
Anyway, code updated with name change variable!

Unable to group two shapes then group resulting shape

I am recieving an error when I attempt to run this program. It is a userform which pops up from double clicking a shape in excel. The userform allows u to change the status of the box from normal to "in progress" to "done". This change creates a box inside the original and adds a border as shown.
This works fine the first time you change. But once you change the status to in prog or done you cannot change it again or you receive the following error:
I was able to repeatedly change the status by using two buttons but once I merged into a combobox in the userform I started experiencing this the code is shown below for the Userform and the line where the error occurs will be bolded. Thanks in advance for the help.
Private Sub UserForm_Initialize()
'fill combobox catagory
Me.cmbCAT.AddItem "L1U"
Me.cmbCAT.AddItem "L1L"
Me.cmbCAT.AddItem "IN"
Me.cmbCAT.AddItem "SC"
Me.cmbCAT.AddItem "GE"
Me.cmbCAT.AddItem "TE"
Me.cmbCAT.AddItem "ExD"
'fill combobox resources
Me.cmbResource.AddItem "Item1"
Me.cmbResource.AddItem "Item2"
'fill combobox Status
Me.cmbStatus.AddItem ""
Me.cmbStatus.AddItem "In Prog"
Me.cmbStatus.AddItem "Done"
End Sub
Private Sub btnSubmit_Click()
Dim AShape As Shape
Dim USelection As Variant
Dim ShapeArray(0 To 1) As String
Dim ShapeArr(0 To 1) As String
'Pull-in what is selected on screen
Set USelection = ActiveWindow.Selection
'Determine if selection is a shape
Set AShape = ActiveSheet.Shapes(Sheet4.Range("B1"))
If cmbCAT.Text = "L1U" Then
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(255, 180, 18)
ElseIf cmbCAT.Text = "L1L" Then
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(147, 196, 22)
ElseIf cmbCAT.Text = "SC" Then
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(147, 196, 22)
ElseIf cmbCAT.Text = "IN" Then
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(255, 255, 70)
ElseIf cmbCAT = "GE" Then
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(255, 173, 203)
ElseIf cmbCAT = "TE" Then
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(114, 163, 255)
Else
ActiveSheet.Shapes(Sheet4.Range("B1")).Fill.ForeColor.RGB = RGB(159, 2, 227)
End If
Sheet4.Range("A3").Value = tbSP.Value
Sheet4.Range("A4").Value = tbDROP.Value
Sheet4.Range("A5").Value = cmbCAT.Text
Sheet4.Range("A6").Value = tbUS.Value
Sheet4.Range("A7").Value = tbTITLE.Text
Sheet4.Range("A8").Value = cmbResource.Text
Sheet4.Range("A9").Value = tbDES.Text
Sheet4.Range("A10").Value = cmbStatus.Text
ActiveSheet.Shapes(Sheet4.Range("B1")).TextFrame.Characters.Caption = tbSP & "-" & tbDROP & "." & cmbCAT & "." & tbUS & vbNewLine & _
"Resource: " & cmbResource & vbNewLine & _
"Description: " & tbDES & vbNewLine
'Update if status is "In progress"
If Sheet4.Range("A10") = "In Prog" Then
With ActiveSheet.Shapes(Sheet4.Range("B1")).line
.Weight = 5
.ForeColor.RGB = RGB(2, 199, 6)
End With
Dim Box1 As Shape
Set Box1 = Sheet1.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveSheet.Shapes(Sheet4.Range("B1")).Left + ActiveSheet.Shapes(Sheet4.Range("B1")).Width - 50, ActiveSheet.Shapes(Sheet4.Range("B1")).TOP + ActiveSheet.Shapes(Sheet4.Range("B1")).Height - 20, 50, 20)
Box1.Fill.ForeColor.RGB = RGB(2, 199, 6)
Box1.OLEFormat.Object.Caption = "In Prog"
'Group the two boxes together
ShapeArray(0) = Box1.Name
ShapeArray(1) = ActiveSheet.Shapes(Sheet4.Range("B1")).Name
**ActiveSheet.Shapes.Range(Array(ShapeArray(0), ShapeArray(1))).Group**
'Update if Status is "done"
ElseIf Sheet4.Range("A10") = "Done" Then
With ActiveSheet.Shapes(Sheet4.Range("B1")).line
.Weight = 5
.ForeColor.RGB = RGB(61, 134, 212)
End With
Dim Box2 As Shape
Set Box2 = Sheet1.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveSheet.Shapes(Sheet4.Range("B1")).Left + ActiveSheet.Shapes(Sheet4.Range("B1")).Width - 50, ActiveSheet.Shapes(Sheet4.Range("B1")).TOP + ActiveSheet.Shapes(Sheet4.Range("B1")).Height - 20, 50, 20)
Box2.Fill.ForeColor.RGB = RGB(61, 134, 212)
Box2.OLEFormat.Object.Caption = "Done"
'Group the two boxes together
ShapeArr(0) = Box2.Name
ShapeArr(1) = ActiveSheet.Shapes(Sheet4.Range("B1")).Name
**ActiveSheet.Shapes.Range(Array(ShapeArr(0), ShapeArr(1))).Group**
End If
Unload UF2
End Sub
It seems the array is not being setup right. Does this have to do with the original shape already being in a group? I tried ungrouping but this causes another error for the first time status change as it ungroup the shape but it hasn't belonged to a group yet. I didn't experience this when i used two separate buttons to change status.

Excel - macro to important data from several workbooks based on cell name

I've been trying to make the below code work, and it did yesterday evening, but somehow this morning upon opening Excel it stopped functioning. Essentially, I'm using a vlookup macro to important data from various workbooks, and the workbook names depend on the respective "title" of that row. First, I check with an if statement whether the file actually exists; if it doesn't, I want to highlight the title cell red, and move onto the next row to carry out the same check. If the file does exist, I want to populate the row with the appropriate data and highlight the title cell with white colour.
Below my code - I'd really appreciate if you could take a look and help me out!
Public Function FileFolderExists(strFullPath As String) As Boolean
On Error GoTo NextStep
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
NextStep:
On Error GoTo 0
End Function
Private Sub CommandButton1_Click()
Dim wsi As Worksheet
Dim wse As Worksheet
Dim j As Integer
Dim i As Integer
Set wsi = ThisWorkbook.Sheets("Income")
Set wse = ThisWorkbook.Sheets("Expense")
j = 3
For i = 1 To 46
If FileFolderExists(wsi.Cells(5, i + 2).Value & ".xlsx") Then
wsi.Range(wsi.Cells(6, j), wsi.Cells(51, j)).Formula = "=VLOOKUP(index($B$6:$AV$51,row()-5,1),'[" & wsi.Cells(5, i + 2).Value & ".xlsx]Sheet1'!$A$1:$E$70,4,FALSE)"
Sheets("Mark-Up Table").Cells(i + 5, 2).Interior.Color = RGB(255, 255, 255)
Sheets("Mark-Up Table").Cells(5, i + 2).Interior.Color = RGB(255, 255, 255)
Else
Sheets("Mark-Up Table").Cells(i + 5, 2).Interior.Color = RGB(255, 0, 0)
Sheets("Mark-Up Table").Cells(5, i + 2).Interior.Color = RGB(255, 0, 0)
End If
If FileFolderExists(wse.Cells(5, i + 2).Value & ".xlsx") Then
wse.Range(wse.Cells(6, j), wse.Cells(51, j)).Formula = "=VLOOKUP(index($B$6:$AV$51,row()-5,1),'[" & wse.Cells(5, i + 2).Value & ".xlsx]Sheet2'!$A$1:$E$70,5,FALSE)"
Else
'do nothing
End If
j = j + 1
Next i
End Sub
I have managed to solve the issue. For people who might be facing similar problems, please see below:
Private Sub CommandButton1_Click()
Dim strPath As String
Dim wsi As Worksheet
Dim wse As Worksheet
Dim j As Integer
Dim i As Integer
Set wsi = ThisWorkbook.Sheets("Income")
Set wse = ThisWorkbook.Sheets("Expense")
strPath = Sheets("Mark-Up Table").Range("H3").Value
j = 3
For i = 1 To 46
If Dir(strPath & wsi.Cells(i + 5, 2).Value & ".xlsx") = vbNullString Then
Sheets("Mark-Up Table").Cells(i + 5, 2).Interior.Color = RGB(255, 0, 0)
Sheets("Mark-Up Table").Cells(5, i + 2).Interior.Color = RGB(255, 0, 0)
Else
wsi.Range(wsi.Cells(3 + j, 3), wsi.Cells(3 + j, 48)).Formula = "=VLOOKUP(index($C$5:$AV$51,1,column()-2),'[" & wsi.Cells(i + 5, 2).Value & ".xlsx]Sheet1'!$A$1:$E$70,4,FALSE)"
Sheets("Mark-Up Table").Cells(i + 5, 2).Interior.Color = RGB(255, 255, 255)
Sheets("Mark-Up Table").Cells(5, i + 2).Interior.Color = RGB(255, 255, 255)
End If
If Dir(strPath & wse.Cells(5, i + 2).Value & ".xlsx") = vbNullString Then
'do nothing
Else
wse.Range(wse.Cells(6, j), wse.Cells(51, j)).Formula = "=abs(VLOOKUP(index($B$6:$AV$51,row()-5,1),'[" & wse.Cells(5, i + 2).Value & ".xlsx]Sheet1'!$A$1:$E$70,5,FALSE))"
End If
j = j + 1
Next i
End Sub

Resources