snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ให้VBAหาภาพในfoldersและใส่ภาพในไฟล์excelอัตโนมัติ
ต่อจากให้ VBA แยกชีทค่ะ
ปัจจุบันใช้ code
Sub Macro1()
Dim i As Integer
For i = 1 To ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(i).Copy
'ActiveSheet.Name = Workbooks("sheet.xlsm").Sheets(i).Range("O4").Value
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range("O4").Value & Range("j13").Value & ".xls", FileFormat:=xlExcel8, CreateBackup:=False
ActiveWorkbook.Close True
Next i
End Sub
Insert Picture & Auto Size To Active Cell Dimmensions
Sub test()
On Error Resume Next
Set pic = ActiveSheet.Pictures.Insert("C:
ange.gif")
On Error Goto 0
If Not pic Is Nothing Then 'Found it!'
Set rng = ActiveCell
With pic
.Height = rng.Height
.Width = rng.Width
.Left = rng.Left
.Top = rng.Top
End With
Sub ShowPicture1()
Dim r As Range, ra As Range
Dim imgIcon As Object
Dim obj As Object
On Error Resume Next
With Worksheets("Sheet1")
Set ra = .Range("G4", .Range("F65536").End(xlUp).Offset(0, 1))
End With
For Each obj In ActiveSheet.Shapes
If Left(obj.Name, 4) = "Pict" Then
obj.Delete
End If
Next obj
For Each r In ra
Set imgIcon = ActiveSheet.Shapes.AddPicture( _
Filename:="D:\" & r.Offset(0, -1).Value & ".jpg", LinkToFile:=False, _
SaveWithDocument:=True, Left:=r.Left, Top:=r.Top, _
Width:=r.Width, Height:=r.Height)
Next r
End Sub
อาจารย์ค่ะ ในไฟล์ AddPic.xlsm มี code คือ
Sub Macro1()
Dim i As Integer
For i = 1 To ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(i).Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range("o4").Value & Range("j13").Value & ".xls", FileFormat:=xlExcel8, CreateBackup:=False
Range("c43").Select
Selection.FormulaR1C1 = "=R[71]C[1]&""\""&R[72]C[1]&""\""&R[73]C[1]&""\""&R[74]C[1]&""\""&R[75]C[1]&""\""&R119C4"
ActiveSheet.Pictures.Insert (Selection.Value)
Range("l43").Select
Selection.FormulaR1C1 = "=R[71]C[-8]&""\""&R[72]C[-8]&""\""&R[73]C[-8]&""\""&R[74]C[-8]&""\""&R[75]C[-8]&""\""&R120C4"
ActiveSheet.Pictures.Insert (Selection.Value)
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Sub RenameInFolder(ByVal FD As String)
Dim FN As String, FileList() As String
Dim I As Integer, J As Integer, Temp As String
Dim MinFN1 As String, MinFN2 As String
If Right(FD, 1) <> "\" Then FD = FD & "\"
FN = Dir(FD & "*.JPG")
Do While Len(FN) > 0
ReDim Preserve FileList(I)
FileList(I) = FN
FN = Dir()
I = I + 1
Loop
If I < 3 Then
MsgBox FD & " only " & I & " files"
Exit Sub
End If
For I = 0 To UBound(FileList) - 1
For J = 1 To UBound(FileList)
If FileList(I) > FileList(J) Then
Temp = FileList(I)
FileList(I) = FileList(J)
FileList(J) = Temp
End If
Next
Next
Name (FD & FileList(1)) As (FD & "1.JPG")
Name (FD & FileList(2)) As (FD & "2.JPG")
End Sub
'Code ส่วนที่ 2 ทำงานได้ดีค่ะ
Sub Macro1()
Dim I As Integer
For I = 1 To ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(I).Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range("o4").Value & Range("j13").Value & ".xls", FileFormat:=xlExcel8, CreateBackup:=False
Range("c43").Select
Selection.FormulaR1C1 = "=R[71]C[1]&""\""&R[72]C[1]&""\""&R[73]C[1]&""\""&R[74]C[1]&""\""&R[75]C[1]&""\""&R119C4"
On Error Resume Next
With ActiveSheet.Pictures.Insert(Selection.Value)
.Top = Range("f55:j86").Top
.Left = Range("f55:j86").Left
If .Height > .Width Then
.Height = Range("f55:j80").Height
Else
.Width = Range("f55:j80").Width
End If
End With
With ActiveSheet.Pictures.Insert(Selection.Value)
.Top = Range("m55:s86").Top
.Left = Range("m55:s86").Left
If .Height > .Width Then
.Height = Range("m55:s86").Height
Else
.Width = Range("m55:s86").Width
End If
End With
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Next I
End Sub
You do not have the required permissions to view the files attached to this post.
Excel 2007 VBA Programming for Dummies <== เนื้อหาเน้นผู้เริ่มต้น
Excel 2010 Power Programming with VBA
Excel 2007 VBA Programmer's Reference
Professional Excel development
Microsoft Excel VBA Professional Projects