从模块调用用户窗体来显示popup图并返回到模块中的同一位置

我有一个VBA程序运行查询从Oracle数据库中提取数据,并将其放置在Excel工作表中,并创build一个用于上传到其他软件的输出.txt文件。

我试图在提取数据和创build输出文件之间放置一个中间步骤,允许用户“仔细检查”输出文件的数据是否正确。 我试图创build一个用户窗体,显示一个popup式图表,允许用户“接受”或“拒绝”创build一个输出文件。 下面是我的VBA代码的基本副本,其中macros调用数据被调用,然后创build输出文件。 如何访问用户表单,然后返回到模块中的相同位置以继续该程序?

Public Sub OutputSurveyFile() 'Call appropriate macro to run the query to get data needed to be exported to file Call qry_DirSurveyRpt 'Set worksheet to the sheet activated by calling query macro Set wsData = ActiveSheet 'Determine last row and column of data With wsData LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column End With 'Store worksheet data to range Set rngData = wsData.Range(Cells(1, 1), Cells(LastRow, LastCol)) 'Store range of data to array vData = rngData 'Output desired data to text file and format accordingly For i = LBound(vData, 1) To UBound(vData, 1) If i <> 1 Then 'Unique well name call If vData(i, 1) <> vData(i - 1, 1) Then '********GO TO USERFORM CREATE POP-UP CHART TO SEE IF USER ACCEPTS OR DECLINES DATA**** '****IF ACCEPT, CONTINUE WITH CREATING OUTPUT FILE BELOW '****IF DECLINE, MOVE TO NEXT i AND CONTINUE LOOPING THROUGH DATA 'Directoy path for output file to go myFile = sPathFileOutput & myFileName & ".txt" fnum = FreeFile() Open myFile For Output As fnum 'Formatting header for new well survey Print #fnum, "# FILE HEADER" End If 'Survey data call If vData(i, 1) = vData(i - 1, 1) Then Print #fnum, vTab & vData(i, 6) & vTab & vData(i, 7) & vTab & vData(i, 8) End If 'Close output text file from editing if next row is a different well If i + 1 < UBound(vData, 1) Then If vData(i, 1) <> vData(i + 1, 1) Then Close #fnum End If End If 'Close output test file if end of data range is met If i = UBound(vData, 1) Then Close #fnum End If End If Next i End Sub 

UserFormpopup式图表允许用户接受拒绝制作输出文件的示例如下所示: 弹出式图表

我只是build立了这个小testingmacros,它应该提供一个很好的shell,让你融入你的macros。 你将不得不为所有的If块做出正确的代码,而不是。

独立模块中的以下代码:

 Public bSwitch As Boolean Sub Tester() MsgBox "Hi" 'this is all your code above the point where you stated you want to call the user form 'call the userform UserForm1.Show 'user will accept or decline if they If bSwitch Then 'if accept then keep moving with rest of code MsgBox "keep moving" Else 'if decline go to next i in loop MsgBox "end" End If End Sub 

我用2个命令button和下面的代码创build了一个用户窗体:

 Private Sub CommandButton1_Click() bSwitch = True Me.Hide End Sub Private Sub CommandButton2_Click() bSwitch = False Me.Hide End Sub