首页 > 解决方案 > Excel VBA change shape fill to an image in a loop

问题描述

I have created a workbook for printing reports from a central dataset. The data is loaded using an index match formula which my VBA iterates through and adds 1 to the reference to get to the next set of data.

The only issue I’m having is that my images aren’t getting updated.

cur_val = to the starting reference number

props_cnt = to the last reference number image_path = the link to an image which is generated by an index match formula which updates as the reference number is changed

map_path = the link to a map (image) which is generated by an index match formula which updates as the reference number is changed

export_report_as_pdf = simple function to print to pdf

Any ideas why the image isn’t getting updated in this loop?

Public Sub Print_all_to_pdf()
Dim i As Integer
Dim cur_val As Integer
Dim props_cnt As Integer
Dim image_path As Variant
Dim map_path As Variant

Application.ScreenUpdating = False


cur_val = Range("Selected_Property").Value
props_cnt = Range("Max_Property").Value

For i = cur_val To props_cnt
    Range("Selected_Property").Value = i
    
    image_path = Range("Image_Path").Value
    map_path = Range("Map_Path").Value
    
    ActiveSheet.Shapes.Range(Array("Image")).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .UserPicture _
        image_path
        .TextureTile = msoFalse
        .RotateWithObject = msoTrue
        
    ActiveSheet.Shapes.Range(Array("Map")).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .UserPicture _
        map_path
        .TextureTile = msoFalse
        .RotateWithObject = msoTrue

    Call export_report_as_pdf
    
Next i

Range("Selected_Property").Value = cur_val

Application.ScreenUpdating = True
End Sub

标签: excelvba

解决方案


推荐阅读