返回列表 上一主題 發帖

一個查詢的表單,

回復 1# hong912
UserForm的程式碼
  1. Option Explicit                            '在模組層次中強迫每個在模組�堛瘍僂くㄔ眸楨�確的宣告。
  2. Private Const 編號 = 3                     '資料庫的欄位列號
  3. Private Const ThePicture = "d:\ttt.gif"    '設立匯出圖片的路徑檔名
  4. Dim Ar(10), Sh As Worksheet
  5. 'Dim Ar()
  6. Private Sub UserForm_Initialize()          '表單初始化的預設事件程序
  7.     Dim I As Integer
  8.     Set Sh = Sheets("Sheet1")              '資料庫的工作表
  9.     With Sh
  10.         ComboBox1.List = .Range("a4", .[a4].End(xlDown)).Value '指定 ComboBox1的內容
  11.     End With
  12.     'Dim Ar()時可用下式
  13.     'Ar = Array(TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10, TextBox11)
  14.     For I = 0 To UBound(Ar)
  15.       Set Ar(I) = Me.Controls("TextBox" & I + 1) '陣列的元素設為TextBox (物件)
  16.     Next
  17.     Image1.PictureSizeMode = fmPictureSizeModeStretch
  18.     '參數fmPictureSizeModeStretch= 1 :調整圖片大小以填滿表單或活頁,此設定會造成圖片的水平與垂直方向比例被扭曲。
  19. End Sub
  20. Private Sub ComboBox1_Change()
  21.     Dim Sp As Picture, I As Integer
  22.     If ComboBox1.ListIndex > -1 Then   '選擇 ComboBox1的內容
  23.         For I = 0 To UBound(Ar)
  24.             Ar(I).Text = Sh.Cells(編號 + ComboBox1, I + 2)
  25.         Next
  26.     End If
  27.     With Sh
  28.         For Each Sp In .Pictures             '尋找N欄中的圖片,複製之
  29.             If Sp.TopLeftCell.Address(0, 0) = "N" & 編號 + ComboBox1 Then Sp.Copy: Exit For
  30.         Next
  31.         '利用圖表匯出存檔
  32.         With .ChartObjects.Add(1, 1, Sp.Width, Sp.Height)           '新增 圖表
  33.              .Chart.Paste                                           '貼上 圖片
  34.              .Chart.Export Filename:=ThePicture                     '匯出 圖片
  35.              .Delete                                                '刪除 圖表
  36.         End With
  37.     End With
  38.     Image1.Picture = LoadPicture(ThePicture)                         'Image1 指定圖片
  39. End Sub
  40. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)  '表單絕束時 預設事件程序
  41.     If Dir(ThePicture) <> "" Then Kill ThePicture                   '刪除 匯出的圖片
  42. End Sub
複製代碼

TOP

回復 4# hong912
試試看
  1. Private Sub UserForm_Click() '在表單沒有控制項的地方按下滑鼠左鍵的事件
  2. '1當圖片傳回表單,  圖片不清, 可有方辦解決 ':修改顯示背景圖片的方式
  3.     With Image1
  4.         If .PictureSizeMode = fmPictureSizeModeClip Then
  5.             .PictureSizeMode = fmPictureSizeModeStretch
  6.         ElseIf .PictureSizeMode = fmPictureSizeModeStretch Then
  7.             .PictureSizeMode = fmPictureSizeModeZoom
  8.         ElseIf .PictureSizeMode = fmPictureSizeModeZoom Then
  9.             .PictureSizeMode = fmPictureSizeModeClip
  10.         End If
  11.     End With
  12. ' [   常                                   數]  [值]  [ 說      明]
  13. 'fmPictureSizeModeClip          0    裁掉圖片多出來的部分 ( 預設 )。
  14. 'fmPictureSizeModeStretch    1    調整圖片大小以填滿表單或活頁,此設定會造成圖片的水平與垂直方向比例被扭曲。
  15. 'fmPictureSizeModeZoom      3     放大圖片,但不扭曲圖片水平與垂直方向的比例。
  16. End Sub
  17. Private Sub ComboBox1_Change()
  18.     Dim Sp As Picture, I As Integer
  19.     If ComboBox1.ListIndex > -1 Then   '選擇 ComboBox1的內容
  20.         For I = 0 To UBound(Ar)
  21.             If I <> UBound(Ar) Then
  22.                 Ar(I).Text = Sh.Cells(編號 + ComboBox1, I + 2)
  23.             Else
  24.                 '2, 在第11個TextBox11物件中, 可否做到把sheet1第11欄及12欄合拼顯示,如123aaa
  25.                  Ar(I).Text = Sh.Cells(編號 + ComboBox1, I + 2) & Sh.Cells(編號 + ComboBox1, I + 3)
  26.              End If
  27.         Next
  28.     End If
  29.     With Sh
  30.         For Each Sp In .Pictures             '尋找N欄中的圖片,複製之
  31.             If Sp.TopLeftCell.Address(0, 0) = "N" & 編號 + ComboBox1 Then Sp.Copy: Exit For
  32.         Next
  33.         '利用圖表匯出存檔
  34.         With .ChartObjects.Add(1, 1, Sp.Width, Sp.Height)           '新增 圖表
  35.              .Chart.Paste                                           '貼上 圖片
  36.              .Chart.Export Filename:=ThePicture                     '匯出 圖片
  37.              .Delete                                                '刪除 圖表
  38.         End With
  39.     End With
  40.     Image1.Picture = LoadPicture(ThePicture)                         'Image1 指定圖片
  41. End Sub
