链接到其他文件并保持格式

时间:2016-12-08 18:33:23

标签: vba excel-vba excel

我正在使用Excel VBA从目录中的许多未打开的工作簿中获取单元格内容。我正在使用的代码是这样的......

Sub Test()

Dim ctr
Range("A3:f200") = ""
Range("k7") = Date
MyPath = "U:\ATTENDANCE ADULT 12\"
myname = Dir(MyPath, vbNormal)
ctr = 3
Do While myname <> ""
  If myname <> "." And myname <> ".." Then
    If (GetAttr(MyPath & myname) And vbNormal) = vbNormal Then
      If ctr = 3 Then Range("h1") = "='" & MyPath & "[" & myname & "]TRMII'!R10C1"
      Range("a" & ctr) = myname
      Range("b" & ctr) = "='" & MyPath & "[" & myname & "]TRMII'!R12C6"
      Range("c" & ctr) = "='" & MyPath & "[" & myname & "]TRMII'!R4C5"
      Range("d" & ctr) = "='" & MyPath & "[" & myname & "]TRMII'!R2C13"
      Range("e" & ctr) = "='" & MyPath & "[" & myname & "]TRMII'!R3C13"
      Range("f" & ctr) = "='" & MyPath & "[" & myname & "]TRMII'!R8C5"

      ctr = ctr + 1
    End If
  End If
  myname = Dir
Loop

End Sub

效果很好!但我希望它不仅可以获取单元格的内容,还可以获取格式。那可能吗?请指教。谢谢!

0 个答案:

没有答案