another Excel 2013/VBA question...
I'm trying to set up a userform in VBA which pulls data from a specified worksheet. I've got the form set up and I am extracting the data from the cells withou any issues barr one. The form is pulling the data from whichever sheet is active in the background. I can't workout were i'm going wrong but I am very knew to Excel/VBA.
My first form ( frmMenu
) is a simple radio button group which I use to select the desired zone and set zoneChoice String to the correct value. the main ( frmNSSMain
) form is then loaded adn with the code i have put below, i am expecting it to pull data of of either of the two sheets in the workbook at present, "Central" and "SE".
Unfortunately, the data is pulled off of whichever is the active sheet at run time, and not the one seletced by the user. im guessing the problem is from the activate function, Set sh = line
. Any help appreciated.
Code from frmMenu
Public zoneChoice As String
Private Sub btnLoadMain_Click()
If Me.optCentral Then
zoneChoice = "Central"
frmNSSMain.Show
ElseIf Me.optSE Then
zoneChoice = "SE"
frmNSSMain.Show
'ElseIf Me.optSNI Then
'frmNSSMain.zoneChoice = "SNI"
'frmNSSMain.Show
'ElseIf Me.optSW Then
'frmNSSMain.zoneChoice = "SW"
'frmNSSMain.Show
Else:
msg = MsgBox("You must choose a Zone from the right hand side before continuing!", vbCritical)
End If
End Sub
Code from frmNSSMain
Public Sub UserForm_Activate()
' Initialize worksheet variables
Set sh = ThisWorkbook.Sheets(frmMenu.zoneChoice)
x = Range("A" & Rows.Count).End(xlUp).Row
y = Application.WorksheetFunction.CountIf(Range("R2:R" & x), "")
'K holds row count based on data in Column A
k = sh.Range("A2", sh.Range("A2").End(xlDown)).Rows.Count
'Disable discard, submit and no contact buttons until data is loaded
btnSubmit.Enabled = False
btnDiscard.Enabled = False
btnNoContact.Enabled = False
txtQ1Score.Enabled = False
txtQ2Score.Enabled = False
txtContactNumber.MaxLength = 11
lblOne.Caption = frmMenu.zoneChoice
End Sub
Private Sub btnRetrieve_Click()
' Check Data for NSS surveys is available, if so retrieve data, else show error message
If y > 0 Then
'Select random row
aRandNum = Int((k - 2) * Rnd + 2)
'Set global variable aRow to held selected row
aRow = Replace(Str(aRandNum), " ", "")
'Get Area
areaName = Replace("D" & Str(aRandNum), " ", "")
txtArea.Text = Range(areaName).Value
'Get Establishment
establishmentName = Replace("E" & Str(aRandNum), " ", "")
txtEstablishment.Text = Range(establishmentName).Value
'Get End User Name
endUserName = Replace("F" & Str(aRandNum), " ", "")
txtName.Text = Range(endUserName).Value
'Get End User Contact Number
endUserTelNum = Replace("G" & Str(aRandNum), " ", "")
txtContactNumber.Text = Replace(Range(endUserTelNum).Value, " ", "")
txtContactNumber.Enabled = True
txtContactNumber.Locked = False
'Get Job Description
jobDescription = Replace("I" & Str(aRandNum), " ", "")
txtJobDescription.Text = Range(jobDescription).Value
'Get contractor
contractorName = Replace("H" & Str(aRandNum), " ", "")
txtContractor.Text = Range(contractorName).Value
'Get job number
jobNumber = Replace("C" & Str(aRandNum), " ", "")
txtJobNumber.Text = Range(jobNumber).Value
' reported by, contact number, contractor, job description, comments, raised by, survey by,
' Disable get NSS data button after retrieval and activate other discard, submit and no contact
btnSubmit.Enabled = True
btnDiscard.Enabled = True
btnNoContact.Enabled = True
btnRetrieve.Enabled = False
txtQ1Score.Enabled = True
txtQ2Score.Enabled = True
ElseIf y < 1 Then
Dim result As String
result = MsgBox("No NSS data available. Please inform Management Team.", vbCritical)
End If
End Sub
Question resolved. Stupid simple mistake like most of the annoying ones are...
sh.Activate
missing from UserForm_Activate
Doh...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.