简体   繁体   English

我希望能够从单元格值设置工作簿,它似乎由于某种原因不起作用

[英]i want to be able to set workbook from a cell value, it doesn't seem to work for some reason

I have the following code: 我有以下代码:

Sub export_toFEP2()
 Dim wb As Workbook, wb2 As Workbook
 Dim ws As Worksheet, ws2 As Worksheet
Dim x As String, lastrow As String
Dim lRow As Long, kRow As Long, i As Long
Dim u As String

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

ThisWorkbook.Activate

Sheets("FEP Selection").Activate
u = Sheets("FEP Selection").Range("File_Name").Value2
Set wb = Workbooks(u)
Set ws = wb.Worksheets("Ship Arrivals")
Set wb2 = ThisWorkbook
Set ws2 = wb2.Worksheets("FEP copy")

lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

If ws2.Range("D" & i).Value = "TRUE" Then

lRow = Application.WorksheetFunction.Match(ws2.Range("A" & i).Value2, ws.Range("A2:CS2"), 0)
kRow = Application.WorksheetFunction.Match(CLng(ws2.Range("B" & i).Value), ws.Range("A1:A145"), 0)

If lRow > 0 And kRow > 0 Then
MsgBox lRow
MsgBox kRow
ws.Cells(kRow, lRow).Value = ws2.Range("C" & i).Value
End If
End If
Next i

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

The problem i am having is that it does not do anything but does not give any errors either. 我遇到的问题是它没有做任何事情,但也没有给出任何错误。 the variable should pick up a value like "A.xls" (that's the value of file name range), it changes every time, hence, i have a range with the file name. 变量应该选择像“A.xls”这样的值(这是文件名范围的值),它每次都会改变,因此,我有一个带有文件名的范围。 if i change to the 如果我改变了

Set wb = Workbooks(u)

to

Set wb = Workbooks("A1.xls")

it seems to work, but that defeats the purpose as the file name is variable. 它似乎有效,但由于文件名是可变的,因此失败了。

thank you for your help :) 谢谢您的帮助 :)

If the workbook in question is open, omit the .xls when you set the value of wb . 如果有问题的工作簿是打开的,则在设置wb的值时省略.xls

Something like: 就像是:
Set wb = Workbooks(Replace(u, ".xls", ""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM