Board logo

標題: [發問] VBA指定格內插入對應的圖片(程式碼討論) [打印本頁]

作者: liebe    時間: 2017-9-6 20:01     標題: VBA指定格內插入對應的圖片(程式碼討論)

各位大大們好!

目前想製作能在指定格內自動插入相對應名稱的客戶LOGO ( 插入圖片大小不能超過指定格的大小),若是找不到相對應名稱的圖片將會開啟資料夾(專門存放客戶LOGO的資料夾)
而程式撰寫部分遇到以下問題:
1.不管圖片格式如何,只要圖名與客戶名相符就能自動插入指定格中,目前寫法如下第七列 (X=Dir("")) ,不知大大們是否有更好得寫法?
2.圖片插入的地方有五個部份,表格大小都不一樣,不知是否能指定圖片大小與儲存格大小相同?
3.如何轉寫開啟指定資料夾   ((( S:archiv\LOGO
以上! 感謝各位大大!!
程式碼如下:(由於還使新手,若語法有錯再麻煩大大們糾正我一下)
  1. Sub Logo()

  2. If Sheets("Grunddefinition").Range("T5").Value <> 0 Or Sheets("Grunddefinition").Range("T5").Value <> "" Then

  3.    Dim A As String
  4.     A = Application.IfError(Application.VLookup(Sheets("Grunddefinition").Range("S5"), Sheets("Grunddefinition").Range("S5:T5"), 2, False), "")

  5.     Dim x As String
  6.     x = Dir("S:archiv\LOGO\" & A & ".png") 'Or x = Dir("S:archiv\LOGO\" & A & ".jpg")
  7.       
  8.       If x <> "" Then
  9.       
  10.          Set p = ActiveSheet.Pictures.Insert("S:archiv\LOGO\" & A & ".png") 'Or x = Dir("S:archiv\LOGO\" & A & ".jpg")
  11.          Set C = Sheets("Grunddefinition").Range("S1") And Sheets("Zeitablaufplan").Range("B5") And Sheets("Zeitablaufplan").Range("M5") And Sheets("Zeitablaufplan").Range("X5") And Sheets("Zeitablaufplan").Range("AI5")
  12.          
  13.    
  14.       
  15.       ElseIf x = "" Then
  16.       MsgBox "Error! Can't find the corresponding picture!"
  17.       MsgBox "You should check whether the picture is existing in the folder of Logo or check the name of the picture is corresponded with the name of customer!"
  18.       
  19. End If

  20. ElseIf Sheets("Grunddefinition").Range("T3").Value = 0 Or Sheets("Grunddefinition").Range("T3").Value = "" Then
  21. MsgBox "Please enter the name of the customer!"
  22. Sheets("Grunddefinition").Select
  23. Rows(5).Select
  24. End If


  25. End Sub
複製代碼

作者: GBKEE    時間: 2017-9-8 07:10

回復 1# liebe

你這語法錯誤 set 變數=物件, 物件只能是一個
  1. Set C = Sheets("Grunddefinition").Range("S1") And Sheets("Zeitablaufplan").Range("B5") And Sheets("Zeitablaufplan").Range("M5") And Sheets("Zeitablaufplan").Range("X5") And Sheets("Zeitablaufplan").Range("AI5")
複製代碼
關於圖片的匯入,請用本討論版的搜索功能,搜索 關鍵字"Pictures"本版有許多討論




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)