Vba 需要将更新推送到已关闭的工作簿,源范围有问题
正如标题所说,我正在尝试将源工作簿中某个单元格区域的内容推送到目标(已关闭)工作簿中的同一区域。我正在使用以下代码:Vba 需要将更新推送到已关闭的工作簿,源范围有问题,vba,excel,Vba,Excel,正如标题所说,我正在尝试将源工作簿中某个单元格区域的内容推送到目标(已关闭)工作簿中的同一区域。我正在使用以下代码: Option Explicit Sub UpdateAdminBook() Dim MyPath As String Dim MyFile As String Dim Wkb As Workbook Dim Cnt As Long Application.ScreenUpdating =
Option Explicit
Sub UpdateAdminBook()
Dim MyPath As String
Dim MyFile As String
Dim Wkb As Workbook
Dim Cnt As Long
Application.ScreenUpdating = False
MyPath = "C:FILEPATH\" 'change the path accordingly
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "Administration.xlsx")
Cnt = 0
Do While Len(MyFile) > 0
Cnt = Cnt + 1
Set Wkb = Workbooks.Open(MyPath & MyFile)
Wkb.Worksheets("Administration").Range("D18:D37").Value = ActiveWorkbook.Sheets("Administration").Range("D18:D37") 'change the new value accordingly
Wkb.Close savechanges:=True
MyFile = Dir
Loop
If Cnt > 0 Then
MsgBox "Completed...", vbExclamation
Else
MsgBox "No files were found!", vbExclamation
End If
Application.ScreenUpdating = True
End Sub
选项显式
Sub-UpdatedMinBook()
将MyPath设置为字符串
将MyFile设置为字符串
将Wkb设置为工作簿
暗淡的碳纳米管
Application.ScreenUpdating=False
MyPath=“C:FILEPATH\”,相应地更改路径
如果正确(MyPath,1)“\”则MyPath=MyPath&“\”
MyFile=Dir(MyPath&“Administration.xlsx”)
Cnt=0
当Len(MyFile)>0时执行
Cnt=Cnt+1
设置Wkb=Workbooks.Open(MyPath&MyFile)
Wkb.Worksheets(“Administration”).Range(“D18:D37”).Value=ActiveWorkbook.Sheets(“Administration”).Range(“D18:D37”)'相应地更改新值
Wkb.Close savechanges:=真
MyFile=Dir
环
如果Cnt>0,则
MsgBox“已完成…”,请使用感叹号
其他的
MsgBox“未找到任何文件!”,VBEQUOTE
如果结束
Application.ScreenUpdating=True
端接头
我在“ActiveWorkbook”开始时遇到问题,我总是得到“TRUE”或空白。你知道我该怎么解决这个问题吗 打开工作簿时,它将成为活动工作簿,而不是原始工作簿。换成这个
Dim wbSrc As Workbook
Set wbSrc = ActiveWorkbook
'...
Do ...
' ...
Set Wkb = Workbooks.Open(MyPath & MyFile)
Wkb.Worksheets("Administration").Range("D18:D37").Value = wbSrc.Sheets("Administration").Range("D18:D37") 'change the new value accordingly
打开工作簿时,它将成为活动工作簿,而不是原始工作簿。换成这个
Dim wbSrc As Workbook
Set wbSrc = ActiveWorkbook
'...
Do ...
' ...
Set Wkb = Workbooks.Open(MyPath & MyFile)
Wkb.Worksheets("Administration").Range("D18:D37").Value = wbSrc.Sheets("Administration").Range("D18:D37") 'change the new value accordingly
您可以将复制范围假定为
With-End With
块中的引用
Sub UpdateAdminBook()
Dim MyPath As String
Dim MyFile As String
Dim Cnt As Long
Application.ScreenUpdating = False
MyPath = "C:FILEPATH\" 'change the path accordingly
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "Administration.xlsx")
With ActiveWorkbook.Sheets("Administration").Range("D18:D37")
Cnt = 0
Do While Len(MyFile) > 0
Cnt = Cnt + 1
Workbooks.Open(MyPath & MyFile).Worksheets("Administration").Range("D18:D37").Value = .Value 'change the new value accordingly
ActiveWorkbook.Close savechanges:=True
MyFile = Dir
Loop
End With
If Cnt > 0 Then
MsgBox "Completed...", vbExclamation
Else
MsgBox "No files were found!", vbExclamation
End If
Application.ScreenUpdating = True
End Sub
Sub-UpdateAdminBook()
将MyPath设置为字符串
将MyFile设置为字符串
暗淡的碳纳米管
Application.ScreenUpdating=False
MyPath=“C:FILEPATH\”,相应地更改路径
如果正确(MyPath,1)“\”则MyPath=MyPath&“\”
MyFile=Dir(MyPath&“Administration.xlsx”)
使用ActiveWorkbook.Sheets(“管理”).Range(“D18:D37”)
Cnt=0
当Len(MyFile)>0时执行
Cnt=Cnt+1
Workbooks.Open(MyPath和MyFile).Worksheets(“管理”).Range(“D18:D37”).Value=.Value”相应地更改新值
ActiveWorkbook.Close savechanges:=True
MyFile=Dir
环
以
如果Cnt>0,则
MsgBox“已完成…”,请使用感叹号
其他的
MsgBox“未找到任何文件!”,VBEQUOTE
如果结束
Application.ScreenUpdating=True
端接头
您可以假设您的复制范围作为With-End With
块中的参考
Sub UpdateAdminBook()
Dim MyPath As String
Dim MyFile As String
Dim Cnt As Long
Application.ScreenUpdating = False
MyPath = "C:FILEPATH\" 'change the path accordingly
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "Administration.xlsx")
With ActiveWorkbook.Sheets("Administration").Range("D18:D37")
Cnt = 0
Do While Len(MyFile) > 0
Cnt = Cnt + 1
Workbooks.Open(MyPath & MyFile).Worksheets("Administration").Range("D18:D37").Value = .Value 'change the new value accordingly
ActiveWorkbook.Close savechanges:=True
MyFile = Dir
Loop
End With
If Cnt > 0 Then
MsgBox "Completed...", vbExclamation
Else
MsgBox "No files were found!", vbExclamation
End If
Application.ScreenUpdating = True
End Sub
Sub-UpdateAdminBook()
将MyPath设置为字符串
将MyFile设置为字符串
暗淡的碳纳米管
Application.ScreenUpdating=False
MyPath=“C:FILEPATH\”,相应地更改路径
如果正确(MyPath,1)“\”则MyPath=MyPath&“\”
MyFile=Dir(MyPath&“Administration.xlsx”)
使用ActiveWorkbook.Sheets(“管理”).Range(“D18:D37”)
Cnt=0
当Len(MyFile)>0时执行
Cnt=Cnt+1
Workbooks.Open(MyPath和MyFile).Worksheets(“管理”).Range(“D18:D37”).Value=.Value”相应地更改新值
ActiveWorkbook.Close savechanges:=True
MyFile=Dir
环
以
如果Cnt>0,则
MsgBox“已完成…”,请使用感叹号
其他的
MsgBox“未找到任何文件!”,VBEQUOTE
如果结束
Application.ScreenUpdating=True
端接头
不应该MyPath=“C:FILEPATH\”
是MyPath=“C:\FILEPATH\”
(额外\
)它实际上正在推送到目标工作簿中的正确范围,但推送的内容是我的问题。“真”或空白出现在正确的范围内。我正在尝试从源工作簿中的相同范围获取内容,一旦打开工作簿,活动工作簿将更改为您刚才打开的工作簿。我认为您需要在函数开始时将活动工作簿分配给一个变量,然后使用该变量。不应该是MyPath=“C:FILEPATH\”
MyPath=“C:\FILEPATH\”(额外\
)它实际上正在推送到目标工作簿中的正确范围,但推送的内容是我的问题。“真”或空白出现在正确的范围内。我正在尝试从源工作簿中的相同范围获取内容,一旦打开工作簿,活动工作簿将更改为您刚才打开的工作簿。我认为您需要在函数的开头将活动工作簿分配给一个变量,然后使用它。仍然只返回空格。为了澄清,我是否只需在Sub UpdateAdminBook()之后添加前两行?然后对最后两个进行更改?是的,假设运行代码时要从中复制数据的wb是ActiveWorkbook,但仍然只返回空格。为了澄清,我是否只需在Sub UpdateAdminBook()之后添加前两行?然后对最后两个进行更改?是的,假设运行代码时要从中复制数据的wb是ActiveWorkbook