I am at a very basic part of VB and I am stuck with this relentless issue. I am trying to copy data from a form to a bunch of cells in excel and I am facing this error.
Object doesn't support this property or method: Runtime Error - 438
Although the first row gets inserted. The issue is with the second row. I don't see the problem. Kindly help
Sub sbInsertingRows()
Range("A3").EntireRow.Insert
End Sub
Sub EditAdd()
sbInsertingRows
Cells(3, 19).Value = Order_Details_Form.Controls(TextBox_orderid).Value
Cells(3, 20).Value = Order_Details_Form.Controls(TextBox_name).Value
Cells(3, 24).Value = Order_Details_Form.Controls(TextBox2).Value
ActiveWorkbook.Save
End Sub
EditAdd gets called on click
I think TextBox_orderid is a name of your textbox control not a variable so you should enclose it with " like:
Cells(3, 19).Value = Order_Details_Form.Controls("TextBox_orderid").Value
Or you can simply use:
Cells(3, 19).Value = Order_Details_Form.TextBox_orderid.Value
Apply this at your other two lines.
Related
I'm writing a macro that will populate an excel file with user inputs from active x controls in word. I've got almost everything working except that I keep getting an error message when I try and select cell A1 in the sheet that I want to use in the workbook. Here is the code:
Workbooks.Open ("mypath\myfile.xlsm")
Workbooks("myfile.xlsm").Activate
Worksheets("sheet1").Select
Range("A1").Select
Do Until (IsEmpty(ActiveCell.Value))
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = n
ActiveCell.Offset(0, 1).Value = a
ActiveCell.Offset(0, 2).Value = b
ActiveCell.Offset(0, 3).Value = c
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Workbooks("myfile.xlsm").Save
Workbooks("myfile.xlsm").Close
The variables in this block of code are the values of the active x controls and are located much further up in the sub. This block of code is a small part of an if statement within the sub. Anyhow, when I take Range("A2").Select out of the code, it works just fine except for the fact that the information that I want to input does not go to the right spot (since it didn't select range A1 to begin with).
The error I get is type mismatch 4218.
Referencing the Excel object model gives you access to some global objects defined in that object model.
VBA resolves identifiers in this order:
Current procedure
Current module
Current project
VBA standard library
Host application object model
Any other referenced library, in the order they appear in the references dialog
So when you invoke Range meaning to be a call to the Excel object model, you actually invoke the same-name Range global member that's defined in the Word object model.
Note I say member and mean it: these are unqualified member calls to Global.Range. This is important, because a member implies an object, and since everything in the Excel object model (Word's too) has an Application property, then if you're not explicit about exactly what you're referring to, you might be implicitly creating an Excel.Application object, that you can't quite clean up properly. This usually translates into a "ghost" EXCEL.EXE process lingering in Task Manager well after your macro finishes running.
The trick is to make that reference explicit, and explicitly constrain its lifetime - a With block is perfect for this:
With New Excel.Application
With .Workbooks.Open(path)
With .Worksheets("Sheet1")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRow, 1) = n
.Cells(lRow, 2) = a
.Cells(lRow, 3) = b
.Cells(lRow, 4) = c
.Columns("A:D").EntireColumn.AutoFit
End With
.Save
.Close
End With
.Close
End With
I'm guessing as I don't usually run Excel from Word, but I think the problem might be related to everything being unqualified from Word.
If Workbooks.Open is working, then we can just hang everything related to that workbook on that..
Try the following code instead:
Dim myWkBk As Workbook, lRow As Long
Set myWkBk = Excel.Application.Workbooks.Open("mypath\myfile.xlsm")
With myWkBk.Sheets("sheet1")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRow, 1) = n
.Cells(lRow, 2) = a
.Cells(lRow, 3) = b
.Cells(lRow, 4) = c
.Columns("A:D").EntireColumn.AutoFit
End With
myWkBk.Save
myWkBk.Close
I've got it figured out. #Cindy Meister I just needed to add an ActiveSheet. qualifier on the troubled line:
Workbooks.Open ("H:\Second Rotation\OBI project\answersUsers.xlsm")
Workbooks("answersUsers.xlsm").Activate
Sheets("Answers Users").Select
ActiveSheet.Range("A1").Select
Do Until (IsEmpty(ActiveCell.Value))
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = n
ActiveCell.Offset(0, 1).Value = cwid
ActiveCell.Offset(0, 2).Value = mann
ActiveCell.Offset(0, 3).Value = dept
Columns("A:D").EntireColumn.AutoFit
Workbooks("answersUsers.xlsm").Save
Workbooks("answersUsers.xlsm").Close
Dim myWkBk As Workbook, lRow As Long
I was wondering if someone could assist me please. I have created a very simple userform to log information. My issue is however when one of the fields is blank I am receiving an error message :
Sub or Function not defined.
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a Name number
If Trim(QueryType.Value) = "" Then
QueryType.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Format(Now, "DD/MM/YYYY")
ws.Cells(iRow, 2).Value = Format(Now, "HH:MM")
ws.Cells(iRow, 3).Value = Application.UserName
ws.Cells(iRow, 4).Value = CType.Value
ws.Cells(iRow, 5).Value = IName.Value
ws.Cells(iRow, 6).Value = QType.Value
ws.Cells(iRow, 7).Value = 1
ws.Cells(iRow, 8).Value = Format(Date, "MMM-YY")
'ws.Cells(iRow, 9).Value = Application.WorksheetFunction.VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0)
'ws.Cells(iRow, 9).Value = Application.WorksheetFunction.IfError(VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0), "")
ws.Cells(iRow, 10).Value = Application.WorksheetFunction.VLookup(InternalName.Value, Sheet2.Range("C1:E23"), 3, 0)
ws.Cells(iRow, 11).Value = "IB"
Unload Me
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
End Sub
The issue is in either one of the commented out lines where I receive the error. I only receive if I leave the dropdown box empty. If populated then no error occurrs. I could easily fit an extra menu option for "Not Applicable" however would rather it was just blank. Does anyone have any suggestions at all please?
Three things.
1- VLookup is not a VBA function on its own, it's a member method of either the Application object or the WorksheetFunction object. So you should qualify VLookup by either, even if you had it inside another method, such as WorksheetFunction.IfError(...).
ws.Cells(iRow, 9).Value =
WorksheetFunction.IfError(WorksheetFunction.VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0), "")
' ^^^^^^^^^^^^^^^^^^
2- The same methods in WorksheetFunction and in Application objects work differently. In the former, an error is raised in VBA if the result is an error (such as: no match for the searched value). In the latter, no error is raised, but the returned value is an Error Variant. This latter form is usually safer for VBA, because you dont need to have some On Error Resume Next or so, but you can just check the result with If(IsError(result)).
3- When your search criteria is empty or unmatched, you had the error raised due to the use of WorksheetFunction.VLookup (According to 2). If your intention is to just set the resulting value and proceed, you can use Application.VLookup instead:
ws.Cells(iRow, 9).Value = Application.IfError(Application.VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0), "")
p.s. I personally prefer Application. most of the time. Some prefer WorksheetFunction mostly because it offers Intellisense, but I find that pretty useless, because the arguments of the methods in Intellisense are unnamed and untyped, i.e. VLookup(arg1, arg2, arg3, [arg4]).. (non-sense to me).
has anyone had an issue in excel vba where a loop to copy and paste rows and check for certain criteria sometimes leaves out information and after i try to debug and run through it works fine
I have a loop
For rnum = 3 To LastRow
'if all cells are equal to the Comboboxes then copy and paste the row
ThisWorkbook.Activate
If Sheets("Revised Budget").Cells(rnum, 1).Value = BUval And _
Sheets("Revised Budget").Cells(rnum, 2).Value = TeamLocVal And _
Sheets("Revised Budget").Cells(rnum, 3).Value = YrVal And _
Sheets("Revised Budget").Cells(rnum, 9).Value = InstVal Then
Sheets("Revised Budget").Range(Cells(rnum, 1), Cells(rnum, 12)).Copy
NewWkbk.Sheets("Actual").Activate
If NewWkbk.Sheets("Actual").Range("A1").Offset(1, 0).Value = "" Then
NewWkbk.Sheets("Actual").Range("A1").Offset(1, 0).PasteSpecial
Else
NewWkbk.Sheets("Actual").Range("A1").End(xlDown).Offset(1, 0).PasteSpecial
End If
Application.CutCopyMode = False
End If
Next rnum
Are your variables strings or integers?
If any of them are strings you can try making sure it's not an issue with capitalization on your string matches.
ucase(Sheets("Revised Budget").Cells(rnum, 3).Value) = ucase(YrVal)
Consider updating all of your .value to .value2. I was running into a (potentially) similar issue, and that fixed it for me.
I think I may have figured it out through some more research. The page in which the macro was running through had data that was filtered. I put in a code that would ShowAllData and I believe that should fix the issue.
New to vba and trying to write a simple if/then statement in sequential order to return a value from different sheets.
If Cells(4, 4).Value = "water stress" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws2.Range("a3:b12"), 2)
End If
If Cells(4, 4).Value = "fire" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws3.Range("a3:b12"), 2)
End If
When i run the code with only the first if statement, it runs fine. When I add in the second if statement, I get a run-time error '1004' and am unable to get the vlookup property of the worksheetfunction class. I am new, and not sure how to trouble shoot.
I am programming a little and easy add client code. But when I add a client it gives me this little error where I can't get rid off. Maybe it will be an easy solution. Here is my code:
Private Sub btn_Toevoegen_Click()
Dim laatsteKlantNummer As Integer
Range("B4:B13").End(xlDown).Select
laatsteKlantNummer = ActiveCell.Value
ActiveCell.Offset(1, 0).Value = txtKlant
ActiveCell.Offset(1, 1).Value = txtNaam
ActiveCell.Offset(1, 2).Value = txtAdres
ActiveCell.Offset(1, 3).Value = txtWoonplaats
ActiveCell.Offset(1, 4).Value = txtContact
Me.Hide
Range("B4:B13").Sort Key1:=Range("B4:B13"), Order1:=xlAscending
End Sub
This code works properly but the thing happening is when it places it in the excel worksheet it is placed as text and not as number. So it gives me the error: "Number stored as text". After that I can click convert to number. Like this:(http://i.imgur.com/mfMnGFI.png) But is it possible to code it instead of clicking it all the time?
I am not sure on what line you get this error, but for example if its this one:
ActiveCell.Offset(1, 4).Value = txtContact
You can add this to 'convert' it to a number:
ActiveCell.Offset(1, 4).Value = txtContact + 0