一直在尝试运行示例代码,但无法执行此操作。最可能的原因是我没有正确定义变量(表1& 2和Cl)。有谁可以帮我纠正它,所以我对如何在将来做这个有更好的想法?非常感谢。这是代码
Option Explicit
Sub ADDCLM()
On Error Resume Next
Dim Dept_Row As Long
Dim Dept_Clm As Long
Dim Table1 As Range
Dim Table2 As Range
Dim cl As Variant
Table1 = Sheet1.Range("A3:A13") ' Employee_ID Column from Employee table
Table2 = Sheet1.Range("H3:I13") ' Range of Employee Table 1
Dept_Row = Sheet1.Range("E3").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm = Sheet1.Range("E3").Column
For Each cl In Table1
Sheet1.Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
Dept_Row = Dept_Row + 1
Next cl
MsgBox "Done"
End Sub
答案 0 :(得分:2)
您不仅没有正确设置范围(正如KS Sheon已经指出的那样),但您也使用了无效引用:$images = array();
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$desired_dir="images";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"images/".$file_name);
}else{ //rename the file if another one exist
$new_dir="images/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$images[] = $file_name;
}else{
print_r($errors);
}
}
if(empty($error)){
$imglinks = implode(" | ", $images);
}
echo $imglinks;
}
应替换为Sheet1
此外,代码可以简化如下
Worksheets("Sheet1")
或者,如果您想仅在E3中保留值:E13
Option Explicit
Sub ADDCLM()
Worksheets("Sheet1").Range("E3:E13").FormulaR1C1 = "=Vlookup(RC1,R3C8:R13C9,2,False)"
MsgBox "Done"
End Sub
答案 1 :(得分:1)
table1和table2被声明为范围“对象”。要为“对象”赋值,必须使用set
,即
set Table1 = Sheet1.Range("A3:A13")`
set Table2 = Sheet1.Range("H3:I13")
您不需要为Dept_Row和Dept_clm使用set
,因为它们仅被分配了“值”。
cl
应声明为范围,因为您正在调用For Each cl In Table1
。
HTH。
答案 2 :(得分:0)
以下代码应该适合您。唯一需要的更改是将Table1和Table2更新为Variant类型,或者在Table1和Table2之前使用Set。这在以下Application.WorksheetFunction.VLookup表达式的语法中定义。
'Option 1 Declare Tables as vaiants
Option Explicit
Sub ADDCLM()
On Error Resume Next
Dim Dept_Row As Long
Dim Dept_Clm As Long
Dim Table1 As Variant
Dim Table2 As Variant
Dim cl As Variant
Table1 = Sheet1.Range("A3:A13") ' Employee_ID Column from Employee table
Table2 = Sheet1.Range("H3:I13") ' Range of Employee Table 1
Dept_Row = Sheet1.Range("E3").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm = Sheet1.Range("E3").Column
For Each cl In Table1
Sheet1.Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
Dept_Row = Dept_Row + 1
Next cl
MsgBox "Done"
End Sub
或者:'
'Option 2 Set Ranges using "Set"
Option Explicit
Sub ADDCLM()
On Error Resume Next
Dim Dept_Row As Long
Dim Dept_Clm As Long
Dim Table1 As Range
Dim Table2 As Range
Dim cl As Variant
Set Table1 = Sheet1.Range("A3:A13") ' Employee_ID Column from Employee table
Set Table2 = Sheet1.Range("H3:I13") ' Range of Employee Table 1
Dept_Row = Sheet1.Range("E3").Row ' Change E3 with the cell from where you need to start populating the Department
Dept_Clm = Sheet1.Range("E3").Column
For Each cl In Table1
Sheet1.Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
Dept_Row = Dept_Row + 1
Next cl
MsgBox "Done"
End Sub