简体   繁体   English

Excel-从SQL(VBA)刷新和验证数据

[英]Excel - Refresh and Validate Data From SQL (VBA)

I'm using office 2013 and SQLServer 2008 R2. 我正在使用Office 2013和SQLServer 2008 R2。 I have an Excel VBA proc that inserts a list of commissions for sales into a database. 我有一个Excel VBA proc,可将销售佣金列表插入数据库。 One of the columns is SalesRepID, which clearly has to be valid. 列之一是SalesRepID,显然必须有效。 When it was written, we had a fairly static sales team, but we've since acquired two other companies who do less specialized work, so there's a quite a bit of change in the sales force now. 在撰写本文时,我们有一个相当静态的销售团队,但此后我们又收购了另外两家从事专业性工作较少的公司,因此现在的销售团队有很大的变化。

The current methods has a list of sales repID's and names in sorted named range, which is then used as validation criteria: the user is given a list of sales reps in drop down list in Excel and when they select one, it uses VLookup to insert the SalesRepID in a hidden cell. 当前方法在已排序的命名范围中具有销售repID和名称的列表,然后用作验证条件:在Excel的下拉列表中为用户提供了销售代表的列表,当他们选择一个时,将使用VLookup插入隐藏单元格中的SalesRepID。 This is then used in the insert operation. 然后将其用于插入操作。

What I want to do now is give the users a refresh button (and ideally run the refresh on open). 我现在想做的是给用户一个刷新按钮(理想情况下在打开时运行刷新)。 Some of the billers work at home at the weekend, so this needs to be able to work offline (obviously, if a new rep is added and they haven't refreshed they won't see the new rep). 一些开票人在周末在家工作,因此这需要脱机工作(很明显,如果添加了新的销售代表并且他们没有刷新,他们将看不到新的销售代表)。

I don't want to have to define anything like ODBC sources on a user's physical machine 我不想在用户的物理计算机上定义诸如ODBC源之类的任何内容

I'm trying to figure out the way to solve this. 我正在尝试找出解决此问题的方法。 One way is to add a refresh button that will execute a query, insert the current list of sales reps and update the range (or use a dynamic range) to be the current no of records returned. 一种方法是添加一个刷新按钮,该按钮将执行查询,插入当前销售代表列表并将范围更新(或使用动态范围),使其成为当前返回的记录数。 (I'm assuming I can update a named range's address, or that a dynamic range will update if the data is inserted from a SQL query) (我假设我可以更新命名范围的地址,或者如果从SQL查询插入数据,则动态范围将更新)

For example: 例如:

Initial Range 初始范围

1 Tom
2 Dick
3 Harry

SalesReps range = A1:B3 SalesReps范围= A1:B3

User Hits Refresh: Deletes old ranges contents. 用户点击刷新:删除旧范围内容。 insert new list 插入新清单

New Range 新范围

1 Tom
2 Dick
3 Harry
4 Fred

SalesReps range = A1:B4 SalesReps范围= A1:B4

Can anyone think of a better way to this? 谁能想到一种更好的方法?

Regards 问候

Mark 标记

Try this for your dynamic range 尝试此动态范围

Dim LastRow as integer
    LastRow = Sheets("yoursheet").Cells(Rows.Count, "A").End(xlUp).Row

    Range("A1:B" & LastRow).name = "SalesReps"

The code works in a slightly unintuitive way. 该代码以一种不太直观的方式工作。 Cells(Rows.Count, "A") finds the very last cell on the sheet in column A (row 1048576 or something). Cells(Rows.Count, "A")在工作表A列中找到最后一个单元格(行1048576或其他内容)。

End(xlUp).Row is the equivalent of pressing ctrl+up from that cell and returning the row number of the first cell containing information. End(xlUp).Row等效于从该单元格按ctrl + up并返回包含信息的第一个单元格的行号。

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

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