複製代碼

TOP

回復 7# hong912
ComboBox1 增加一欄內容
  1. Private Sub UserForm_Initialize()          '表單初始化的預設事件程序
  2.     Dim I As Integer, e As Range
  3.     Set Sh = Sheets("Sheet1")              '資料庫的工作表
  4.     With Sh
  5.         For Each e In .Range("a4", .[a4].End(xlDown))  '指定 ComboBox1的內容
  6.             With ComboBox1                             'ComboBox1.ColumnCount=1 系統預設 顯示1欄資料
  7.                ' .ColumnCount=2                        '可顯示2欄資料
  8.                 .AddItem
  9.                 'AddItem 方法 在一個單列清單方塊或下拉式清單方塊中加入一個項目。在一個多列清單方塊或下拉式清單方塊中加入一行。
  10.                 .List(.ListCount - 1, 0) = e            'ComboBox第1欄 : 字串
  11.                 .List(.ListCount - 1, 1) = e.Row - 編號 'ComboBox第2欄 : 列號 1 - ....
  12.             End With
  13.         Next
  14.     End With
  15.     For I = 0 To UBound(Ar)
  16.       Set Ar(I) = Me.Controls("TextBox" & I + 1) '陣列的元素設為TextBox (物件)
  17.     Next
  18.     Image1.PictureSizeMode = fmPictureSizeModeStretch
  19.     '參數fmPictureSizeModeStretch= 1 :調整圖片大小以填滿表單或活頁,此設定會造成圖片的水平與垂直方向比例被扭曲。
  20. End Sub
  21. Private Sub ComboBox1_Change()
  22.     Dim Sp As Picture, I As Integer, R As Integer
  23.     If ComboBox1.ListIndex > -1 Then                    '選擇 ComboBox1的內容
  24.         R = ComboBox1.List(ComboBox1.ListIndex, 1)      'ComboBox第2欄 : 列號 1....
  25.         For I = 0 To UBound(Ar)
  26.             If I <> UBound(Ar) Then
  27.                 Ar(I).Text = Sh.Cells(編號 + R, I + 2)  '列號 : 編號 + R
  28.             Else
  29.                 '2, 在第11個TextBox11物件中, 可否做到把sheet1第11欄及12欄合拼顯示,如123aaa
  30.                  Ar(I).Text = Sh.Cells(編號 + R, I + 2) & Sh.Cells(編號 + R, I + 3)
  31.              End If
  32.         Next
  33.     End If
  34.     With Sh
  35.         For Each Sp In .Pictures             '尋找N欄中的圖片,複製之
  36.             If Sp.TopLeftCell.Address(0, 0) = "N" & 編號 + R Then Sp.Copy: Exit For
  37.         Next
  38.         '利用圖表匯出存檔
  39.         With .ChartObjects.Add(1, 1, Sp.Width, Sp.Height)           '新增 圖表
  40.              .Chart.Paste                                           '貼上 圖片
  41.              .Chart.Export Filename:=ThePicture                     '匯出 圖片
  42.              .Delete                                                '刪除 圖表
  43.         End With
  44.     End With
  45.     Image1.Picture = LoadPicture(ThePicture)                         'Image1 指定圖片
  46. End Sub
