I'm stuck on a bit of code. Essentially, I have a dynamic number of sheets where I'm trying to implement the same code. I can get the code to look at the correct sheet, but I'm struggling with the variant. The first line of code works, but only for 1 of the 2 values I'm looking for.
m = Application.Match(ws4.Range("D4").Value, .Columns("AW"), False)
To get the code to read D2 or D4, I've tried these lines of code, to no avail:
'm = Application.Match(ws4.Range("D4").Value, .Columns("AW"), False) Or Application.Match(ws4.Range("D2").Value, .Columns("AW"), False)
'm = ws.Range("AV").Value <> "0"
'm = ws.Range("AV").Value <> 0
'm = Application.Match(ws.Range("AV").Value) <> 0
'm = Application.Match(ws.Range("AV").Value <> 0)
'm = Offset(Application.Match(ws4.Range("D4").Value, .Columns("AW"), False), Application.Match(ws4.Range("D2").Value, .Columns("AW"), False))
When the actual code runs, this is what m is doing:
txt_Name = .Range("E" & m).Value
Also, this is tied to a post on another forum.
https://www.mrexcel.com/forum/excel-questions/1037378-convert-formula-vba-3.html
I think that if I can get this piece solved, I may be able to wrap up this project by end of day tomorrow, so I'm a little anxious.
Related
I need some help with vba code. I'm self-lerning so please be understanding for simply cases ;)
I'm trying to create macro which will be looking for some records/cells in one workbook (FileA) (determined by 3 conditions) and then paste values to another workbook (FileB) and after that find in another tab in FileB some values where condition will be pasted value to match them with looking value (I belivie it could be done somehow with Vlookup but I get stuck).
Below problematic for me part of code (I'm working on some files found in work, no one use it now).
First issue is with Set Update, I don't know why it takes it as "Nothing"... conditions are true - I mean "pp1" is existing in column A in FileA.
Second issue shows when I change i start range to some "later" numbers, eg From i = 2280, macro is ignoring last line where should assign some values (again shows update2 as "nothing") but value if pp2 is existing in W column in tab data...
Dim name As String
name = "[path to file on sharepoint]"
Application.ScreenUpdating = False
Workbooks.Open Filename:=name
a = 1
For i = 2263 To 14000
If Workbooks("FileA").Sheets("Main").Cells(i, 11) = "CANCEL" And Workbooks("FileA").Sheets("Main").Cells(i, 6) = "DENIS" And Workbooks("FileA").Sheets("Main").Cells(i, 5) > 1301358454 Then
pp1 = Workbooks("FileA").Sheets("Main").Cells(i, 1)
If pp1 > 0 Then
Set Update = Workbooks("FileA").Worksheets("Main").Range("A:A").Find(pp1, lookat:=xlPart)
If Update > 0 Then
Update = Update.Row
Workbooks("FileB").Worksheets("lost").Cells(a, 1).Value = Workbooks("FileA").Worksheets("Main").Cells(Update, 5)
pp2 = Workbooks("FileB").Worksheets("lost").Cells(a, 1)
update2 = Workbooks("FileB").Worksheets("data").Range("W:W").Find(pp2, lookat:=xlPart).Row
Workbooks("FileB").Worksheets("lost").Cells(a, 5) = Workbooks("FileB").Worksheets("data").Cells(update2, 43)
I am trying to pull CustomDocumentProperties from a Word Document (that I select using Application.GetOpenFilename) to an Excel Sheet.
I can get the to run code using the number of the item:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties(r).Value
Next r
If I hard code the name of the custom property, it also works:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties("Subject Name").Value
Next r
Obviously in this case, it returns the "Subject Name" 7 times).
I don't want to return all the values - only specific ones based on the values of a named range (in this case DataFields).
DataFields references A1:A7. I would like to take those cells(that contain the names of the custom properties) and paste the value of the corresponding custom properties in C1:C7
However, I can seem to get the code to return values in C1:C7 based on the values in A1:A7.
Based on my (limited) knowledge, I thought that the following would return the desired results, but it's not working for me:
Set ExcelRange = Range("DataFields")
For r = 1 To ExcelRange.Rows.Count Step 1
ExcelWorkbook.Sheets("Sheet1").Cells(r, 3) = WordDoc.CustomDocumentProperties(ExcelRange(r, 1))
Next r
Any help would be appreciated.
Thanks!
I am making a project in VBA and I face a problem with the following code...
It says there is a object or an undefined function, but I don't understand why... Any Idea of what the problem is?
(All the variables are declared and i checked the name of the sheets)
Set FinalRange = ActiveWorkbook.Worksheets("Fiche Horaire").Range("A:A")
emptyroww = WorksheetFunction.CountA(FinalRange)
precautionroww = 0
If emptyroww > 50 Then precautionroww = emptyroww - 50
For a = precautionroww To emptyroww
If ActiveWorkbook.Worksheets("Fiche Horaire").Cells(a, 1).Value = name Then ActiveWorkbook.Worksheets("Fiche Horaire").Cells(a, 4).Value = "it works"
Next
The message it gives is "Application-defined or object-defined error"
If your precautionroww is 0 because your emptyrow is below 50 you try to insert your data in row 0 which is causing the error
Thats a problem within VBA if you have a for loop and start with the same value as your "end" value than i still runs 1 time.
In my program, the user types a Zip Code and gets as an output information related to the Zip Code (province, city, district). To do this, I use the Vlookup function.
So, the user :
Types a Zip code in the main sheet
The program search in a database (in another sheet) in which Zip Code are associated to City, Province, District.
When there is a match, it sends the result to the main pages, so the user can get a city, province, district just by typing the Zip Code. Quite simple process.
I use this code to do so :
If Range("J9").Value <> "N/A" Then 'if there is actually a zip code entered by the user (if not, it will be "N/A")
cityZip = Application.WorksheetFunction.VLookup(sMain.Range("J9").Value,
sZipCodes.Range("B2:E864"), 3, False)
barangayZip = Application.WorksheetFunction.VLookup(sMain.Range("J9").Value,
sZipCodes.Range("B2:E864"), 2, False)
provinceZip = Application.WorksheetFunction.VLookup(sMain.Range("J9").Value,
sZipCodes.Range("B2:E864"), 4, False)
sMain.Range("J7").Value = provinceZip
sMain.Range("J13").Value = cityZip
sMain.Range("J16").Value = barangayZip
Else
End If
It works perfectly when there is a Zip Code which is in my database. But if not, it crashes the execution of the program and I have an error message (like "execution error '1004', unable to read the Vlookup ...).
How to modify my code to just say that if there is no match, then it should just do nothing? I don't know how to introduce this request in a Vlookup function.
Thanks in advance !
EDIT : here is my new code, after following Tim Williams suggestion :
'Using Zip Code
If Range("J9").Value <> "N/A" Then
provinceZip = Application.Lookup(sMain.Range("J9").Value, sZipCodes.Range("B2:E907"), 4, False)
If IsError(provinceZip) = False Then
cityZip = Application.Lookup(sMain.Range("J9").Value, sZipCodes.Range("B2:E907"), 3, False)
barangayZip = Application.Lookup(sMain.Range("J9").Value, sZipCodes.Range("B2:E907"), 2, False)
sMain.Range("J7").Value = provinceZip
sMain.Range("J13").Value = cityZip
sMain.Range("J16").Value = barangayZip
Else
'do nothing
End If
End If
My error is on this line :
provinceZip = Application.Lookup(sMain.Range("J9").Value, sZipCodes.Range("B2:E907"), 4, False)
=> Error 1004, invalid number of arguments
You should read up on VBA error handling. A source such as http://www.cpearson.com/excel/errorhandling.htm might help. That said, try the following code.
You want something like:
Public Function SafeVlookup(lookup_value, table_array, _
col_index, range_lookup, error_value) As Variant
On Error Resume Next
Err.Clear
return_value = Application.WorksheetFunction.VLookup(lookup_value, _
table_array, col_index, range_lookup)
If Err <> 0 Then
return_value = error_value
End If
SafeVlookup = return_value
On Error GoTo 0
End Function
In your code you might call it like:
cityZip = SafeVlookup(sMain.Range("J9").Value, sZipCodes.Range("B2:E864"), 3, _
False, "")
The last parameter is the default value to return if the vlookup failed. So in this example it'd return an empty string.
I usually wrap the vlookup() with an iferror() which contains the default value.
The syntax would be as follows:
iferror(vlookup(....), <default value when lookup fails>)
You can also do something like this:
Dim result as variant
result = Application.vlookup(......)
If IsError(result) Then
' What to do if an error occurs
Else
' what you would normally do
End if
You changed from Vlookup to Lookup, which has less arguments. Using only 2 arguments, you should be fine: provinceZip = Application.Lookup(sMain.Range("J9").Value, sZipCodes.Range("B2:E907") )
SafeVlookup is a good function.
I am still learning VB.
I changed like this and it works for me.
Function SafeVlookup(lookup_value, _
range_lookup, col_index, error_value) As Variant
.....
return_value = Application.WorksheetFunction.vlookup(lookup_value, _
range_lookup, col_index, error_value)
....
End Function
Hope I can use it like this.
Details:
In this segment, I am filling the cell referenced by 'z' with modified date values from cell referenced by 'a', depending on the conditions met. The code crashes at the inner End If line.
Code snippet:
If Range(x).Value =1 Then
If Day(Range(a)) > Day(Range(b)) Then
Range(z).Value = DateSerial(Year(Range(a)), Month(Range(a)), Day(Range(a)-1)) + TimeSerial(20,0,0)
Else
Range(z).Value = DateSerial(Year(Range(a)), Month(Range(a)), Day(Range(a))) + TimeSerial(20,0,0)
End If
ElseIf Range(y).Value =1 Then
Range(z).Value = DateSerial(Year(Range(a)), Month(Range(a)), Day(Range(a)-1)) + TimeSerial(8,0,0)
Else
Range(z).Value = Range(a).Value
End If
Your code is good. Either you have a problem where one of your ranges is pointing to invalid data, or you have corruption in your module.
You can handle corruption by exporting your moodules to a text file (right click module in VBA editor->export) and then import into a clean workbook.
It seems to test fine for me.
I know default properties have been created in order to make code more concise, and I realize objects should not need to be fully qualified with their parent objects but I don't always trust the VBA compiler so my version is a little longer than yours (I've used the cells C1 to C5 to test):
Option Explicit
Sub FullyQualified()
With Excel.ThisWorkbook.Sheets("Sheet1")
If .Range("C1").Value = 1 Then
If Day(.Range("C3").Value) > Day(.Range("C4").Value) Then
.Range("C5").Value = DateSerial(Year(.Range("C3").Value), Month(.Range("C3").Value), Day(.Range("C3").Value - 1)) + TimeSerial(20, 0, 0)
Else
.Range("C5").Value = DateSerial(Year(.Range("C3").Value), Month(.Range("C3").Value), Day(.Range("C3").Value)) + TimeSerial(20, 0, 0)
End If
ElseIf .Range("C2").Value = 1 Then
.Range("C5").Value = DateSerial(Year(.Range("C3").Value), Month(.Range("C3").Value), Day(.Range("C3").Value - 1)) + TimeSerial(8, 0, 0)
Else
.Range("C5").Value = .Range("C3").Value
End If
End With
End Sub