VBA Excel + using match function - vba

I'm having some kind of problem in a piece of code.
I need to find values in a excel book and then copy it to another. The thing is that i need to copy it concept by concept in the right place. The problem is that the file doesnt come every week in the same order.
So i need to find the concept and then copy the next cell that is the value of that concept.
First of all i use need to locate the correct line so i can start copying (this part is easy and its done).
Secondly, having the correct line, i need to find the concept, which in this example im going to use the "619".
After having find the location of this value i store the value in "c_audiovisual".
On Error GoTo ErrhandlerCAV
lRowC_AV = Application.WorksheetFunction.Match(619, Range("A" & line & ":FI" & line), 0) + 1
'On Error GoTo ErrhandlerCAV
Continue:
If errorCAV = 1 Then
c_audiovisual = 0
errorCAV = 0
Else
c_audiovisual = ActiveSheet.Cells(line+ m2, lRowC_AV).Value
End If
I've made a escape in case that this concept isn't present in that line (which sometimes occurs).
Sometimes this piece of code works, and other it doesn't.
When i'm on debug mode (pressing F8) it works. And when i use small files to look for the values it works. On bigger files sometimes don't.
Any ideas?

I faced similar issues. I fixed it with using ThisWorkbook.Sheets("MySheet").Range(...) inside the Match function. Or try ActiveWorkbook as applicable. Let me know if this worked. Curious to know.
Btw I noticed column and row number both are "line". Is that a mistake?

Related

Run-time error '5852' when iterating over revisions

I am easily annoyed by word tracking format changes (even in text that is newly inserted in a revision), so I am using a macro to accept all format changes, once they summed up:
Sub AcceptAllFormatChanges()
Dim xRev As Revision
Dim count As Integer
count = 0
For Each xRev In ActiveDocument.Revisions
If Not xRev Is Nothing Then
If xRev.Type = wdRevisionProperty Then
count = count + 1
xRev.Accept
End If
End If
Next xRev
MsgBox ("Accepted " & count & " format changes")
End Sub
This works fine most of the time, but on some documents, it gives me a run-time error '5852' - Requested object is not available on the "If xRev.Type" line. I am looking for a way to check the object that is yielded by ActiveDocument.Revisions without it throwing a run-time error. Checking for Nothing is not enough. The issue also occures when removing the xRev.Accept or when looping backwards through the revisions.
Update: I tracked down the issue to a Word Bug today. If the macro strikes, I am also unable to iterate through changes in the document using the Next Change button on the review panel. Additionally, if I open the revision panel, the number of revisions jumps back and forth between two numbers. This helped me track down those ghost revisions to a few insertions which included fields (references to other sections). I am able to correct those by deleting/reinserting, so at least now I know how to fix my documents to make the macro work again. Unfortunately, I cannot reproduce the bug in order to actually file a bug report.
The VBA question though remains open: Is there a way for the macro to skip those ghost revisions without throwing a run-time error?
Use the WdRevisionType enumeration instead of cryptic numbers in the code for checking the Type property. Also you may try to check for Nothing before getting the Type property value. The following code works like a charm for me:
Public Sub AcceptSelection()
Dim rev As Revision
For Each rev In Selection.Range.Revisions
rev.Accept
Next rev
End Sub
And the last resort is to replace the for each loop with a reverse for loop.

Find a Specific Cell entry in a Column in VBA

I'm working on creating a userform for reviewing an excel sheet. I need to search a specific column to see if the user has already reviewed the row. If they have, the cell will be filled with "Reviewed", if it hasn't been reviewed yet it will have "Not Reviewed" in it.
Each department has their own Column logging whether the Row has been reviewed or not. I.e. Dept1 may have reviewed the row, while Dept2 has not yet.
I've tried something like
With Sheets("ECR")
UnReviewedRow = .Range(DepartmentReviewColumn:DepartmentReviewColumn).Find(what:="Not Reviewed", after:=.Cells(DepartmentReviewColumn, 3)).Row
End With
But I'm getting an error, not quite sure where it's coming from though. The hardcoded "3" is because I know that all entries begin at row three, everything above is headers.
I've found a few different similar questions, but they all assume that the column being searched will be the same every time. My issue is that I don't want to code this for each department, I'd like to be a bit more elegant than that.
Something like this:
With Sheets("ECR")
UnReviewedRow = .Range(DepartmentReviewColumn & ":" & DepartmentReviewColumn).Find(What:="Not Reviewed", After:=.Cells(3, DepartmentReviewColumn)).Row
End With
Modifications made:
Range() now takes a properly built string argument;
Cells() now has a row number as its first argument, and a column expressed as a string as its second argument.
Note that this code will fail with Object variable or With block variable not set if the search string is not found. You can handle this situation by initializing UnReviewedRow to e.g. 0 (zero) and putting On Error Resume Next above the Find call, and On Error Resume <either 0 or your original error handler's label> below the Find call. Then, check if UnReviewedRow = 0 and act appropriately.
Always put Option Explicit at the top of modules and classes, and compile your code (Debug / Compile VBAProject). When posting, include the text of all errors encountered.

