在工作表中select最高N%的数据

我正在从一个用户表单中提取数据从sql server中的表中,我提取顶级80%的数据,并把它放在一个VBA代码的Excel文件。 我想要做的是从这80%的数据是存储在excel工作表我想要允许用户从excel工作表中select一个百分比的数据,只需input文本区域中的数字是可以做到这一点?

这是我用来从数据库从SQL Server中提取的代码:

Private Sub CommandButton1_Click() Dim sSql As String Dim rs As ADODB.Recordset Dim cn As ADODB.Connection Dim cmdObj As ADODB.Command Dim qf As Object Sheets("sheet1").Select Selection.ClearContents Set cn = New ADODB.Connection cn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=swat_admin;PASSWORD=swat_admin;Initial Catalog=SWAT_V2_PLL;Data Source=NCEFORREP" sSql = "select TOP 80 PERCENT func.coresitecode,func.SiteCode,feature_id, TOTAL_PNRs,PNR_rank_id from rpt.functional_site_mapping func inner join (select TOP 80 PERCENT * from rpt.top_PNR_contributing_sites order by TOTAL_PNRs DESC) PNR on func.ptf_id = pnr.ptf_id and func.coresitecode = pnr.coresitecode and func.sitecode = pnr.sitecode order by PNR_rank_id " Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient rs.Open sSql, cn, adOpenForwardOnly, adLockReadOnly, adCmdText If rs.EOF Then MsgBox (" the record set is empty. rs.EOF = " & rs.EOF) Else MsgBox ("The number of rows returned from the query is: " & rs.RecordCount) Range("a1").Select For Each qf In rs.Fields Range("a1").Offset(0, coloffset).Value = qf.Name coloffset = coloffset + 1 Next qf Range("a2").CopyFromRecordset rs rs.Close Set rs = Nothing End If End sub 

这里是我的Excel文件,如果有人可以帮助我,谢谢你:

http://www.cjoint.com/c/FDzjyTNB1Lv

确保你得到一个vbvariables的百分比,例如myPercent ,*然后检查它是一个0到100之间的数字*然后在你现有的查询中使用它

所以

 sSql = "select TOP 80 PERCENT 

 sSql = "select TOP " & myPercent & "PERCENT 

希望能帮助到你