I am attempting to create a user form that will propagate the first available row of the excel sheet with information. I am struggling to get the range correct, and the form is currently showing 'Subscript out of range' ; 'Runtime error 9'. There also seems to be a problem with the 'if' statement, but following the standard advice online doesn't seem to have helped solve the problem.
If anyone has any idea where I'm going wrong (I'm very new to this) that would be great.
Private Sub UserForm_Initialize()
BusinessAreaBox.List = Array("option one", "option two")
End Sub
Private Sub CommandButton1_Click()
Dim RowCount As Long
RowCount = Sheets("Sheet1").Range.Sheets("Sheet1").Cells(2, "A")
With ThisWorkbook.Sheets("Sheet1").Range("A2")
.Offset(RowCount, 0).Value = BusinessArea1.Value
.Offset(RowCount, 1).Value = BusinessContact1.Value
.Offset(RowCount, 2).Value = LPSContact1.Value
.Offset(RowCount, 4).Value = ProjectedFTE1.Value
.Offset(RowCount, 5).Value = DateOfMostRecentMeeting1.Value
.Offset(RowCount, 6).Value = FTEComment1.Value
.Offset(RowCount, 7).Value = ProposedMove1.Value
.Offset(RowCount, 8).Value = DeskUtilisation1.Value
.Offset(RowCount, 9).Value = OtherComment1.Value
.Offset(RowCount, 10).Value = Actions1.Value
If RegularMeeting1.Value = True Then
.Offset(RowCount, 3).Value = "Yes"
Else
.Offset(RowCount, 3).Value = "No"
End If
RegularMeeting1.Value = True Or False
End With
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
I guess you may be after this
Private Sub CommandButton1_Click()
With ThisWorkbook.Sheets("Sheet1")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 11).Value = Array(BusinessArea1.Value, _
BusinessContact1.Value, _
LPSContact1.Value, _
IIf(RegularMeeting1.Value, "Yes", "No"), _
ProjectedFTE1.Value, _
DateOfMostRecentMeeting1.Value, _
FTEComment1.Value, _
ProposedMove1.Value, _
DeskUtilisation1.Value, _
OtherComment1.Value, _
Actions1.Value)
End With
RegularMeeting1.Value = True Or False '<--| what is this supposed to do?
End Sub
Try writing to the cells this way. You need to active the sheet and cell first.
Dim myArr As Variant
myArr = Array(BusinessArea1.Value, _
LPSContact1.Value, _
ProjectedFTE1.Value, _
DateOfMostRecentMeeting1.Value, _
FTEComment1.Value, _
ProposedMove1.Value, _
DeskUtilisation1.Value, _
OtherComment1.Value, _
Actions1.Value)
sheet1.activate
sheet1.range("A2").activate
for I = 0 to ubound(myArr)
activecell.value = myArr(I)
activecell.Offset(1,0).activate
Next I
Related
I am trying to copy several data from a table on a different sheet, to a new table on another sheet. My structure is like:
These are the steps that me and Mr. #QHarr have tried:
Checked the objects and values exist
Tried running the codes line by line
Activate sheets and re-arranging the codes
None worked so far:
Here is my current codes:
Private Sub cmdedit_Click()
If MsgBox("Transfer selected asset to " & Me.ComboBox1.Text & "?", vbYesNo, "CONFIRMATION") = vbYes Then
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim wsendRow As Range
Dim wsendRow1 As Range
Dim lo As ListObject
Dim lr As ListRow
Set ws = Sheets("FIELD OFFICE DATABASE")
Set ws1 = Sheets("Transferred Items")
Set lo = ws1.ListObjects("table3")
Set lr = lo.ListRows.Add
Set wsendRow = ws.Range("B" & Rows.Count).End(xlUp)
'Set wsendRow1 = ws1.Range("A" & Rows.Count).End(xlUp)
ws.Activate
Range("B2").Select
Do Until ActiveCell.Address = wsendRow.Address
If ActiveCell.Value = Me.cmbemn.Text Then
'ws1.Unprotect "321321"
'ws1.Activate
lr.Range(1, 1).Value = Me.cmbemn.Text 'error appears on this line. if I place a comment here, the error will just move on the next line.
lr.Range(1, 2).Value = Me.TextBox1.Text
lr.Range(1, 3).Value = Me.txttype.Text
lr.Range(1, 4).Value = Me.txtmodel.Text
lr.Range(1, 5).Value = ActiveCell.Offset(0, 4).Value
lr.Range(1, 6).Value = ActiveCell.Offset(0, 5).Value
lr.Range(1, 7).Value = Me.txtpurdate.Text
lr.Range(1, 8).Value = Me.txtprice.Text
lr.Range(1, 9).Value = Me.txtcon.Text
lr.Range(1, 10).Value = ActiveCell.Offset(0, 9).Value
lr.Range(1, 11).Value = ActiveCell.Offset(0, 11).Value
lr.Range(1, 12).Value = Me.ComboBox1.Text
lr.Range(1, 13).Value = ActiveCell.Offset(0, 13).Value
lr.Range(1, 14).Value = Date
lr.Range(1, 15).Value = ws.Range("A13").Value
lr.Range(1, 16).Value = Me.TextBox2.Text
Exit Do
Exit Sub
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
End If
End Sub
Codes never worked. Also to note, this question is in reference to my other question, (which took a lot of comments from me and Mr. QHarr) until he suggested that I should ask another question instead.
I hope someone can help me figure this out.
Thank you so much in advance
I wanted to have a code wherein there will be an error or prompt in the userform if the cell in the sheet- wherein the data will be transferred, already has content. As of now, the code I'm using doesn't show any prompt but it succeeds in not updating the cell if a data is to be transferred to it again.
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub ClearButton_Click()
Call UserForm_Initialize
End Sub
Private Sub OKButton_Click()
Dim emptyRow As Long
'Make Sheet1 active
Sheet1.Activate
'Determine emptyRow
Dim rFound As Range: Set rFound = Range("B:B").Find(BarcodeTextBox.Value, , , xlWhole)
If rFound Is Nothing Then
emptyRow = Range("B" & Rows.Count).End(xlUp).Row + 1
Else
emptyRow = rFound.Row
End If
'Transfer information
If TimeOptionButton1.Value = True Then
Cells(emptyRow, 5).Value = "Yes"
End If
If TimeOptionButton2.Value = True Then
Cells(emptyRow, 7).Value = "Yes"
End If
If BreakOptionButton1.Value = True Then
Cells(emptyRow, 9).Value = "Yes"
End If
If BreakOptionButton2.Value = True Then
Cells(emptyRow, 11).Value = "Yes"
End If
If BreakOptionButton3.Value = True Then
Cells(emptyRow, 14).Value = "Yes"
End If
If BreakOptionButton4.Value = True Then
Cells(emptyRow, 16).Value = "Yes"
End If
Cells(emptyRow, 2).Value = BarcodeTextBox.Value
Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub
Private Sub UserForm_Initialize()
'Set Time In as default
TimeOptionButton1.Value = True
'Empty BarcodeTextBox
BarcodeTextBox.Value = ""
Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub
Thank you in advance!
Concerning your code, I think that you may add something like this:
If TimeOptionButton1.Value = True Then
if len(cells(emptyRow,5)) = 0 then
MsgBox "Write Error Message here"
else
Cells(emptyRow, 5).Value = "Yes"
end if
End If
For each yes. You may consider building a separate function/sub later to avoid repetition.
I apologise if this has been answered in a previous thread, I search for a while but was really unsure of what to call the problem I am having.
I am very new to VBA, this is my first real foray into it.
I am having an issue with VBA pasting the contents of a form into a spreadsheet to the spreadsheet having a vlookup reference column in column one (the sheet requires to have a excel lookup of the form so this reference is required)
I am currently using a button with:-
Private Sub submit_Click()
Dim RowCount As Long
RowCount = Worksheets("Raw Data").Range("B1").CurrentRegion.Rows.Count
Private Sub submit_Click()
With Worksheets("Raw Data").Range("B1")
.Offset(RowCount, 0).Value = Format(Now, "dd/mm/yyyy")
.Offset(RowCount, 1).Value = Me.operator.Value
.Offset(RowCount, 2).Value = Me.custexp.Value
.Offset(RowCount, 3).Value = Me.fcr.Value
.Offset(RowCount, 4).Value = Me.opfcr.Value
.Offset(RowCount, 5).Value = Me.compliant.Value
.Offset(RowCount, 6).Value = Me.summary.Value
.Offset(RowCount, 7).Value = Me.evaluation.Value
.Offset(RowCount, 8).Value = Me.compliance.Value
.Offset(RowCount, 9).Value = Me.callid.Value
.Offset(RowCount, 10).Value = Me.calltime.Value
.Offset(RowCount, 11).Value = Me.leader.Value
.Offset(RowCount, 12).Value = Me.custnum.Value
End With
Dim ctl As Control
End Sub
But this finds the first completely blank cell and avoids anything with formula in it.
Is there anyway to get around this?
Maybe something like this:
Private Sub submit_Click()
Dim c As Range
'find first empty cell in ColB (looking from the bottom of the sheet)
Set c = Worksheets("Raw Data").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
With c.EntireRow
.Cells(2).Value = Format(Now, "dd/mm/yyyy")
.Cells(3).Value = Me.Operator.Value
.Cells(4).Value = Me.custexp.Value
.Cells(5).Value = Me.fcr.Value
.Cells(6).Value = Me.opfcr.Value
.Cells(7).Value = Me.compliant.Value
.Cells(8).Value = Me.Summary.Value
.Cells(9).Value = Me.evaluation.Value
.Cells(10).Value = Me.compliance.Value
.Cells(11).Value = Me.callid.Value
.Cells(12).Value = Me.calltime.Value
.Cells(13).Value = Me.leader.Value
.Cells(14).Value = Me.custnum.Value
End With
End Sub
I am relatively new to the whole VBA so any help would be greatly appreciated...
I am having issues with getting my Combobox on my form (once you hit the submit button) to input the information to my excel spreadsheet in a specific column which would move down a row each time a new record is entered.
Please see my code below and If you need any further information please ask away :) Many Thanks in advance Paula
Option Explicit
Private Sub cmdAdd_Click()
Dim irow As Long
Dim EorP As String
Dim ComboStaus As ComboBox
Dim ws As Worksheet
Set ws = Worksheets("BS Personal Data")
'find first empty row in spreadsheet
irow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
EorP = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Combo = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the spreadsheet
With ws
.Cells(irow, 1).Value = Me.txtmanager.Value
.Cells(irow, 2).Value = Me.txtdivision.Value
.Cells(irow, 3).Value = Me.txtlocation.Value
.Cells(irow, 4).Value = Me.txtsystemname.Value
.Cells(irow, 9).Value = Me.Txtpurpose.Value
.Cells(irow, 10).Value = Me.txtaccess.Value
.Cells(irow, 11).Value = Me.txtdatecompleted.Value
End With
'clear the data
Me.txtmanager.Value = ""
Me.txtdivision.Value = ""
Me.txtlocation.Value = ""
Me.txtsystemname.Value = ""
Me.Txtpurpose.Value = ""
Me.txtaccess.Value = ""
Me.txtdatecompleted.Value = ""
'set option button to input data based on type of information to column 5
With ws
If OptElectronic Then
.Cells(EorP, 5).Value = "Electronic"
Else
.Cells(EorP, 5).Value = "PaperBased"
End If
End With
'set option button to input data based on personal data into column 7
With ws
If PersonalYes Then
.Cells(EorP, 7).Value = "Yes"
Else
.Cells(EorP, 7).Value = "No"
End If
End With
'set option button to input data based on privacy notes into column 8
With ws
If PrivacyYes Then
.Cells(EorP, 8).Value = "Yes"
Else
.Cells(EorP, 8).Value = "No"
End If
End With
End Sub
'combo button setup
Private Sub UserForm_Activate()
ComboStatus.Clear
With ComboStatus 'this loads the combo
.AddItem ""
.AddItem "Live"
.AddItem "Archived"
.AddItem "zzz"
End With
End Sub
'close button on the form
Private Sub cmdClose_Click()
Unload Me
End Sub
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Hi I have a VB linking to my Excel sheet done by the previous IT guy. Now its showing error "End If without Block If". Please help me debug. Codes below. Thank you all.
Private EditingRow As String
Private gCurrentStatus As String
Private gLocation As String
Private gRack As String
Private Sub cboType_Change()
cboSerialNo.Clear
Application.ScreenUpdating = False
Sheets("SGS Cylinder List").Select
ActiveSheet.Unprotect
ActiveSheet.UsedRange.Select
Selection.Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Set rng = Columns("D")
txtTotalSelectedType.Value = WorksheetFunction.countIF(rng, cboType.Value)
''' List all the selected Serial No
firstRowfound = False
firstrow = 0
lastRow = 0
Range("D1").Select
Do Until ActiveCell.Value = ""
If (firstRowfound = False And Cells(ActiveCell.Row, 4).Value = cboType.Value) Then
'MsgBox "1st row =" & ActiveCell.Row
firstrow = ActiveCell.Row
firstRowfound = True
End If
If (firstRowfound) Then
cboSerialNo.AddItem Trim(Cells(ActiveCell.Row, 3).Value) & " ," & Trim(Cells(ActiveCell.Row, 11))
End If
If (firstRowfound And Cells(ActiveCell.Row + 1, 4).Value <> cboType.Value) Then
'MsgBox "last row =" & ActiveCell.Row
lastRow = ActiveCell.Row
lastRowFound = True
Exit Do
End If
ActiveCell.Offset(1, 0).Select
Loop
If (firstrow > 0) Then
Set rngSelectedStatus = Range("I" & firstrow & ":I" & lastRow)
txtTotalCylinderAvailable.Value = WorksheetFunction.countIF(rngSelectedStatus, "Available")
Else
txtTotalCylinderAvailable.Value = 0
End If
ActiveSheet.Protect
Application.ScreenUpdating = True
End Sub
Private Sub cmdAdvancedAnalysis_Click()
If cboType.Value = "" Then
Exit Sub
End If
Worksheets("SGS Cylinder List").Select
ActiveSheet.Unprotect
newAddr = Sheets("SGS Cylinder List").[A2].CurrentRegion.Address(ReferenceStyle:=xlR1C1)
Sheets("Advanced").PivotTableWizard SourceType:=xlDatabase, SourceData:="SGS Cylinder List!" & newAddr
Sheets("Advanced").PivotTables("PivotTable1").RefreshTable
' Filter the PivotTable with the new Cylinder Type
Sheets("Advanced").PivotTables("PivotTable1").PageFields("Type").CurrentPage = cboType.Value
End Sub
Private Sub cmdCreateCylinder_Click()
Dim form1 As frmCylinder
Set form1 = New frmCylinder
form1.Show
End Sub
Private Sub cmdDisposalDate_Click()
Dim form1 As frmSelectDate
Set form1 = New frmSelectDate
form1.Show
Me.txtRsltDisposalDate = form1.SelectedDate
End Sub
Private Sub cmdLastUpdate_Click()
Dim form1 As frmSelectDate
Set form1 = New frmSelectDate
form1.Show
Me.txtLastUpdate.Value = form1.SelectedDate
End Sub
Private Sub cmdSearch_Click()
Sheets("SGS Cylinder List").Select
Range("C2").Select
Debug.Print cboSerialNo.Value
If cboSerialNo.Value = "" Then
Exit Sub
End If
cmdUpdate.Enabled = True
Do Until ActiveCell.Value = ""
' Found the row contains this given Serial No
''' to check the cboSerialNo first
serialNo = Left(cboSerialNo.Value, InStr(cboSerialNo.Value, ",") - 2)
If CStr(ActiveCell.Value) = serialNo Then
EditingRow = ActiveCell.Row
ActiveCell.EntireRow.Select
''' Show Selection
' Populate Location List
cboLocation.Clear
Sheets("Location").Select
Range("A2").Select
Do Until ActiveCell.Value = ""
cboLocation.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
If CStr(ActiveCell.Value) = serialNo Then
EditingRow = ActiveCell.Row
ActiveCell.EntireRow.Select
cboRack.Clear
Sheets("Location").Select
Range("B2").Select
Do Until ActiveCell.Value = ""
cboRack.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop
End If
Sheets("SGS Cylinder List").Select
If (Cells(ActiveCell.Row, 1).Value <> "") Then
cboLocation.Value = Cells(ActiveCell.Row, 1).Value
End If
If (Cells(ActiveCell.Row, 1).Value <> "") Then
cboRack.Value = Cells(ActiveCell.Row, 1).Value
End If
txtRsltClientName.Value = Cells(ActiveCell.Row, 5).Value
txtRsltWell.Value = Cells(ActiveCell.Row, 6).Value
txtRsltJobID.Value = Cells(ActiveCell.Row, 7).Value
''' Populate Sample Type List
cboRsltSampleType.Clear
Set sampleTypeList = Range("SampleTypes")
For Each cell In sampleTypeList
cboRsltSampleType.AddItem cell.Value
Next
If (Cells(ActiveCell.Row, 8).Value <> "") Then
cboRsltSampleType.Value = Cells(ActiveCell.Row, 8).Value
End If
txtRsltDisposalDate.Value = Cells(ActiveCell.Row, 9).Value
' Set Existing Cylinder Status
cboRsltCylinderStatus.Clear
Set statusList = Range("StatusTypes")
For Each cell In statusList
cboRsltCylinderStatus.AddItem cell.Value
Next
If (Cells(ActiveCell.Row, 10).Value <> "") Then
cboRsltCylinderStatus.Value = Cells(ActiveCell.Row, 10).Value
End If
''' Save Current Row of Cylinder Data to Global variables
gLocation = Cells(ActiveCell.Row, 1).Value
gRack = Cells(ActiveCell.Row, 2).Value
gClientName = Cells(ActiveCell.Row, 5).Value
gWell = Cells(ActiveCell.Row, 6).Value
gJobID = Cells(ActiveCell.Row, 7).Value
gSampleType = Cells(ActiveCell.Row, 8).Value
gCurrentStatus = Cells(ActiveCell.Row, 10).Value
Exit Sub
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Private Sub cmdUpdate_Click()
''' 1. Save the current setting to a History Sheet if found changes made
''' 2. Update the current row
'' if any property of the Cylinder change
If ((gCurrentStatus <> cboRsltCylinderStatus.Value _
Or gLocation <> cboLocation.Value _
Or gWell <> txtRsltWell.Value _
Or gJobID <> txtRsltJobID.Value _
Or gSampleType <> cboRsltSampleType.Value) _
And EditingRow <> "") Then
Range("LastUpdateDate").Value = Date
Sheets("SGS Cylinder List").Select
ActiveSheet.Unprotect
' Copy that edited range
Range("A" & EditingRow & ":I" & EditingRow).Select
Selection.Copy
'' Check if Cylinder Status change
If gCurrentStatus <> cboRsltCylinderStatus.Value Then
Sheets("History List").Select
Range("A" & ActiveSheet.Rows.Count).End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
' Add a Current Status
Cells(ActiveCell.Row, 10).Value = cboRsltCylinderStatus.Value
' Add a Modified Date
If txtLastUpdate = "" Then
txtLastUpdate = Date
End If
Cells(ActiveCell.Row, 11).Value = txtLastUpdate
Cells(ActiveCell.Row, 11).NumberFormat = "dd-mmm-yy"
''' End of Step 1
End If
''' Start updating new changes
Sheets("SGS Cylinder List").Select
''' Add a New Location
If (txtRsltLocation.Value <> "" And cboLocation.Value = "") Then
Sheets("Location").Select
Range("A" & ActiveSheet.Rows.Count).End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = txtRsltLocation.Value
Sheets("SGS Cylinder List").Select
Cells(EditingRow, 1).Value = txtRsltLocation.Value
Else
Cells(EditingRow, 1).Value = cboLocation.Value
End If
Cells(EditingRow, 5).Value = txtRsltClientName.Value
Cells(EditingRow, 6).Value = txtRsltWell.Value
Cells(EditingRow, 7).Value = txtRsltJobID.Value
Cells(EditingRow, 8).Value = cboRsltSampleType.Value
Cells(EditingRow, 9).Value = txtRsltDisposalDate.Value
Cells(EditingRow, 10).Value = cboRsltCylinderStatus.Value
End If
''' Reset
cboRsltCylinderStatus.Value = ""
cboLocation.Value = ""
txtRsltClientName.Value = ""
txtRsltWell.Value = ""
txtRsltJobID.Value = ""
cboRsltSampleType.Value = ""
txtRsltDisposalDate.Value = ""
cboRsltCylinderStatus.Value = ""
End Sub
Private Sub ComboBox1_Change()
End Sub
Private Sub UserForm_Initialize()
Set typeList = Range("CylinderTypes")
For Each cell In typeList
cboType.AddItem cell.Value
Next
End Sub
Any help is much appreciated. Thank you
End If
ActiveCell.Offset(1, 0).Select
Loop
The End If at the beginning there is the cause of the error because it doesn't correspond to an opening If statement. Did you mean to put an Else If earlier in your code?
Update: I see this code:
Do Until ActiveCell.Value = ""
cboLocation.AddItem ActiveCell.Value
ActiveCell.Offset(1, 0).Select
There is no corresponding Loop keyword, and it's in the same scope as the End If I mentioned above, it's possible this is throwing the VBA interpreter/compiler off, but don't you have any detailed error messages with line-numbers or anything?
I think it's here .. in your Private Sub cmdSearch_Click()
cboLocation.Clear
Sheets("Location").Select
Range("A2").Select
Do Until ActiveCell.Value = "" '-------> You dont have 'Loop'