Vlookup 到另一个工作簿

发布于 2024-09-04 20:36:41 字数 293 浏览 21 评论 0原文

这是我的困境。

我有两张工作表,一张包含客户姓名,另一张我想根据城市将姓名复制到其中。

例如:

与每列关联的是姓氏、名字和城市。 我有数百个与不同城市相关联的名字,我想要的是从worksheet1.xls将所有纽约客户复制到worksheet2.xls,无论是当我打开worksheet2时还是通过宏,哪个更容易,因为姓氏在一个单元格中并且第一个名字在另一个中,我必须复制两个名字。

我发现可以将单元格从一个工作表链接到另一个工作表,然后根据条件进行 vlookup。这是最好的最简单的方法还是还有其他方法?

Here is my dilemma.

I have two worksheets one that has the name of clients and one that i want to copy the names to depending on the city.

For instance:

associated to each column is last name, first name and city.
i have hundreds of names associated to different cities and what i would like is from worksheet1.xls to copy all the New York clients to worksheet2.xls either when i open worksheet2 or via macro what ever is easier and because last name is in one cell and the first name is in the other i would have to copy both.

I saw that its possible to link cells from one worksheet to another and then do a vlookup depending on the criteria. Is that the best easiest way or is there another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2024-09-11 20:36:41

执行此操作的一种方法是在工作表 2 中添加查询工作表 1 的远程数据引用。
然后,当您打开工作表 2 时,刷新数据以从工作表 1 获取最新数据,

例如

SELECT * FROM worksheet1.xls WHERE City=YourChoice

这不是语法正确的查询,请使用查询编辑器帮助您构建它

。如果您需要帮助,请包括您正在使用的 Excel 版本。

One way to do this is to add a Remote Data reference in worksheet2 that queries worksheet1.
Then when you open worksheet2 you refresh the data to get latest from worksheet1

Something like

SELECT * FROM worksheet1.xls WHERE City=YourChoice

This is not a syntactically correct query, use the query editor to help you build it

If you need help, please include the version of Excel your are using.

上课铃就是安魂曲 2024-09-11 20:36:41

我可以提供这个宏作为答案,它有以下改进空间:

  • 停用闪烁application.ScreenUpdating
  • LastName和FirstName作为数组,以添加更多数据并避免许多窗口更改
  • 使用打开关闭直接打开Excel,而不是事先打开。

这是代码

Option Explicit

Sub Macro1()
'
' Macro1 Macro
'
Dim City As String
Dim FirstName As String
Dim LastName As String

City = Range("B4").Value 'B4 cell where city is
Range("C4").Select 'C4 cell where name is pasted

'go to the book, shhet and firs range with names
Windows("book_with_cities.xlsx").Activate
Sheets("year2011").Select
Range("A4").Select 'where  the data start
'main copy loop
Do While ActiveCell.Value <> ""
    'if is the city I´m lookin for copy the data to my excel of results
    If ActiveCell.Value = City Then
        LastName = ActiveCell.Offset(0, 1).Value
        FirstName = ActiveCell.Offset(0, 2).Value
        Windows("book_with_results.xlsx").Activate
        Sheets("Your_city_data").Select
        ActiveCell.Value = LastName
        ActiveCell.Offset(0, 1).Value = FirstName
        ActiveCell.Offset(1, 0).Select
        Windows("book_with_cities.xlsx").Activate
        Sheets("year2011").Select
    End If
    ActiveCell.Offset(1, 0).Select
Loop
'going back to your sheet
Windows("book_with_results.xlsx").Activate
Sheets("Your_city_data").Select

End Sub

希望这有帮助

最好的问候

艾伦

I can offser this macro as answer, it has the following room for improvement:

  • deactivate flicker application.ScreenUpdating
  • LastName and FirstName as array, to adding more data and avoid many windows changes
  • Using open and close to open the excel directly, instead of having previously open.

this is the code

Option Explicit

Sub Macro1()
'
' Macro1 Macro
'
Dim City As String
Dim FirstName As String
Dim LastName As String

City = Range("B4").Value 'B4 cell where city is
Range("C4").Select 'C4 cell where name is pasted

'go to the book, shhet and firs range with names
Windows("book_with_cities.xlsx").Activate
Sheets("year2011").Select
Range("A4").Select 'where  the data start
'main copy loop
Do While ActiveCell.Value <> ""
    'if is the city I´m lookin for copy the data to my excel of results
    If ActiveCell.Value = City Then
        LastName = ActiveCell.Offset(0, 1).Value
        FirstName = ActiveCell.Offset(0, 2).Value
        Windows("book_with_results.xlsx").Activate
        Sheets("Your_city_data").Select
        ActiveCell.Value = LastName
        ActiveCell.Offset(0, 1).Value = FirstName
        ActiveCell.Offset(1, 0).Select
        Windows("book_with_cities.xlsx").Activate
        Sheets("year2011").Select
    End If
    ActiveCell.Offset(1, 0).Select
Loop
'going back to your sheet
Windows("book_with_results.xlsx").Activate
Sheets("Your_city_data").Select

End Sub

Hope this helps

Best Regards

Alen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文