从另一个工作表中的单元格引用中查找列中的数据,然后将一些数据复制到另一个工作表

时间:2017-04-21 17:01:01

标签: excel vba excel-vba

我在工作簿中有3个工作表,第一个是信息表,其中单元格S1具有我需要在名为Classes的第二个工作表中找到的培训师名称。一旦在Classes表(H列)中找到了Trainer Name,我就需要将该Trainer名称放在Output表中(下一个空白行,A列)。然后我还需要从类(列A),Grad日期(列P)和列X到AB中的更多数据获取类名。我似乎无法正确编码这个代码,因为我运行的代码,但它不会将数据输入到输出表。到目前为止我只测试了2个字段。

Sub GetClassData()
Dim cls As Worksheet
Dim shOUT As Worksheet
Set cls = Worksheets("Classes")
Set shOUT = Worksheets("Output")
Dim trName As Range
Set trName = Worksheets("Info").Range("S1")

cls.Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For x = 2 To FinalRow
    ' Decide if to copy based on column H
    thisvalue = Cells(x, 8).Value
    If thisvalue = trName.Value Then
        irow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        With shOUT
            .Cells(irow, 1).Value = trName.Value
            .Cells(irow, 2).Value = trName.Offset(, -7).Value
        End With
    End If
Next x
End Sub

1 个答案:

答案 0 :(得分:1)

尝试下面的代码(解释在代码注释中):

// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, title, image, description, tekst, address, website, telephone, email, openinghours FROM ExperienceUtrecht";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo "<table border='1'> <tr> <th>ID</th> <th>Title</th> <th>Image</th> <th>Description</th> <th>Tekst</th> <th>Address</th> <th>Website</th> <th>Telephone</th> <th>Email</th> <th>Opening Hours</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['image'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['tekst'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['website'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['openinghours'] . "</td>"; echo "</tr>"; } echo "</table>";
}
} else {
echo "0 results";
}

mysqli_close($conn);
?>