複製代碼

TOP

回復 11# hong912
9# 的程式碼, 是依據你7# 附檔修改的,我執行時並沒有你提到的錯誤
請查看你檔案中的圖片是否有移動,離開7# 附檔的位置.

TOP

回復 25# 周大偉
  1. Dim PicAr() As Picture '圖片陣列
  2. Dim fs As String  ' *** = "E:\temp.jpg" '暫存圖片目錄位置
  3. Private Const r = 4 '資料起始列號
  4. Private Sub UserForm_Initialize() '表單初始化
  5.     Dim Pic As Picture
  6.     fs = CurDir & "\temp.jpg"  '*** 這裡修改為當下的目錄 ( CurDir )為暫存圖片目錄位置 ***
  7.     With Sheet1
  8.         ReDim PicAr(.Pictures.Count)
  9.         For Each Pic In .Pictures '將每個圖片置入陣列
  10.             Set PicAr(Pic.TopLeftCell.Row - r) = Pic
  11.         Next
  12.         ComboBox1.List = .Range("A4", .[A4].End(xlDown).Offset(, 12)).Value '下拉清單內容
  13.     End With
  14.     Image1.PictureSizeMode = fmPictureSizeModeStretch '圖片載入的型態
  15. End Sub
複製代碼

TOP

回復 29# 周大偉
CurDir: 傳回前目錄位置
當使用開啟舊檔指令: 所看到的目錄位置

TOP

本帖最後由 GBKEE 於 2012-9-30 15:51 編輯

回復 33# hong912
修改 查詢檔案 表單程式碼如下
  1. Dim PicAr() As Picture '圖片陣列
  2. Dim fs As String, Sh As Worksheet    ' 表單查詢表 = "E:\temp.jpg" '暫存圖片目錄位置
  3. Private Const r = 4 '資料起始列號
  4. Private Sub UserForm_Initialize() '表單初始化
  5.     Dim Pic As Picture
  6.     查看資料庫
  7.     fs = CurDir & "\temp.jpg"  '表單查詢表 這裡修改為當下的目錄 ( CurDir )為暫存圖片目錄位置 表單查詢表
  8.     With Sh
  9.         ReDim PicAr(.Pictures.Count)
  10.         For Each Pic In .Pictures '將每個圖片置入陣列
  11.             Set PicAr(Pic.TopLeftCell.Row - r) = Pic
  12.         Next
  13.         ComboBox1.List = .Range("A4", .[A4].End(xlDown).Offset(, 12)).Value '下拉清單內容
  14.     End With
  15.     Image1.PictureSizeMode = fmPictureSizeModeStretch '圖片載入的型態
  16. End Sub
  17. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) '關閉表單
  18.     If Dir(fs) <> "" Then Kill fs '刪除暫存圖片檔案
  19.     Sh.Parent.Close 0             '關閉資料庫檔案
  20. End Sub
  21. Private Sub 查看資料庫()
  22.     Dim 資料庫 As String, Wo As Workbook, Msg As Boolean        'Boolean型態的預設值為 False
  23.     資料庫 = "D:\資料庫.XLS"                                    '資料庫檔案的路徑目錄
  24.     For Each Wo In Workbooks                                    '活頁簿物件集合
  25.         If Wo.FullName = 資料庫 Then                            '資料庫檔案開啟中
  26.             Msg = True
  27.             Set Sh = Wo.Sheets(1)                                   '將變數指定為第一個工作表
  28.             Exit For
  29.         End If
  30.     Next
  31.     Application.ScreenUpdating = False
  32.     'ScreenUpdating 屬性 如果螢幕更新功能是開啟的則為 True。讀/寫 Boolean。
  33.      If Msg = False Then Set Sh = CreateObject(資料庫).Sheets(1)
  34.     '資料庫檔案如未開啟, 開啟它:將變數指定為第一個工作表
  35.     Application.ScreenUpdating = True
  36. End Sub
複製代碼

TOP

        靜思自在 : 【停滯不前,終無所得】人都迷於尋找奇蹟,因而停滯不前;縱使時間再多、路再長,也了無用處,終無所得。
返回列表 上一主題