从模块调用用户窗体以显示弹出式图表和 return 到模块内的相同位置
Call a UserForm from module to show pop-up chart and return to same place within module
我有一个 VBA 程序运行查询以从 oracle 数据库中提取数据并将其放置在 Excel 工作表中,还创建一个用于上传的输出 .txt 文件进入其他一些软件。
我正在尝试在提取数据和创建输出文件之间放置一个中间步骤,让用户可以"double-check"确保进入输出文件的数据是正确的。我正在尝试创建一个显示弹出式图表的用户窗体,该图表允许用户 "accept" 或 "decline" 创建输出文件。下面是我的 VBA 代码的基本副本,其中调用提取数据的宏,然后创建输出文件。如何访问用户表单,然后 return 到模块中的相同位置以继续程序?
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
允许用户接受拒绝生成输出文件的用户窗体弹出图表示例如下:
我刚刚构建了这个小测试宏,它应该提供一个很好的 shell 供您合并到您的宏中。您将必须为所有 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 个命令按钮和以下代码的用户窗体:
Private Sub CommandButton1_Click()
bSwitch = True
Me.Hide
End Sub
Private Sub CommandButton2_Click()
bSwitch = False
Me.Hide
End Sub
我有一个 VBA 程序运行查询以从 oracle 数据库中提取数据并将其放置在 Excel 工作表中,还创建一个用于上传的输出 .txt 文件进入其他一些软件。
我正在尝试在提取数据和创建输出文件之间放置一个中间步骤,让用户可以"double-check"确保进入输出文件的数据是正确的。我正在尝试创建一个显示弹出式图表的用户窗体,该图表允许用户 "accept" 或 "decline" 创建输出文件。下面是我的 VBA 代码的基本副本,其中调用提取数据的宏,然后创建输出文件。如何访问用户表单,然后 return 到模块中的相同位置以继续程序?
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
允许用户接受拒绝生成输出文件的用户窗体弹出图表示例如下:
我刚刚构建了这个小测试宏,它应该提供一个很好的 shell 供您合并到您的宏中。您将必须为所有 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 个命令按钮和以下代码的用户窗体:
Private Sub CommandButton1_Click()
bSwitch = True
Me.Hide
End Sub
Private Sub CommandButton2_Click()
bSwitch = False
Me.Hide
End Sub