晚上好。我正在急需一些我正在编写的VBA代码的帮助。
Public TFOCUS As Integer ' Creates TFOCUS, which is the worksheet in focus
Public RFOCUS As Integer ' Creates RFOCUS, which is the row in focus
Public CFOCUS As String ' Creates CFOCUS, which is the column in focus
Public RECORD As Integer ' Creates RECORD, wich is the row that is having the record written to
FILEPATH.Worksheets(TFOCUS).Range(Cells(RFOCUS, B)).Value = Worksheets(3).Range(Cells(RECORD, A)).Value 'copies focus EmpID to destination
FILEPATH.Worksheets(TFOCUS).Range(Cells(4, CFOCUS)).Value = Worksheets(3).Range(Cells(RECORD, B)).Value 'copies focus Course to destination
FILEPATH.Worksheets(TFOCUS).Range(Cells(RFOCUS, CFOCUS)).Value = Worksheets(3).Range(Cells(RECORD, C)).Value 'copies focus Date to destination
CFOCUS = CFOCUS + 1 'moves focus to next column
RECORD = RECORD + 1 'creates next record
FILEPATH设置为外部Excel工作簿的路径。在这种情况下,TFOCUS设置为1,RFOCUS设置为5,CFOCUS设置为“Q”,RECORD设置为1.
目的是将外部Excel文档中的记录复制到活动电子表格中,然后通过移动单元格内容重新格式化它们。这将用于移动多个源,并且必须处理每个源文档中的每个选项卡(可能都被命名为不同的)。
我遇到的问题是,我在接收以下行时收到运行时错误13:类型不匹配错误:
FILEPATH.Worksheets(TFOCUS).Range(Cells(RFOCUS, B)).Value = Worksheets(3).Range(Cells(RECORD, A)).Value 'copies focus EmpID to destination
我假设这与使用TFOCUS作为整数或FILEPATH作为文件路径有关。任何人都可以建议:
提前感谢您的帮助。
答案 0 :(得分:0)
您没有向我们展示变量的分配位置,但是......
Public RFOCUS As Integer ' Creates RFOCUS, which is the row in focus Public CFOCUS As String ' Creates CFOCUS, which is the column in focus
尝试将CFOCUS
声明为Integer
。或者更好,作为Long
,以便您的代码在行32767之外工作(Integer
类型是16位并且已签名,因此32768是溢出值。)
此外,如果FILEPATH
是String
,那么您的代码无效:
FILEPATH设置为外部Excel工作簿的路径。
FILEPATH.Worksheets(TFOCUS)
它应该是Workbook
对象..但是你正在使用的标识符非常混乱。
Dim wb As Workbook
Set wb = Workbooks.Open(FILEPATH)
wb.Worksheets(TFOCUS).Range(Cells(RFOCUS, B)).Value = Worksheets(3).Range(Cells(RECORD, A)).Value 'copies focus EmpID to destination
wb.Worksheets(TFOCUS).Range(Cells(4, CFOCUS)).Value = Worksheets(3).Range(Cells(RECORD, B)).Value 'copies focus Course to destination
wb.Worksheets(TFOCUS).Range(Cells(RFOCUS, CFOCUS)).Value = Worksheets(3).Range(Cells(RECORD, C)).Value 'copies focus Date to destination
CFOCUS = CFOCUS + 1 'moves focus to next column
RECORD = RECORD + 1 'creates next record
'save [wb] workbook? Close it?
Set wb = Nothing
我是否还建议保留YELLCASE
常量,并将camelCase
用于本地人,PascalCase
用于其他所有内容?
答案 1 :(得分:0)
如果RFOCUS设置为" Q"而B和A是整数,那么:
FILEPATH.Worksheets(TFOCUS).Range(Cells(RFOCUS, B)).Value = Worksheets(3).Range(Cells(RECORD, A)).Value
应该是:
FILEPATH.Worksheets(TFOCUS).Range(RFOCUS & B).Value = Worksheets(3).Cells(Record, A).Value
以下是所有3行:
FILEPATH.Worksheets(TFOCUS).Range(RFOCUS & B).Value = Worksheets(3).Cells(Record, A).Value
FILEPATH.Worksheets(TFOCUS).Cells(4, CFOCUS).Value = Worksheets(3).Cells(Record, B).Value
FILEPATH.Worksheets(TFOCUS).Range(RFOCUS & CFOCUS).Value = Worksheets(3).Cells(Record, C).Value