cant get this formula to work, although the top and bottom code works fine

I did a search on this site, but can't find an answer to my problem. I am using VB 2010 Express. I'm trying to write formulas into a Excel spreadsheet.
oWorkSheet = oBook.Sheets(STRName)
'my variables are all declared strings
'this section works
FMLAValue1 = "=-L8"
FMLAValue2 = "=SUM($I$7;$F$8:$H$8)"
FMLAValue3 = "=SUM(M8:U8)"
oWorkSheet.Range("H8").Select()
oWorkSheet.Range("H8").Formula = FMLAValue1
oWorkSheet.Range("H8").AutoFill (oWorkSheet.Range("H8:H40"))
'up to here
'now this is the problem code below: if I paste the formula into a
'excel sheet cell, it works.
oWorkSheet.Range("I8").Select()
oWorkSheet.Range("I8").Formula = FMLAValue2
'**the line above gives an "Exception from HRESULT: 0x800A03EC "**
oWorkSheet.Range("I8").AutoFill (oWorkSheet.Range("I8:I40"))
'this part also works
oWorkSheet.Range("L8").Select()
oWorkSheet.Range("L8").Formula = FMLAValue3
oWorkSheet.Range("L8").AutoFill (oWorkSheet.Range("L8:L40"))
oWorkSheet.Range("J7").Select()
oWorkSheet.Range("J7").Formula = "=I7"
The formula stored in your FMLAValue2 variable doesn't look correct, you shouldn't have a ; (semi-colon) to separate the numbers, you should use a , (comma).
Use this instead:
FMLAValue2 = "=SUM($I$7,$F$8:$H$8)"
and you should hopefully be ok, unless there are other issues somewhere that I haven't noticed.
To test whether a formula is correct, use FormulaDesk. It will immediately tell you if there is an error, and pinpoint exactly where it is in your formula. You can then paste the formula into your VBA code, knowing that it works.
The image below shows how it can pinpoint exactly where your error is within the formula. So in your case, paste the non-working error into a cell in Excel, then view the error report in FormulaDesk to find out the exact cause.
[Disclosure: I am the author of FormulaDesk. I think it will definitely help the poster find any errors]

VBA - Unable to get TextBoxes Property of Worksheet Class

Fairly new to VBA and I just started encountering the error Unable to get TextBoxes Property of Worksheet Class.
It highlights the following line:
ActiveSheet.TextBoxes("txtFilePath").Text = Application.ActiveWorkbook.Path & "\"
The mystery to me is that I know this line was working before without any problems. The only thing I could think of is that I was messing with protecting the sheet, but even now that it is unprotected I still get the error. I've also tried the following solution, but encounter the same error:
ActiveSheet.OLEObjects("txtFilePath").Object.Text = Application.ActiveWorkbook.Path & "\"
Can anyone explain to me why I am encountering the error and why I started encountering it so randomly? How can I fix it?
Sadly enough, there is no clear way on how to access the properties of a grouped shape (a group is basically a shape, at least in 2010). There are two possible solutions to go about this.
One is obviously to ungroup the textboxes, access the textbox in question and modify it, then regroup them. However, this will prove to be difficult to track as the Group number increases with every regroup that you do. This can pose a potential problem if you have multiple groupings.
The other way, surprisingly enough, is to do it the 'dirty' way, which is basically what the macro recorder gives us in a nutshell. The way to do it is to select the shape itself and to change the text directly, emulating a direct click-and-type motion manually. I've tested it and it works, even when the textbox in question is grouped multiple times.
Sub Macro3()
foo = Application.ActiveWorkbook.Path & "\"
ActiveSheet.Shapes.Range(Array("txtFilePath")).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = foo
End Sub
Sorry to answer my own question. Figured it out by accident.
The problem was that I had my textbox (txtFilePath) grouped together with another textbox on my worksheet. When I ungrouped them everything worked fine. Can anyone explain to me why the grouping would make a difference?

Unable to copy a table from a word document and paste it in to a excel sheet in VB.NET

Following is the code snippet i tried
r.Cells(3).Range.Copy()
.Sheets("sheet4").Range("F" & k).Select()
.Sheets("sheet4").Cells(k, 6).Columnwidth = 14
.Sheets("sheet4").paste()
k = k + 1
this doesnt copy the entire string inside one cell.I tried to use the pastespecial()
when i use pastespecial its showing error that pastespecial is not a member of Application Class
.Sheets("sheet4").Range("F" & k).pasteSpecial(ExApp.XlPasteType.xlPasteAll, False, False)
Any help would be appreciated.
I haven't ever tried this before, but it seems to be an Excel-Word coordination issue. From my MS Office coupling experience, I know that there are many alternatives, which theoretically should be fine, provoking errors. Instead spending time on finding out a working alternative (perhaps there is none), why not removing the problem (Excel-Word communication)? You can store the Word value in a temporary variable and write this variable to the Excel cell.