I have created a userform and one of the commandbuttons launches another userform in which data can be entered into. This data is then added to a table in a worksheet, the userform is then unloaded and the user is returned to the original userform. The error occurs when the data is meant to be entered into the worksheet. This userform works perfectly on its own, but when it is launched from the first userform, this is when the error occurs.
Private Sub CommandButton1_Click()
'check all fields are filled
Dim nextRow As Integer
Dim nextCell As String
If Len(Trim(ComboBox1.Value)) = 0 Then
MsgBox "All feilds must be filled"
Exit Sub
End If
If Len(Trim(TextBox1.Value)) = 0 Then
MsgBox "All feilds must be filled"
Exit Sub
End If
If Len(Trim(TextBox2.Value)) = 0 Then
MsgBox "All feilds must be filled"
Exit Sub
End If
'Check if supplier ID already exists
Dim FindString As String
Dim Rng As Range
FindString = TextBox1.Value
If Trim(FindString) <> "" Then
With Sheet4.Range("B:B")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, False
MsgBox "Sorry Bro, " & FindString & " already exists!"
Exit Sub
Else
FindString = TextBox2
If Trim(FindString) <> "" Then
With Sheet4.Range("D:D")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, False
MsgBox "Sorry Bro, the Ordering Details you entered:" & vbNewLine & _
"'" & FindString & "'" & vbNewLine & _
"Already exists in our Database!" & vbNewLine & _
"U wanna check ur data?"
Exit Sub
End If
End With
End If
End If
End With
End If
'enter supplier ID into sheet
Sheet4.Activate
nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count
nextCell = Cells(nextRow + 2, 2).Activate
'this is where the error occurs
ActiveCell.Value = TextBox1.Value
ActiveCell.Offset(0, 1).Value = ComboBox1.Value
ActiveCell.Offset(0, 2).Value = TextBox2.Value
Sheet2.Activate
Unload Me
End Sub
I'm not sure why it doesn't work because personally I avoid the use of "Activate". Maybe you can try if this works:
'Previous code that worked fine
nextRow = ActiveSheet.Range("B2", Range("B2").End(xlDown)).Count
With ActiveSheet.Cells(nextRow + 2, 2)
.Value = TextBox1.Value
.Offset(0, 1).Value = ComboBox1.Value
.Offset(0, 2).Value = TextBox2.Value
End With
Sheet2.Activate
Unload Me
End Sub
Hope this does the job! (Note that this is my first answer so I'm very open to feedback)
Related
I need help with a project i am working on. I am trying to find a certain value in Column A - once that value is found - move to Column B & copy the contents of Column B-N into the row below it - there are breaks or rows in the data and it only seems to be "finding down" until the first break in the data - would someone be able to help?
Thanks for the time - my current code is below:
Worksheets("Chg").Activate
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Set FoundTheCell = Selection.Find(What:="'8/2020", After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If FoundTheCell Is Nothing Then
MyMsg = "The column 'During' was not found." & vbCrLf
GoTo err_subCopyLastRowandReplace
Else
FoundTheCell.Offset(0, 1).Activate
lastrowchange = Range(Selection, Selection.End(xlRight)).Select
lastrowchange.Offset(1).Formula = lastrowchange.Formula
End If
exit_subCopyLastRowandReplace:
Application.Cursor = xlDefault
Application.ScreenUpdating = True
Exit Sub
err_subCopyLastRowandReplace:
MyMsg = MyMsg & "Errored out in subCopyLastRowandReplace" & vbCrLf
MyMsg = MyMsg & "Error was: " & Err.Description
MsgBox MyMsg, vbOKOnly, MyTitle
Resume exit_subCopyLastRowandReplace
Try these changes and see if they work for you. I've done away with any selections or activations, and changed a few things around that were throwing errors for me.
Note that this will only apply to the first entry of 8/2020.
Sub test()
Dim SearchRange As Range, FoundTheCell As Range, ws As Worksheet, lCol As Long, MyMsg As String, MyTitle As String
Set ws = Worksheets("Chg")
Set SearchRange = ws.Range(Range("A1"), Range("A" & Range("A" & Rows.Count).End(xlUp).Row))
Set FoundTheCell = SearchRange.Find(What:="8/2020")
If FoundTheCell Is Nothing Then
MyMsg = "The column 'During' was not found." & vbCrLf
GoTo err_subCopyLastRowandReplace
Else
lCol = ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Range(FoundTheCell.Offset(1, 1), FoundTheCell.Offset(1, lCol - 1)).Formula = Range(FoundTheCell.Offset(, 1), FoundTheCell.Offset(, lCol - 1)).Formula
End If
exit_subCopyLastRowandReplace:
Application.Cursor = xlDefault
Application.ScreenUpdating = True
Exit Sub
err_subCopyLastRowandReplace:
MyMsg = MyMsg & "Errored out in subCopyLastRowandReplace" & vbCrLf
MyMsg = MyMsg & "Error was: " & Err.Description
MsgBox MyMsg, vbOKOnly, MyTitle
GoTo exit_subCopyLastRowandReplace
End Sub
Sub Find_Value()
Dim FindString As Double
Dim Rng As Range``
FindString = InputBox("Enter a search value")
If Trim(FindString) <> "" Then
With Sheets("Sheet1").Range("D:D")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
the inputbox from the code has to read the barcode data which usually gives data in this format "0.00000E+12". However, my present code just reads numbers...what do i do ? i am new to vba so... please help :)
Try the next code, please:
Sub Find_Value()
Dim sh As Worksheet, FindString As String, Rng As Range, lastRow As Long
Set sh = sheets("Sheet1")
lastRow = sh.Range("D" & Rows.count).End(xlUp).Row
FindString = InputBox("Enter a search value")
If FindString = "" Then MsgBox "No input...": Exit Sub
FindString = Trim(FindString) 'to trim spaces if they exist in the copied string...
If Trim(FindString) <> "" Then
With sh.Range("D1:D" & lastRow)
.TextToColumns Destination:=.cells(1), FieldInfo:=Array(1, 2)
Set Rng = .Find(What:=FindString, _
After:=.cells(.cells.count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
My vba code below, how do it faster ? (obs: i have +- 33000 lines of values)
I search codes from products to my company, i need help to do it faster.
Private Sub TextBox1_Enter()
Dim FindString As String
Dim Rng As Range
FindString = TextBox1.Text
If Trim(FindString) <> "" And Len(TextBox1.Text) = 6 Then
With Sheets("CADMAT").Range("B:B") 'searches all of column B
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Dim ultimalinha As Object
Set ultimalinha = Plan3.Range("A35565").End(xlUp)
ultimalinha.Offset(1, 0).Value = TextBox1.Text
ultimalinha.Offset(1, 1).Value = TextBox2.Text
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus
Else
MsgBox "Produto não existe na tabela!" 'value not found
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus
End If
End With
End If
End Sub
Option Explicit
Private Sub TextBox1_Enter()
Application.ScreenUpdating = False
Code here ...
Application.ScreenUpdating = True
End Sub
I cannot find or create VBA code to allow pasting copied text from one cell in another sheet(sheet2) into a previously created comment in another sheet(sheet1).
Here is the code I have successfully compiled thus far, and I am stuck on how to get the text found into the comment box.
Sub For_Reals()
'Add Comment
Sheets("Sheet1").Range("F2").AddComment
Range("F2").Comment.Visible = False
'Find Value in Sheet2 based on Value from Sheet1
Dim FindString As String
Dim Rng As Range
FindString = Sheets("Sheet1").Range("F2").Value
If Trim(FindString) <> "" Then
With Sheets("Sheet2").Range("C:C")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
'Copy Value 4 cells to the right of found Value
Selection.Offset(0, 4).Copy
'Need Code to paste copied value in previously created comment
End Sub
Rather than copy and paste the cell value into the comment, you create the text at the same time as creating the comment box. If a comment box already exists an error is raised - so remove any comment boxes in that cell beforehand.
The VBA help gives this as an example:
Worksheets(1).Range("E5").AddComment "Current Sales"
So with this in mind, this code will do the trick:
Sub For_Reals()
'Find Value in Sheet2 based on Value from Sheet1
Dim FindString As String
Dim Rng As Range
FindString = Sheets("Sheet1").Range("F2").Value
If Trim(FindString) <> "" Then
With Sheets("Sheet2").Range("C:C")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
'Remove any existing comments, create comment and add text.
If Not Rng Is Nothing Then
Sheets("Sheet1").Range("F2").ClearComments
Sheets("Sheet1").Range("F2").AddComment Rng.Offset(0, 4).Value
Range("F2").Comment.Visible = True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
Final code I ended up with is below. Added a loop to run through the column, and added a second reference to pull both the definition and description into the comment. Thank you Darren Bartrup-Cook for helping me out when I was stuck!
Sub Add_Comment_As_Def_Desc_Reference()
'Posted by Jeff Barrett 2015-04-10
Dim FindString1 As String
Dim Rng1 As Range
Dim sCommentText1 As String
Dim sCommentText2 As String
Dim str1 As String
Dim str2 As String
Dim cmmt As String
Dim i As Integer
str1 = "Definition: "
str2 = "Description: "
'Loop Code, must specify range for i based on # of FieldAlias
Sheets("Fields").Select
Range("F4").Select
For i = 4 To 59
'Find Definition & Description in NASDefs based on Value from FieldAlias
FindString1 = ActiveCell.Value
If Trim(FindString1) <> "" Then
With Sheets("NASDefs").Range("C:C")
Set Rng1 = .Find(What:=FindString1, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
End If
'Remove any existing comments, create comment and add text in FieldAlias
If Not Rng1 Is Nothing Then
ActiveCell.ClearComments
sCommentText1 = Rng1.Offset(0, 4).Value
sCommentText2 = Rng1.Offset(0, 5).Value
ActiveCell.AddComment.Text Text:=str1 & Chr(10) & Chr(10) & sCommentText1 & Chr(10) & Chr(10) & str2 & Chr(10) & Chr(10) & sCommentText2
ActiveCell.Comment.Visible = False
ActiveCell.Comment.Shape.AutoShapeType = msoShapeRoundedRectangle
'Format lines of text
With ActiveCell.Comment.Shape.TextFrame
.Characters.Font.ColorIndex = 5
End With
Else
MsgBox "Nothing found"
End If
'End Loop
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Select
Next i
'Resize Comment to fit text
'posted by Dana DeLouis 2000-09-16
Dim MyComments As Comment
Dim lArea As Long
For Each MyComments In ActiveSheet.Comments
With MyComments
.Shape.TextFrame.AutoSize = True
If .Shape.Width > 300 Then
lArea = .Shape.Width * .Shape.Height
.Shape.Width = 300
' An adjustment factor of 1.1 seems to work ok.
.Shape.Height = (lArea / 200) * 0.6
End If
End With
Next ' comment
End Sub
I am getting a type mismatch error for the below at the if iRowValue null check
what i am trying to do is to determine if the value already exists in the sheet then update that column or else append at the end.
Public iRowValue As Long
Public iRow As Long
----------
Private Sub Update_Click()
Dim Rng As Range
Dim FindString1 as String
With ws.Range("A:A")
Set Rng = .Find(What:=FindString1, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Rng Is Nothing Then
MsgBox "Name does not Exists"
Else
iRowValue = Rng.Row
End If
End With
**If iRowValue <> "" Then**
iRow = iRowValue
Else
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
End If
ws.Cells(iRow, 1).Value = Me.FirstName.Value
ws.Cells(iRow, 2).Value = Me.LastCode.Value
End Sub
iRowValue is declared as Long, you then try to compare it to a String - which can't work. Simply compare against 0 and it should work.