简体   繁体   English

用于 A 列范围的 Excel vba 复制不同列的值

[英]Excel vba for a range on column A copy the values of the different columns

I'm creating a new access database to import the data I manage in excel for the test results of several users.However the excel file I have due to the layout and the number of columns and files, is really hard to prepare the file for import manually.我正在创建一个新的访问数据库,以导入我在 excel 中管理的数据,用于多个用户的测试结果。但是,由于布局以及列和文件的数量,我的 excel 文件很难准备文件手动导入。 This is why I have been looking for any vba code to make the work :)这就是为什么我一直在寻找任何 vba 代码来完成工作:)

The file have in the column A the test references and then in each column it have the name of one user and the respective scores for each test.该文件在 A 列中包含测试引用,然后在每一列中包含一个用户的名称和每个测试的相应分数。 I need to copy for each user's name and all the test references and scores in consequent blocks so just using 3 columns, one for user's name, then test refence and finally the test score.我需要复制每个用户的姓名以及随后块中的所有测试参考和分数,所以只使用 3 列,一列用于用户名,然后是测试参考,最后是测试分数。

I think it is not so clear (but cannot explain better, sorry), so let me upload 2 pics of examples of current sheet data and how data should look after run the code.我认为它不是那么清楚(但不能解释得更好,对不起),所以让我上传2张当前工作表数据示例的图片以及运行代码后数据应该如何处理。

Thanks so much in advance非常感谢提前

Current sheet data当前工作表数据

IMG1

How data should look after run the code运行代码后数据应该如何处理

IMG2

Alright, this should be able to loop through like you're looking for.好的,这应该能够像您正在寻找的那样循环。 It's a little bit rigid because I don't really know much about your book layout.这有点僵硬,因为我对你的书的布局不太了解。 Right now, it assumes that Sheets(1) is the source and Sheets(2) is your destination.现在,它假定Sheets(1)是源,而Sheets(2)是目标。

Sub test()

    Dim lastCol As Long
    lastCol = Sheets(1).Cells(2, Columns.count).End(xlToLeft).Column

    Dim col As Long
    For col = 2 To lastCol
        userLoop (col)
    Next col

End Sub

Sub userLoop(ByVal col As Long)

    Dim lastRow_sheet1 As Long
    lastRow_sheet1 = Sheets(1).Cells(Rows.count, "A").End(xlUp).row

    Dim lastRow_sheet2 As Long
    lastRow_sheet2 = Sheets(2).Cells(Rows.count, "A").End(xlUp).row + 1
    If lastRow_sheet2 = 1 Then lastRow_sheet2 = 2

    Dim i As Long
    For i = 2 To lastRow_sheet1
        With Sheets(2)
            .Cells(lastRow_sheet2, "A").Value2 = Sheets(1).Cells(1, col).Value2
            .Cells(lastRow_sheet2, "B").Value2 = Sheets(1).Cells(i, "A").Value2
            .Cells(lastRow_sheet2, "C").Value2 = Sheets(1).Cells(i, col).Value2
        End With

        lastRow_sheet2 = lastRow_sheet2 + 1
    Next i

End Sub

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

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